Highlighting does not work in Elasticsearch and PHP Highlighting does not work in Elasticsearch and PHP elasticsearch elasticsearch

Highlighting does not work in Elasticsearch and PHP


From the manual:

  1. 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).
  2. 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

  1. 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).
  2. The pre/post_tags can also be strings (and not array).
  3. 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'].