2

I am trying to add search function to my website, using Elasticsearch + Laravel

The package I use can be found here: https://github.com/cviebrock/laravel-elasticsearch

So far I am able to get everything work except highlight. Here is my PHP code:

            $params = [
            'index' => 'my_index',
            'type' => 'web_page',
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [
                            [
                                'match' => [ 'combined' => $keywords ]
                            ],
                            [
                                'match' => [ 'status' => 1 ],
                            ]
                        ]
                    ]
                ],
                'highlight' => [
                    'pre_tags'  => '<em>',
                    'post_tags' => '</em>',
                    'fields'    => [
                       'combined' => new \stdClass()
                    ]
                ],
            ]
        ];

        try {
            $results = Elasticsearch::search($params);
        } catch (Exception $e) {
            var_dump($e->getMessage());
        }

        dd($results);

The results I get is like the following:

array:4 [▼
  "took" => 250
  "timed_out" => false
  "_shards" => array:3 [▶]
  "hits" => array:3 [▼
  "total" => 2
  "max_score" => 0.8117509
  "hits" => array:2 [▼
      0 => array:5 [▶]
      1 => array:5 [▼
        "_index" => "my_index"
        "_type" => "web_page"
        "_id" => "wp_2"
        "_score" => 0.4709723
        "_source" => array:7 [▶]
      ]
    ]
  ]
]

As you may see, I am missing 'highlight' field which is supposed to come after "_source".

I did follow instructions described in the following page:

https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_dealing_with_json_arrays_and_objects_in_php.html

Checked several related questions here, but still cannot figure out what I did wrong.

3
  • I have same problem on elastic 2.3.5 and PHP client 2.2.1. All those answers about empty objects in PHP are not helpful. It must be something else. Highlight broke when upgraded Elastic from 1.6 to 2.3 Commented Oct 11, 2016 at 20:24
  • Try to set "store" => true in your mapping. This solved my problem. stackoverflow.com/questions/38417170/… Commented Oct 13, 2016 at 2:59
  • It was not the case for me, I shared my solution in answer. Wasted so many hours on this... Commented Oct 13, 2016 at 7:55

2 Answers 2

2

Here is solution to missing highlight in my case

All answers about adding store => true to mapping didn't help, including restarting elasticsearch etc. in the end I'm running highlight correctly without adding it at all.

Elastic Search 2.3.5 Elastic PHP library 2.x

In my case it was conflict between ['body']['query']['match']['_all'] and highlight on specific fields

$params['body']['highlight']['fields']['headline'] = (object) [];
$params['body']['highlight']['fields']['description'] = (object) [];

Started to work after adding

$params['body']['highlight']["require_field_match"] = false;

Sharing code snippet.

$params = [];
$params['index'] = $this->index;
$params['type'] = $this->type;

$perPage = 20;
$offset = $perPage * ($page - 1);

$params['size'] = $perPage;
$params['from'] = $offset;

$params['body']['query']['match']['_all'] = [
    'query' => $searchQuery->getValue(),
    'fuzziness' => 'AUTO'
];

$params['body']['filter']['bool']['must'] = [];
$params['body']['filter']['bool']['must'][] = [
    'term' => ['verified' => true]
];

$params['body']['highlight']['fields']['headline'] = (object) [];
$params['body']['highlight']['fields']['description'] = (object) [];
$params['body']['highlight']["require_field_match"] = false;

$response = $this->elasticClient->search($params);

I hope this help someone

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

Comments

0

Try

'highlight' => [                      
       'fields'    => [
             'combined' => (object) []
        ]
 ]

Check out this tread: https://github.com/elastic/elasticsearch-php/issues/281

1 Comment

Thanks for posting answer, but yours is essentially equal to "'combined' => new \stdClass()".

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.