elasticsearch - 哪位朋友知道es-php或mmanos/laravel-search的查询结果高亮问题

php中文网
发布: 2016-08-04 09:21:42
原创
1704人浏览过

以下代码是laravel+elastisearch-php(https://github.com/elastic/elasticsearch-php)实现的,但是不能返回高亮内容,网上看了https://segmentfault.com/a/1190000003010186 下面有一个例子提到高亮,但mmanos的不太会用,比如如何找指定索引下指定类型的某个查询?
不乱以上哪种方式,哪位朋友能指点一二,感谢!!!

        $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 "
登录后复制
登录后复制
";

以上代码可以查出以下结果

立即学习PHP免费学习笔记(深入)”;

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"
        }
      }
    }
  }
}
登录后复制
登录后复制

没有返回高亮部分,

回复内容:

以下代码是laravel+elastisearch-php(https://github.com/elastic/elasticsearch-php)实现的,但是不能返回高亮内容,网上看了https://segmentfault.com/a/1190000003010186 下面有一个例子提到高亮,但mmanos的不太会用,比如如何找指定索引下指定类型的某个查询?
不乱以上哪种方式,哪位朋友能指点一二,感谢!!!

        $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 "
登录后复制
登录后复制
";

以上代码可以查出以下结果

立即学习PHP免费学习笔记(深入)”;

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"
        }
      }
    }
  }
}
登录后复制
登录后复制

没有返回高亮部分,

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号