我正在尝试使用Elasticsearch + Laravel向我的网站添加搜索功能
我使用的软件包可以在这里找到:
https://github.com/cviebrock/laravel-elasticsearch
到目前为止,除了突出显示之外,我都能使所有功能正常运行。这是我的PHP代码:
$params = [
'index' => 'my_index',
'type' => 'web_page',
'body' => [
'query' => [
'bool' => [
'must' => [
[
'match' => [ 'combined' => $keywords ]
],
[
'match' => [ 'status' => 1 ],
]
]
]
],
'highlight' => [
'pre_tags' => '<em>',
'post_tags' => '</em>',
'fields' => [
'combined' => new \stdClass()
]
],
]
];
try {
$results = Elasticsearch::search($params);
} catch (Exception $e) {
var_dump($e->getMessage());
}
dd($results);
我得到的结果如下所示:
array:4 [▼
"took" => 250
"timed_out" => false
"_shards" => array:3 [▶]
"hits" => array:3 [▼
"total" => 2
"max_score" => 0.8117509
"hits" => array:2 [▼
0 => array:5 [▶]
1 => array:5 [▼
"_index" => "my_index"
"_type" => "web_page"
"_id" => "wp_2"
"_score" => 0.4709723
"_source" => array:7 [▶]
]
]
]
]
如您所见,我缺少“突出显示”字段,该字段应该在“_source”之后。
我确实遵循了以下页面中描述的说明:
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_dealing_with_json_arrays_and_objects_in_php.html
在这里检查了几个相关的问题,但仍然无法弄清楚我做错了什么。
最佳答案
这是我案例中缺少突出显示的解决方案
关于将store => true
添加到映射的所有答案都无济于事,包括重新启动elasticsearch等。最后,我正确运行了高亮显示,而根本没有添加它。
flex 搜寻2.3.5
flex PHP库2.x
就我而言,这是['body']['query']['match']['_all']
和突出显示特定字段之间的冲突
$params['body']['highlight']['fields']['headline'] = (object) [];
$params['body']['highlight']['fields']['description'] = (object) [];
添加后开始工作
$params['body']['highlight']["require_field_match"] = false;
共享代码段。
$params = [];
$params['index'] = $this->index;
$params['type'] = $this->type;
$perPage = 20;
$offset = $perPage * ($page - 1);
$params['size'] = $perPage;
$params['from'] = $offset;
$params['body']['query']['match']['_all'] = [
'query' => $searchQuery->getValue(),
'fuzziness' => 'AUTO'
];
$params['body']['filter']['bool']['must'] = [];
$params['body']['filter']['bool']['must'][] = [
'term' => ['verified' => true]
];
$params['body']['highlight']['fields']['headline'] = (object) [];
$params['body']['highlight']['fields']['description'] = (object) [];
$params['body']['highlight']["require_field_match"] = false;
$response = $this->elasticClient->search($params);
我希望这可以帮助某人
关于php - PHP的Elasticsearch结果中缺少突出显示的字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38406841/