如下es基於6.4php
一、在 composer.json 文件中引入 elasticsearch-php:json
{ "require":{ "elasticsearch/elasticsearch":"~6.0", "monolog/monolog": "~1.0" } }
二、用 composer 安裝客戶端:瀏覽器
curl -s http://getcomposer.org/installer | php php composer.phar install --no-dev
一、建立一個test.php文件,內容以下app
<?php require 'vendor/autoload.php'; use Elasticsearch\ClientBuilder; $hosts = [ '192.168.16.241:9200', // IP + Port '192.168.16.241', // Just IP 'localhost:9200', // Domain + Port 'localhost', // Just Domain 'http://localhost', // SSL to localhost 'https://192.168.16.241:9200' // SSL to IP + Port ]; $client = ClientBuilder::create()->setHosts($hosts)->build(); // Instantiate a new ClientBuilder // Set the hosts $params = [ 'index' => 'test_data', 'type' => 'users', 'id' => 100027, 'client' => [ 'ignore' => 404 ] ]; var_dump( $client->get($params));
二、瀏覽器訪問test.php,結果以下(前提是你的es已經有數據)composer
3、基本操做less
一、建立索引curl
$params = [ 'index' => 'test_index' ]; // Create the index print_r($client->indices()->create($params));
二、建立索引(指定模板)elasticsearch
$params = [ 'index' => 'test_index', 'body' => [ 'settings' => [ 'number_of_shards' => 5, 'number_of_replicas' => 2 ], 'mappings' => [ 'test_type' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'name' => [ 'type' => 'text', 'analyzer' => 'ik_max_word' ], 'age' => [ 'type' => 'integer' ] ] ] ] ] ]; // Create the index with mappings and settings now print_r($client->indices()->create($params));
三、刪除索引、ui
$params = ['index' => 'test_index']; print_r($client->indices()->delete($params));
四、更改索引的配置參數:url
$params = [ 'index' => 'test_index', 'body' => [ 'settings' => [ 'number_of_replicas' => 0, 'refresh_interval' => -1 ] ] ]; print_r($client->indices()->putSettings($params));
五、獲取一個或多個索引的當前配置參數
$params = [ 'index' => [ 'test_index', 'test_data' ] ]; print_r($client->indices()->getSettings($params));
六、更改或增長一個索引的映射
$params = [ 'index' => 'test_index', 'type' => 'test_type', 'body' => [ 'test_type' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'name' => [ 'type' => 'text', 'analyzer' => 'ik_max_word' ], 'age' => [ 'type' => 'integer' ], 'createtime' => [ 'type' => 'date' //加了一個時間 ] ] ] ] ]; // Update the index mapping print_r($client->indices()->putMapping($params));
七、返回索引和類型的映射細節
$response = $client->indices()->getMapping(); // Get mappings for all types in 'my_index' $params = ['index' => 'my_index']; $response = $client->indices()->getMapping($params); // Get mappings for all types of 'my_type', regardless of index $params = ['type' => 'my_type' ]; $response = $client->indices()->getMapping($params); // Get mapping 'my_type' in 'my_index' $params = [ 'index' => 'my_index' 'type' => 'my_type' ]; $response = $client->indices()->getMapping($params); // Get mappings for two indexes $params = [ 'index' => [ 'my_index', 'my_index2' ] ]; $response = $client->indices()->getMapping($params);
八、索引一個文檔(提供id,則會更新對應id的記錄。若沒有提供,則會生成一條文檔)
$params = [ 'index' => 'test_data', 'type' => 'users', 'id' => '100027', 'body' => [ 'nickname' => 'update222'] ]; // Document will be indexed to my_index/my_type/my_id print_r($client->index($params));
九、獲取文檔
$params = [ 'index' => 'test_data', 'type' => 'users', 'id' => '100027' ]; // Get doc at /my_index/my_type/my_id print_r($client->get($params));
十、更新文檔 (doc指定要更新的字段內容)
$params = [ 'index' => 'test_data', 'type' => 'users', 'id' => '100027', 'body' => [ 'doc' => [ 'nickname' => 'abc', 'mobile' => '13800138000' ] ] ]; // Update doc at /my_index/my_type/my_id print_r($client->update($params));
十一、執行一個腳本進行更新,對某個字段的數據進行拼接或自增
$params = [ "index" => "test_data", "type" => "users", "id" => "100027", "body" => [ "script" => "ctx._source.nickname += 'hahh'" ] ]; print_r($client->update($params));
十二、刪除文檔
$params = [ 'index' => 'test_data', 'type' => 'users', 'id' => '100027' ]; // Delete doc at /my_index/my_type/my_id print_r($client->delete($params));
1三、搜索內容
$json = '{ "query" : { "match" : { "id" : "100073" } } }'; $params = [ 'index' => 'test_data', 'type' => 'users', 'body' => $json ]; print_r($client->search($params)); $params = [ 'index' => 'test_data', 'type' => 'users', 'body' => [ 'query' => [ 'bool' => [ 'should' => [ [ 'match' => [ 'nickname' => [ 'query' => 'user440032', 'boost' => 3, // 權重大 ]]] ], ], ], 'sort' => ['id'=>['order'=>'desc']] //排序 分頁 , 'from' => 0, 'size' => 10 ] ]; print_r($client->search($params));