美文网首页ELK stack互联网科技大数据
Elasticsearch-php之高亮搜索

Elasticsearch-php之高亮搜索

作者: lookphp | 来源:发表于2017-12-29 15:15 被阅读100次

搜索关键字,使结果高亮关键字,类似百度搜索结果一样,如下图。

关键字高亮.png

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

    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>高亮包含了“中国”:

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为什么不能返回高亮数据?

相关文章

  • Elasticsearch-php之高亮搜索

    搜索关键字,使结果高亮关键字,类似百度搜索结果一样,如下图。 在PHP中,调用elasticsearch进行搜索时...

  • VIM:搜索高亮

    打开高亮显示搜索结果 :set hlsearch 暂时取消高亮显示当前的搜索结果 :noh:nohlsearch ...

  • 搜索关键字高亮显示,就比微信多个多音字搜索

    搜索关键字高亮显示,就比微信多个多音字搜索 搜索关键字高亮显示,就比微信多个多音字搜索

  • vim使用技巧总结

    1. 搜索高亮显示 2. 取消搜索结果高亮显示 3. 多行注释: 4. 删除多行注释:

  • 00102-全文检索

    全文搜索 Get 结果 短语搜索 Get 结果 高亮我们的搜索 Get 结果

  • 实践-小效果 V

    1.搜索关键字高亮显示 搜索关键字高亮显示Dome 2.动态改变tableHeaderView的高度 关键效果设置...

  • 2.Elasticsearch-PHP

    Elasticsearch-PHP

  • linux实用命令解析

    vim打开文本进行搜索后,搜索到的内容会高亮显示 但是,如果搜索完成后这些匹配的内容还是会高亮显示 解决方法很简单...

  • UIButton 疑难杂症

    button 不能正常显示高亮,轻点 button 时不高亮,而长按时才高亮搜索发现,问题在于将 button 加...

  • 小程序、uni-app 搜索内容高亮

    搜索结果展示高亮效果 效果图展示 未高亮效果 高亮效果含模糊查询 实现逻辑 展示数据 map + includes...

网友评论

  • 张震宇_419f:能麻烦回答下我吗?我的elasticsearch-php版本是6.4.3
  • 张震宇_419f:为什么我返回了高亮的数据,但是没标签

本文标题:Elasticsearch-php之高亮搜索

本文链接:https://www.haomeiwen.com/subject/qwbigxtx.html