首頁  >  文章  >  後端開發  >  elasticsearch - 哪位朋友知道es-php或mmanos/laravel-search的查詢結果高亮問題

elasticsearch - 哪位朋友知道es-php或mmanos/laravel-search的查詢結果高亮問題

WBOY
WBOY原創
2016-08-04 09:21:421384瀏覽

以下程式碼是laravel+elastisearch-php(https://github.com/elastic/elasticsearch-php)實現的,但是不能返回高亮內容,網路上看了https://segmentfault.com/a/1190000003010186 下面有一個例子提到高亮,但mmanos的不太會用,例如如何找指定索引下指定類型的某個查詢?
不亂以上哪一種方式,哪位朋友能指點一二,感謝! ! !

<code>        $params=[
            'index' => 'n_index',
            'type' => 'n_type',
            'body' => [
                'query' => [
                    'match' => ['ntitle' => '要查询的词']
                ],
                'highlight'=>[
                    'pre_tags'=>['<tag1>', '<tag2>'],
                    'post_tags'=>['</tag1>', '</tag2>'],
                    'fields'=>[
                        'ntitle'=>[]
                    ]
                ]
            ]
        ];

        $response = $this->client->search($params);
    
        echo "<pre class="brush:php;toolbar:false">";
        var_dump($response);
        echo "
";

以上程式碼可以查出以下結果

<code>array(4) {
  ["took"]=>
  int(6)
  ["timed_out"]=>
  bool(false)
  ["_shards"]=>
  array(3) {
    ["total"]=>
    int(5)
    ["successful"]=>
    int(5)
    ["failed"]=>
    int(0)
  }
  ["hits"]=>
  array(3) {
    ["total"]=>
    int(3)
    ["max_score"]=>
    float(1.8223838)
    ["hits"]=>
    array(3) {
      [0]=>
      array(5) {
        ["_index"]=>
        string(7) "n_index"
        ["_type"]=>
        string(6) "n_type"
        ["_id"]=>
        string(1) "6"
        ["_score"]=>
        float(1.8223838)
        ["_source"]=>
        array(1) {
          ["ntitle"]=>
          string(35) "ntitle内容2"
        }
      }
      [1]=>
      array(5) {
        ["_index"]=>
        string(7) "n_index"
        ["_type"]=>
        string(6) "n_type"
        ["_id"]=>
        string(1) "7"
        ["_score"]=>
        float(1.2087858)
        ["_source"]=>
        array(1) {
          ["ntitle"]=>
          string(35) "ntitle内容1"
        }
      }
      [2]=>
      array(5) {
        ["_index"]=>
        string(7) "n_index"
        ["_type"]=>
        string(6) "n_type"
        ["_id"]=>
        string(1) "5"
        ["_score"]=>
        float(0.23033649)
        ["_source"]=>
        array(1) {
          ["ntitle"]=>
          string(35) "ntitle内容3"
        }
      }
    }
  }
}</code>

沒有回到高亮部分,

回覆內容:

以下程式碼是laravel+elastisearch-php(https://github.com/elastic/elasticsearch-php)實現的,但是不能返回高亮內容,網路上看了https://segmentfault.com/a/1190000003010186 下面有一個例子提到高亮,但mmanos的不太會用,例如如何找指定索引下指定類型的某個查詢?
不亂以上哪一種方式,哪位朋友能指點一二,感謝! ! !

<code>        $params=[
            'index' => 'n_index',
            'type' => 'n_type',
            'body' => [
                'query' => [
                    'match' => ['ntitle' => '要查询的词']
                ],
                'highlight'=>[
                    'pre_tags'=>['<tag1>', '<tag2>'],
                    'post_tags'=>['</tag1>', '</tag2>'],
                    'fields'=>[
                        'ntitle'=>[]
                    ]
                ]
            ]
        ];

        $response = $this->client->search($params);
    
        echo "<pre class="brush:php;toolbar:false">";
        var_dump($response);
        echo "
";

以上程式碼可以查出以下結果

<code>array(4) {
  ["took"]=>
  int(6)
  ["timed_out"]=>
  bool(false)
  ["_shards"]=>
  array(3) {
    ["total"]=>
    int(5)
    ["successful"]=>
    int(5)
    ["failed"]=>
    int(0)
  }
  ["hits"]=>
  array(3) {
    ["total"]=>
    int(3)
    ["max_score"]=>
    float(1.8223838)
    ["hits"]=>
    array(3) {
      [0]=>
      array(5) {
        ["_index"]=>
        string(7) "n_index"
        ["_type"]=>
        string(6) "n_type"
        ["_id"]=>
        string(1) "6"
        ["_score"]=>
        float(1.8223838)
        ["_source"]=>
        array(1) {
          ["ntitle"]=>
          string(35) "ntitle内容2"
        }
      }
      [1]=>
      array(5) {
        ["_index"]=>
        string(7) "n_index"
        ["_type"]=>
        string(6) "n_type"
        ["_id"]=>
        string(1) "7"
        ["_score"]=>
        float(1.2087858)
        ["_source"]=>
        array(1) {
          ["ntitle"]=>
          string(35) "ntitle内容1"
        }
      }
      [2]=>
      array(5) {
        ["_index"]=>
        string(7) "n_index"
        ["_type"]=>
        string(6) "n_type"
        ["_id"]=>
        string(1) "5"
        ["_score"]=>
        float(0.23033649)
        ["_source"]=>
        array(1) {
          ["ntitle"]=>
          string(35) "ntitle内容3"
        }
      }
    }
  }
}</code>

沒有回到高亮部分,

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn