在Elasticsearch结果中缺少高亮字段,PHP

时间:2016-07-16 01:19:08

标签: php elasticsearch

我正在尝试使用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' => new \stdClass()
                    ]
                ],
            ]
        ];

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

        dd($results);

我得到的结果如下:

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 [▶]
      ]
    ]
  ]
]

正如您所看到的,我错过了“突出”字段,它应该出现在“_source”之后。

我确实遵循了以下页面中描述的说明:

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

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

2 个答案:

答案 0 :(得分:2)

这是我案例中缺少亮点的解决方案

关于将store => true添加到映射的所有答案都无济于事,包括重新启动弹性搜索等,最后我正在正确运行高亮显示而不添加它。

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

就我而言,['body']['query']['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' => 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);

我希望这有助于某人

答案 1 :(得分:0)

尝试

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

看看这个步骤:https://github.com/elastic/elasticsearch-php/issues/281