前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch-php之高亮搜索 转

Elasticsearch-php之高亮搜索 转

作者头像
双面人
发布2019-04-10 15:24:38
1.4K0
发布2019-04-10 15:24:38
举报
文章被收录于专栏:热爱IT热爱IT

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

代码语言:javascript
复制
    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>高亮包含了“中国”:

代码语言:javascript
复制
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为什么不能返回高亮数据?

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/12/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档