php 使用ES
Download Elasticsearch | Elastic
<?phprequire 'vendor/autoload.php';use Elasticsearch\ClientBuilder;$client = ClientBuilder::create()->build();# 索引一个文档
# Version 7.11
$params = ['index' => 'my_index','id' => 'my_id','body' => ['testField' => 'abc']
];$response = $client->index($params);
print_r($response);# 获取一个文档
# Version 7.11
$params = ['index' => 'my_index','id' => 'my_id'
];$response = $client->get($params);
print_r($response);# 搜索一个文档
# Version 7.11
$params = ['index' => 'my_index','type' => 'my_type','body' => ['query' => ['match' => ['testField' => 'abc']]]
];$response = $client->search($params);
print_r($response);
注意:
testindex 为 ElasticSearch 的索引,文档 ID 为 5,在插入文档时,报错:Rejecting mapping update to [testindex] as the final mapping would have more than 1 type: [_doc, 5]。测试环境使用的是 7.1.1 版本的 ElasticSearch,经查阅,原因是 Elasticsearch 在 7.x 版本中已经去除 type 的概念。
type 的版本迭代:
5.x 及以前版本,一个 index 有一个或者多个 type
6.x 版本,一个 index 只有一个 type
7.x 版本移除了 type,type 相关的所有内容全部变成 Deprecated,为了兼容升级和过渡,所有的 7.x 版本 es 数据写入后 type 字段都默认被置为 “_doc”
8.x 版本完全废弃 type