Highlighting does not work in Elasticsearch and PHP
- The value of
pre_tags
andpost_tags
should be an array (however if you don't want to change theem
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']
.