How to write and read from collections #1049
-
I'm trying to index documents in newly created collections. I know that I can define a specific connection at create a client, but I need to read and write into multiple collection but I'm not sure how to achieve this. <?php
use Solarium\Client;
use Solarium\Core\Client\Adapter\Curl;
// create Client
$adapter = new Curl();
$eventDispatcher = new EventDispatcher();
$options = [
'endpoint' => array(
'localhost' => array(
'host' => $host,
'port' => $port,
),
)
];
$client = new Client($adapter, $eventDispatcher, $options);
// create collection test_simple
$collectionQuery = $this->client->createCollections();
$action = $collectionQuery->createDelete(['name' => 'test_simple']);
$collectionQuery->setAction($action);
$this->client->collections($collectionQuery);
// create collection test_complex
$collectionQuery = $this->client->createCollections();
$action = $collectionQuery->createDelete(['name' => 'test_complex']);
$collectionQuery->setAction($action);
$this->client->collections($collectionQuery);
// now I try to index different documents into different collections but it seems to fail:
$update = $this->client->createUpdate([
'collection' => 'test_simple',
]);
$indexDocument = $update->createDocument(['id' => 'c1ee078b-289e-42e6-8040-d5ea3288e804', 'title' => 'Test']);
$update->addDocuments([$indexDocument], true);
$update->addCommit();
$result = $this->client->update($update); But it fails with:
I'm setting the collectino as |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
You're calling the wrong method to create collection (twice). This should be $action = $collectionQuery->createDelete(['name' => 'test_simple']); After that, you have to add an endpoint that points to your new collection to the client instance. $endpoint = [
'key' => 'test_simple', // this is how you want to identify the endpoint, doesn't have to be the collection name
'host' => $host,
'port' => $port,
'collection' => 'test_simple',
];
$this->client->addEndpoint($endpoint); You can now use the endpoint key when executing an update (or any other) query. $update = $this->client->createUpdate(); // no options needed here for your use case
// add documents, commit
$result = $this->client->update($update, 'test_simple'); // execute against endpoint identified as 'test_simple' |
Beta Was this translation helpful? Give feedback.
-
From your question regarding schemas, I assume you're trying to configure which schema to use for your new collection? You can specify the name of a configuration to use for the collection. $action = $collectionQuery->createCreate();
$action->setName('test_simple')
->setCollectionConfigName('techproducts'); (I prefer the above syntax, but If you're not sure what "name of a configuration" means here and how it relates to the schema, start with reading configsets in the ref guide. |
Beta Was this translation helpful? Give feedback.
From your question regarding schemas, I assume you're trying to configure which schema to use for your new collection? You can specify the name of a configuration to use for the collection.
(I prefer the above syntax, but
$collectionQuery->createCreate(['name' => 'test_simple', 'collection.configName' => 'techproducts'])
also works.)If you're not sure what "name of a configuration" means here and how it relates to the schema, start with reading configsets in the ref guide.