Jac*_*ian 11 php elasticsearch elasticsearch-5
我刚刚在Windows机器上下载并安装了最新版本的Elasticsearch.我做了我的第一次搜索查询,一切似乎都正常.然而.当我试图突出搜索结果时,我失败了.所以,这就是我的查询的样子:
$params = [
'index' => 'test_index',
'type' => 'test_index_type',
'body' => [
'query' => [
'bool' => [
'should' => [ 'match' => [ 'field1' => '23' ] ]
]
],
'highlight' => [
'pre_tags' => "<em>",
'post_tags' => "</em>",
'fields' => (object)Array('field1' => new stdClass),
'require_field_match' => false
]
]
]
$res = $client->search($params);
Run Code Online (Sandbox Code Playgroud)
总的来说,查询本身效果很好 - 结果被过滤了.在控制台中我看到,所有文档确实在其field1字段中包含"23"值.但是,这些标签 - <em></em>根本不会添加到结果中.我所看到的只是field1像" some text 23"," 23 another text"中的原始值.这不是我期望看到的 - " some text <em>23</em>"," <em>23</em> another text".那么,这有什么问题,我该如何解决?
Dek*_*kel 14
从手册:
pre_tags和post_tags应该是一个数组(但是如果你不想改变em你可以忽略他们的标签,他们已经设置为默认值).fields值应为一个数组,关键是字段名称和值是与场选项的阵列.试试这个修复:
$params = [
'index' => 'test_index',
'type' => 'test_index_type',
'body' => [
'query' => [
'bool' => [
'should' => [ 'match' => [ 'field1' => '23' ] ]
]
],
'highlight' => [
// 'pre_tags' => ["<em>"], // not required
// 'post_tags' => ["</em>"], // not required
'fields' => [
'field1' => new \stdClass()
],
'require_field_match' => false
]
]
];
$res = $client->search($params);
var_dump($res['hits']['hits'][0]['highlight']);
Run Code Online (Sandbox Code Playgroud)
fields数组中字段的值应该是一个对象(这是一个要求,与其他选项不完全相同).pre/post_tags也可以是字符串(而不是数组).$res['hits']['hits'][0]['highlight']需要注意的重要一点是,高分辨率的结果会进入
highlight数组 -$res['hits']['hits'][0]['highlight'].