13

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?

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • 4
    It looks like `pre_tags` and `post_tags` may expect an array...can you try wrapping both in `em` strings in `[]`? – Sam Nov 01 '16 at 20:18
  • Did you try running that query outside PHP? (Like using the Sense plugin in Kibana or a simple `curl` command.) Does the highlighting work? – Andrei Stefan Nov 01 '16 at 20:19
  • 1
    @Andrei Stefan. No, I have not tried that yet. – Jacobian Nov 02 '16 at 05:59
  • @Sam. It does not help. I checked it. I get filtered results, but without any highlighting – Jacobian Nov 02 '16 at 09:15
  • @Andrei Stefan. I tried curl, but it works really terrible on Windows. I'm not able to make requests that contain -d flag and some json body - on Windows in this case I'm getting some parse errors. – Jacobian Nov 02 '16 at 09:36

1 Answers1

19

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'].

Dekel
  • 60,707
  • 10
  • 101
  • 129
  • Thanks! I will check it in a minute. – Jacobian Nov 02 '16 at 06:00
  • Unfortunatelly, it is not working. Now I'm getting this error message `[highlight_field] Expected START_OBJECT but was: START_ARRAY`. So, I think, there is some trouble with `'field1' => []` in your example. By the way, in docs it looks like so `"fields" : { "content" : {} }`. I see no arrays here. – Jacobian Nov 02 '16 at 09:10
  • While, this structure is working - `'fields' => ['field1' => (object)[]]`. But still, neither default pre_tags and post_tags (when I ignore them), nor implicit `'pre_tags' => [''], 'post_tags' => ['']` work. – Jacobian Nov 02 '16 at 09:14
  • @Jacobian, check the update please. Which version of the php library you use? – Dekel Nov 02 '16 at 12:11
  • I installed the library with composer. The file looks like `{"require": { "elasticsearch/elasticsearch": "~5.0" }}` – Jacobian Nov 02 '16 at 12:38
  • Seems right. Did you check `$res['hits']['hits'][0]['highlight']` for the highlighted results? – Dekel Nov 02 '16 at 12:42
  • I've just checked! Now I see, that it is what I need! Thanks! I did not know that highlighted results go separately from _source. But still thanks! – Jacobian Nov 02 '16 at 12:45
  • Added a clarification regarding this :) – Dekel Nov 02 '16 at 12:51