1.引入包 composer require elasticsearch/elasticsearch
代码语言:javascript复制2.DEMO参考
代码语言:javascript复制<?php
require_once './vendor/autoload.php';
use ElasticsearchClientBuilder;
$hosts = [
[
'host' => '192.168.56.201',
'port' => '9200',
'scheme' => 'http',
//'user' => 'username',
//'password' => 'password'
],
];
try {
$client = ClientBuilder::create()->setHosts($hosts)->build();
//创建index并设置mapping
/*$params = [
'index' => 'demo', //索引名(相当于关系型mysql的数据库)
'body' => [
'settings' => [
'number_of_shards' => 1, //分片数
'number_of_replicas' => 1, //副本分骗术
],
'mappings' => [
'm_type' => [
'_all' => [
'enabled' => 'false'
],
'_routing' => [
'required' => 'true'
],
'properties' => [ //文档类型设置(相当于mysql的数据类型)
'name' => [
'type' => 'string',
'store' => 'true'
],
'age' => [
'type' => 'integer'
]
]
]
]
]
];
$response = $client->indices()->create($params);
print_r($response);//Array ( [acknowledged] => 1 [shards_acknowledged] => 1 )
*/
// 索引操作
// $params = [
// 'index' => 'demo',
// ];
// $response = $client->indices()->delete($params); //删除索引
// print_r($response);//Array ( [acknowledged] => 1 )
//$response = $client->indices()->getSettings($params);//获取索引设置信息
//print_r($response);//Array ( [demo] => Array ( [settings] => Array ( [index] => Array ( [creation_date] => 1502246895144 [number_of_shards] => 5 [number_of_replicas] => 5 [uuid] => tW3DB-9FRQC2W-bwXl0fbg [version] => Array ( [created] => 5040199 ) [provided_name] => demo ) ) ) )
//$response = $client->indices()->exists($params); //检测索引是否存在
//print_r($response);//1
//$response = $client->indices()->getMapping($params); //获取索引的mapping信息
//print_r($response);//Array ( [demo] => Array ( [mappings] => Array ( [m_type] => Array ( [_all] => Array ( [enabled] => ) [_routing] => Array ( [required] => 1 ) [properties] => Array ( [age] => Array ( [type] => integer ) [name] => Array ( [type] => text [store] => 1 ) ) ) ) ) )
//添加文档
// $params = [
// 'index' => 'demo',
// 'type' => 'm_type',
// 'id' => '2',
// 'body' => [
// 'name' => 'demo',
// 'age' => 150
// ],
// 'routing' => '/demo/m_type'
// ];
// $response = $client->index($params);
// print_r($response);
//删除文档
// $params = [
// 'index' => 'demo',
// 'type' => 'm_type',
// 'id' => '2',
// 'routing' => '/demo/m_type'
// ];
// $response = $client->delete($params);
// print_r($response);//Array ( [found] => 1 [_index] => demo [_type] => m_type [_id] => 1 [_version] => 2 [result] => deleted [_shards] => Array ( [total] => 2 [successful] => 2 [failed] => 0 ) )
//修改文档
// $params = [
// 'index' => 'demo',
// 'type' => 'm_type',
// 'id' => '2',
// 'body' => [
// 'doc' => [
// 'name' => 'demo22',
// 'age' => 130
// ]
// ],
// 'routing' => '/demo/m_type'
// ];
// $response = $client->update($params);
// print_r($response);
//获取文档并指定字段
// $params = [
// 'index' => 'demo',
// 'type' => 'm_type',
// 'id' => '2',
// '_source' => [
// 'age'
// ],
// 'routing' => '/demo/m_type'
// ];
// $response = $client->get($params);
// print_r($response);
// $response = $client->getSource($params);
// print_r($response);
//高级搜索
// $params = [
// 'index' => 'demo',
// 'type' => 'm_type',
// 'routing' => '/demo/m_type',
// 'body' => [
// 'query' => [
// 'match' => [
// 'age' => 130,
// ]
// ]
// ]
// ];
// $response = $client->search($params);
// print_r($response);
} catch (Exception $e) {
echo $e->getMessage();
}
备注:如果提示routing_missing_exception则参数当中需要传递routing
参考:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html