Elasticsearch-php之高亮搜索

在PHP中,调用elasticsearch进行搜索时,想让结果高亮显示,添加参数highlight,在其下的field中,添加须要高亮的字段,以前写成'content => []',没有返回高亮结果。
后来经过搜索查询相关问答网站,才知道须要将类型数组转换为对象,如'content' => new \stdClass()php

public function search()
    {
        $hosts = ['127.0.0.1:9200'];
        $clientBuilder = ClientBuilder::create();   // Instantiate a new ClientBuilder
        $clientBuilder->setHosts($hosts);           // Set the hosts
        $client = $clientBuilder->build();          // Build the client object

        //Set search params
        $params = [
            'index' => 'index',
            'type'  => 'fulltext',
            'body'  => [
                'query' => [
                    'term' => [
                        'content' => '中国'
                    ]
                ],
                'highlight' => [
                    'pre_tags' => ["<em>"],
                    'post_tags' => ["</em>"],
                    'fields' => [
                        "content" => new \stdClass()
                    ]
                ]
            ]
        ];
        $response = $client->search($params);
        print_r($response);
    }

返回的结果以下,查询关键字“中国”,返回的结果中,在highlight中,标签<em>高亮包含了“中国”:laravel

Array
(
    [took] => 2
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 2
            [max_score] => 1.5
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => index
                            [_type] => fulltext
                            [_id] => 4
                            [_score] => 1.5
                            [_source] => Array
                                (
                                    [content] => 中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首
                                )

                            [highlight] => Array
                                (
                                    [content] => Array
                                        (
                                            [0] =>
<em>中国</em>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => index
                            [_type] => fulltext
                            [_id] => 3
                            [_score] => 0.53699243
                            [_source] => Array
                                (
                                    [content] => 中韩渔警冲突调查:韩警平均天天扣1艘中国渔船
                                )

                            [highlight] => Array
                                (
                                    [content] => Array
                                        (
                                            [0] => 中韩渔警冲突调查:韩警平均天天扣1艘
<em>中国</em>渔船
                                        )

                                )

                        )

                )

        )

)

参考连接地址:用elasticsearch-php laravel为何不能返回高亮数据?segmentfault

 
相关文章
相关标签/搜索