Home>Article>Backend Development> How to use Elasticsearch in PHP
This article mainly introduces the method of using Elasticsearch in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Course Recommendations→ :"Elasticsearch full-text search practice"(practical video)
From the course"Ten million-level data concurrency solution (theoretical practice)"
Using Elasticsearch in PHP
composer require elasticsearch/elasticsearch
will automatically load the appropriate version! My php is 5.6, it will automatically load the 5.3 elasticsearch version!
Using version ^5.3 for elasticsearch/elasticsearch ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 4 installs, 0 updates, 0 removals - Installing react/promise (v2.7.0): Downloading (100%) - Installing guzzlehttp/streams (3.0.0): Downloading (100%) - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%) - Installing elasticsearch/elasticsearch (v5.3.2): Downloading (100%) Writing lock file Generating autoload files
Simple use
es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build(); } public function search() { $params = [ 'index' => 'megacorp', 'type' => 'employee', 'body' => [ 'query' => [ 'constant_score' => [ //非评分模式执行 'filter' => [ //过滤器,不会计算相关度,速度快 'term' => [ //精确查找,不支持多个条件 'about' => '谭' ] ] ] ] ] ]; $res = $this->es->search($params); print_r($res); } }
search();
Execution results
Array ( [took] => 2 [timed_out] => [_shards] => Array ( [total] => 5 [successful] => 5 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 1 [max_score] => 1 [hits] => Array ( [0] => Array ( [_index] => megacorp [_type] => employee [_id] => 3 [_score] => 1 [_source] => Array ( [first_name] => 李 [last_name] => 四 [age] => 24 [about] => 一个PHP程序员,热爱编程,谭康很帅,充满激情。 [interests] => Array ( [0] => 英雄联盟 ) ) ) ) ) )
The following are some official examples:
require '../vendor/autoload.php'; use Elasticsearch\ClientBuilder; $client = ClientBuilder::create()->build();
Add configuration
$hosts = [ '127.0.01:9200', // IP + Port ]; $client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts ->build(); // Build the client object
or
$hosts = [ '127.0.01:9200', // IP + Port ]; $clientBuilder = ClientBuilder::create(); // Instantiate a new ClientBuilder $clientBuilder->setHosts($hosts); // Set the hosts $client = $clientBuilder->build(); // Build the client object
// Index 一个文档 $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => ['testField' => 'abc'] ]; $response = $client->index($params); print_r($response);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->get($params); print_r($response);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ] ]; $response = $client->search($params); print_r($response);
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->delete($params); print_r($response);
The results are as follows
Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 3 [result] => deleted [_shards] => Array ( [total] => 2 [successful] => 1 [failed] => 0 ) [_seq_no] => 2 [_primary_term] => 1 )
$deleteParams = [ 'index' => 'my_index' ]; $response = $client->indices()->delete($deleteParams); print_r($response);
$params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ] ]; $response = $client->indices()->create($params); print_r($response);
The above is the entire content of this article. I hope it will be helpful to everyone's learning. More For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
PHP data structure basic stack
PHP methods and parameter comments for operating Beanstalkd
The above is the detailed content of How to use Elasticsearch in PHP. For more information, please follow other related articles on the PHP Chinese website!