php-elasticaHow can I use Elastic Search with PHP?
Elasticsearch can be used with PHP by using the official Elasticsearch client library for PHP. This library allows you to communicate with an Elasticsearch cluster from your PHP code.
Example code for connecting to an Elasticsearch cluster in PHP:
<?php
require 'vendor/autoload.php';
$hosts = [
'localhost:9200'
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
Output example
Array
(
[_index] => my_index
[_type] => my_type
[_id] => my_id
[_version] => 1
[result] => created
[_shards] => Array
(
[total] => 2
[successful] => 1
[failed] => 0
)
[created] => 1
)
The code above:
require 'vendor/autoload.php';
: Loads the Elasticsearch client library$hosts = [ 'localhost:9200' ];
: Sets the address of the Elasticsearch cluster$client = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
: Creates a connection to the Elasticsearch cluster$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => ['testField' => 'abc'] ];
: Sets the parameters for the index request$response = $client->index($params);
: Sends an index request to the Elasticsearch clusterprint_r($response);
: Prints the response from the Elasticsearch cluster
For more information on using Elasticsearch with PHP, see the official documentation.
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 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...