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

 
相關文章
相關標籤/搜索