Highlighting does not work in Elasticsearch and PHP
elasticsearch, elasticsearch-5, php
Solution
From the manual:
- The value of `pre_tags` and `post_tags` should be an array (however if you don't want to change the `em` tags you can ignore them, they already set as default).
- The `fields` value should be an array, key is the field name and the value is an array with the field options.
Try this fix:
$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']);
update
- Did a double check, the value of the field in the `fields` array should be an object (which is a requirement, not exactly the same as other options).
- The `pre/post_tags` can also be strings (and not array).
- Did you check the correct response? `$res['hits']['hits'][0]['highlight']`
The important thing to notice is that the highligted results goes into the `highlight` array - `$res['hits']['hits'][0]['highlight']`.
Problem
I've just downloaded and installed the last version of Elasticsearch on my Windows machine. I did my first search queries and everything seemed to work ok. However. when I try to highlight the search results, I fail. So, this is how my query looks like: ``` $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); ``` On the whole the query itself works nice - the results are filtered. In the console I see, that all documents indeed contain "23" value in their `field1` field. However, these tags - `<em></em>` are simply not added to the result. What I see is just the raw value in `field1` like "`some text 23`", "`23 another text`". It is not what I expect to see - "`some text <em>23</em>`", "`<em>23</em> another text`". So, what is wrong with that and how can I fix it?