php-elasticsearch used (1)

installation

1, is introduced in composer.json elasticsearch-php file:

{

      "require":{

               "elasticsearch/elasticsearch":"~6.0"

       }

}

2, with the composer client

curl -s http://getcomposer.org/install | php

php composer.phar install --no-dev

3, the introduction of automatic loading files (if not already incorporated) in the project, and examples of a client

require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

4, a document indexing

In elasticsearch-php, almost all operations are used to configure the associative array. REST path (endpoint), documentation, and optional parameters are used to configure the associative array

In order to index a document, we want to specify four pieces of information: index, type, id and a body. Build an associative array of key-value pairs to complete contents of the above. The key body to maintain the consistency of the data format of the document.

$data = $request->all();
$es_arr['index'] = "my_index";
$es_arr['type'] = "my_type";
$es_arr['id'] = "my_id";
$es_arr['body'] = ['name'=>"lisi","sex"=>'男',"age"=>22,"works"=>"IT"];

$client = ClientBuilder::create()->build();
is_es = $ Client- $> index ($ Client); 
5, to obtain a document
$es_arr['index'] = "my_index";
$es_arr['type'] = "my_type";
$es_arr['id'] = "my_id";

$client = ClientBuilder::create()->build();
$ es = $ client-> get ( $ es_array); 
response data contains metadata (e.g., index, type, etc.) and _source properties, the original document data which is sent to you in Elasticsearch
The Array 
(
[the _index] => my_index
[_type] => my_type
[the _id] => my_id
[_VERSION] =>. 3
[found] =>. 1
[_Source] => the Array
(
[name] => Lisi
[Sex] => M
[Age] => 22
[Works] => IT
)

)
6, a search document
search is a feature elasticsearch, so we try to perform a search. We are ready to use Match query:
$es_data["index"] = "my_index";
$es_data['type'] = "my_type";
$es_data['body']=[
"query"=>[
"match"=>[
'name'=>'lisi'
]
]
];
$client = ClientBuilder::create()->build();
$es = $client->search($es_data);
The response data in the previous example of the response data is different, here are some of the metadata (such as took, time_out, etc.) and a array of hits, which represents your search results. The interior also has a hits array of hits, hits include specific internal search results:
Array
(
    [took] => 16 [timed_out] => [_shards] => Array ( [total] => 5 [successful] => 5 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 1 [max_score] => 0.2876821 [hits] => Array ( [0] => Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_score] => 0.2876821 [_source] => Array ( [testField] => abc ) ) ) ) )
7, delete a document
$es_data["index"] = "my_index";
$es_data['type'] = "my_type";
$es_data['id'] = "my_id";
$client = ClientBuilder::create()->build();
$es = $client->delete($es_data);
Delete the document syntax and obtain the document syntax is the same. The only difference is the delete method instead of the get method. 
8, remove an index
due to the dynamic characteristics elasticsearch, the first document we created will automatically create an index, but also the settings inside the parameters set to the default parameters.
$es_data['index'] = "my_index";
$client = ClientBuilder::create()->build();
$es = $client->indices()->delete($es_data);
9, create an index 
Add an index, while customize settings:
$es_data['index'] = "my_index";
$es_data['body']=[
"settings"=>[
"number_of_shards"=>2,
"number_of_replicas"=>0
]
];
$client = ClientBuilder::create()->build();
$es = $client->indices()->create($es_data);
This section Conclusion: 
all core method (index, search, access, etc.) under the $ client objects are available. Index management and cluster management are at $ client-> indices () and $ client-> cluster () in.

Guess you like

Origin www.cnblogs.com/shangfz/p/11528320.html