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?

5
  • 4
    It looks like pre_tags and post_tags may expect an array...can you try wrapping both in em strings in []? Commented Nov 1, 2016 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? Commented Nov 1, 2016 at 20:19
  • 1
    @Andrei Stefan. No, I have not tried that yet. Commented Nov 2, 2016 at 5:59
  • @Sam. It does not help. I checked it. I get filtered results, but without any highlighting Commented Nov 2, 2016 at 9: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. Commented Nov 2, 2016 at 9:36

1 Answer 1

19
+100

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks! I will check it in a minute.
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.
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' => ['<em>'], 'post_tags' => ['</em>'] work.
@Jacobian, check the update please. Which version of the php library you use?
I installed the library with composer. The file looks like {"require": { "elasticsearch/elasticsearch": "~5.0" }}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.