php-elasticaHow do I use PHP Elastica to create an index?
-
First, install the Elastica library into your project using composer:
composer require "elastica/elastica:~2.0"
-
Next, create a new
\Elastica\Client
instance and set the connection parameters for the Elasticsearch server:
$client = new \Elastica\Client(['host' => 'localhost', 'port' => 9200]);
- Then, create an index with the desired settings:
$index = $client->getIndex('my_index');
$index->create(array(
'number_of_shards' => 3,
'number_of_replicas' => 2
), true);
- Finally, add documents to the index:
$doc1 = new \Elastica\Document(1, array('title' => 'Foo', 'content' => 'Foo bar'));
$doc2 = new \Elastica\Document(2, array('title' => 'Bar', 'content' => 'Bar foo'));
$docs = array($doc1, $doc2);
$index->addDocuments($docs);
- You can then check the index status with:
$status = $index->getStatus();
echo $status->getNumberOfDocuments(); // Outputs: 2
-
To learn more about creating and managing indexes with Elastica, refer to the official documentation.
-
You can also find more detailed examples in the Elastica GitHub repository.
More of Php Elastica
- How do I configure PHP Elastica using YML?
- How can I use PHP and Elastica to parse XML data?
- How can I use Elastic Search with PHP?
- How can I use Amazon Elastic Transcoder with PHP?
- How do I use Elastic Search with PHP?
- How can I use the Elasticsearch PHP client to interact with an Elasticsearch server?
- How can I use Elastica to create a query in PHP?
- How can I use PHP to create an elastic query?
- How can I use a PHP Elastic Query Builder to create an effective search query?
- How can I use the Elastica options with PHP?
See more codes...