从弹性搜索结果,PHP中突出显示字段缺失 [英] highlight field missing from Elasticsearch results, PHP

查看:188
本文介绍了从弹性搜索结果,PHP中突出显示字段缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加搜索功能到我的网站,使用Elasticsearch + Laravel



我使用的包可以在这里找到:
https://github.com/cviebrock/laravel-elasticsearch



到目前为止,除了亮点之外,我能够完成一切工作。这是我的PHP代码:

  $ 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'=>新的\stdClass()
]
],
]
];

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

dd($ results);

我得到的结果如下:

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

如您所见,我错过了_source之后的亮点字段。



我遵循以下描述的说明页面:



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



在这里检查了几个相关的问题,但仍然无法弄清楚我做错了什么。

解决方案

我的案例



所有关于添加 store =>真正的映射没有帮助,包括重新启动弹性搜索等,最后我正好运行高亮显示,而不添加它。



弹性搜索2.3.5
弹性PHP库2.x



在我的情况下,这是 ['body'] ['查询'] ['match'] ['_ all'] 并突出显示特定字段

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

添加

开始工作

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

共享代码段。

  $ 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'=>真]
];

$ 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 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.

解决方案

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

这篇关于从弹性搜索结果,PHP中突出显示字段缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆