我一直在使用Elasticsearch 7.6和PHP客户端API进行所有操作。我已经创建了如下的弹性搜索索引设置和映射
$params = [
'index' => 'elasticindex',
'body' => [
'settings' => [
"number_of_shards" => 1,
"number_of_replicas" => 0,
"index.queries.cache.enabled" => false,
"index.soft_deletes.enabled" => false,
"index.requests.cache.enable" => false,
"index.refresh_interval" => -1
],
'mappings' => [
'_source' => [
"enabled" => false
],
'properties' => [
"text" => [
"type" => "text",
"index_options" => "docs"
]
]
]
]
];
我能够使用以下代码索引文档
$params = array();
$params['index'] = 'elasticindex';
for($i = 1; $i <=2; $i++) {
$params['id'] = $i;
$params['body']['text'] = 'apple';
$responses = $client->index($params);
}
但是当我使用以下搜索查询时
$params = [
'index' => 'elasticindex',
'body' => [
'query' => [
'match' => [
"text" => "apple"
]
]
]
];
$results = $client->search($params);
我得到如下的空结果
Array
(
[took] => 3
[timed_out] =>
[_shards] => Array
(
[total] => 1
[successful] => 1
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => Array
(
[value] => 0
[relation] => eq
)
[max_score] =>
[hits] => Array
(
)
)
)
在不创建静态索引模板的情况下,如果我尝试索引,弹性搜索动态映射效果良好,并且我正在获得结果。
目标是,我希望弹性搜索在其反向索引中只索引文档id,而不是位置或偏移量,并且我希望只检索匹配的文档id作为结果。非常感谢您的帮助。提前感谢!
由于文档是由get
处理程序而不是查询处理程序返回的,因此在对文档进行索引后,索引没有正确刷新。
正如您自己所指出的,在您的配置中,您设置了:
"index.refresh_interval" => -1
这意味着索引不会自动刷新。很少需要更改刷新间隔,除非在吞吐量非常高的情况下或需要特定行为的情况下。
试着这样索引文档。
$client = ElasticsearchClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'elasticindex',
'type' => 'documents',
'id' => '1',
'body' => ['text' => 'apple']
];
$response = $client->index($params);
print_r($response);
注意:id可以根据需要动态定义,否则id将由elastic自动设置。
并尝试通过搜索查询获取文档。
{
"query": {
"bool": {
"must": {
"term": {
"text": "apple"
}
}
}
}
}
如果要对此密钥进行全文搜索,请将此密钥属性设置为
"properties": {
"name": {
"type": "text"
}
}