在 elasticsearch-php 中,几乎一切操作都是用关联数组来配置。REST 路径(endpoint)、文档和可选参数都是用关联数组来配置。
为了索引一个文档,我们要指定4部分信息:index,type,id 和一个 body。构建一个键值对的关联数组就可以完成上面的内容。body 的键值对格式与文档的数据保持一致性。(译者注:如 [“testField” ⇒ “abc”] 在文档中则为 {“testField” : “abc”}):
在 composer.json 文件中引入 elasticsearch-php:
{
"require":
"elasticsearch/elasticsearch": "~6.0"
}
composer update也可指定更新
composer update elasticsearch/elasticsearch
在项目中引入自动加载文件(如果还没引入),并且实例化一个客户端 (引入就不需要这步)
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
//如果更改端口号则需要 setHosts 更改端口号 不写默认9200
//$client= ClientBuilder::create()->setHosts(['127.0.0.1:9400'])->build();
$client = ClientBuilder::create()->build();
创建索引
use Elasticsearch\ClientBuilder;
//简单索引
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index'
];
// Create the index
$response = $client->indices()->create($params);
//指定分词
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
'name' => [
'type' => 'text',
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_max_word"
],
'desc' => [
'type' => 'text',
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_max_word"
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
添加
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
'type' => '_doc',
//'id' => '1',
'body' => ['name' => "iphone7ಋ",
'desc' => "iphone7ಋ"]
];
$response = $client->index($params);
print_r($response);
搜索
单个id搜索
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
'type' => '_doc',
'id' => '1'
];
$response = $client->get($params);
print_r($response);
Match搜索
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
//'type' => '_doc',
'body' => [
'query' => [
'match' => [
'name' => '手机'
]
],
'highlight' => [
'pre_tags' => ["<em>"],
'post_tags' => ["</em>"],
'fields' => [
"name" => new \stdClass()
]
]
]
];
//偏移
$params["size"] = 1;
$params["from"] = 1;
$results = $client->search($params);
print_r($results);
Bool查询
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'name' => '中华' ] ],
[ 'match' => [ 'name' => '人民' ] ],
]
]
]
]
];
$results = $client->search($params);
print_r($results);
复杂查询
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'filter' => [
'term' => [ 'age' => 12 ]
],
'should' => [
'match' => [ 'name' => '中国'
]
]
]
]
];
$results = $client->search($params);
print_r($results);
高亮显示
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
//'type' => '_doc',
'body' => [
'query' => [
'match' => [
'name' => '中华人民'
]
],
'highlight' => [
'pre_tags' => ["<em>"],
'post_tags' => ["</em>"],
'fields' => [
"name" => new \stdClass()
]
]
]
];
$results = $client->search($params);
print_r($results);
删除索引
$client = ClientBuilder::create()->build();
$params = ['index' => 'goods'];
$response = $client->indices()->delete($params)
更新索引
$client = ClientBuilder::create()->build();
$params = [
'index' => 'goods',
'type' => '_doc',
'id' => 'srgiyW0BpEEPDQFsRhIK',
'body' => [
'doc' => [
'name' => 'abc'
]
]
];
$response = $client->update($params);
print_r($response);exit;