首页 > 其他分享 >elasticsearch之单请求多查询

elasticsearch之单请求多查询

时间:2023-01-05 18:24:00浏览次数:52  
标签:multi name hits 查询 score elasticsearch 之单 test total

一、需要解决的问题

有的时候我们需要同时执行多个查询,并且需要得到每个单独查询的搜索结果,elasticsearch提供了multi search此需求的支持;

二、elasticsearch multi search简介

elasticsearch提供了multi search api来支持一个请求执行多个查询;

multi search api的请求体使用换行分割的JSON格式;

header\n
body\n
header\n
body\n

multi search返回的结果是responses数组,每个查询对应一个数组元素;每个数组元素都有一个status字段指示查询是否执行成功,如果执行失败则error字段返回错误信息;

每个查询可以通过自己的header设置查询执行的index,也可以是空的JSON对象,这是在URL中指定的index执行查询;

三、数据准备

index以下四个文档

PUT /multi_test/_doc/1
{
	"name":"Google Chrome"
}


PUT /multi_test/_doc/2
{
	"name":"NotePad"
}

PUT /multi_test/_doc/3
{
	"name":"Word"
}

PUT /multi_test/_doc/4
{
	"name":"PyCharm"
}

查询查看已经索引的数据

GET /multi_test/_search

{
    "took":0,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":4,
        "max_score":1,
        "hits":[
            {
                "_index":"multi_test",
                "_type":"_doc",
                "_id":"2",
                "_score":1,
                "_source":{
                    "name":"NotePad"
                }
            },
            {
                "_index":"multi_test",
                "_type":"_doc",
                "_id":"4",
                "_score":1,
                "_source":{
                    "name":"PyCharm"
                }
            },
            {
                "_index":"multi_test",
                "_type":"_doc",
                "_id":"1",
                "_score":1,
                "_source":{
                    "name":"Google Chrome"
                }
            },
            {
                "_index":"multi_test",
                "_type":"_doc",
                "_id":"3",
                "_score":1,
                "_source":{
                    "name":"Word"
                }
            }
        ]
    }
}

四、查询测试

我们构造以下四个查询同时执行

POST /multi_test/_msearch

{}
{"query":{"match":{"name":"google"}}}
{}
{"query":{"match":{"name":"nohit"}}}
{}
{"query":{"match":{"name":"word"}}}
{}
{"query":{"bool":{"should":[{"match":{"name":"word"}}, {"match":{"name":"pycharm"}}]}}}

我们可以看到不管查询是否命中结果,都会有一个responses数组元素对应;同时responses数组元素的顺序与查询是一 一对应的;

{
    "responses":[
        {
            "took":0,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":1,
                "max_score":0.2876821,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"1",
                        "_score":0.2876821,
                        "_source":{
                            "name":"Google Chrome"
                        }
                    }
                ]
            },
            "status":200
        },
        {
            "took":0,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":0,
                "max_score":null,
                "hits":[

                ]
            },
            "status":200
        },
        {
            "took":0,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":1,
                "max_score":0.2876821,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"3",
                        "_score":0.2876821,
                        "_source":{
                            "name":"Word"
                        }
                    }
                ]
            },
            "status":200
        },
        {
            "took":0,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":2,
                "max_score":0.6931472,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"4",
                        "_score":0.6931472,
                        "_source":{
                            "name":"PyCharm"
                        }
                    },
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"3",
                        "_score":0.2876821,
                        "_source":{
                            "name":"Word"
                        }
                    }
                ]
            },
            "status":200
        }
    ]
}

五、multi search对search template的支持

multi search api也支持search template;

muti search内联search template查询

POST /multi_test/_msearch/template
{}
{ "source" : "{ \"query\": { \"match\": { \"name\" : \"{{name}}\" } } } }", "params": { "name": "google" } }
{}
{ "source" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }

查询结果为

{
    "responses":[
        {
            "took":0,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":1,
                "max_score":0.2876821,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"1",
                        "_score":0.2876821,
                        "_source":{
                            "name":"Google Chrome"
                        }
                    }
                ]
            }
        },
        {
            "took":8,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":4,
                "max_score":1,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"2",
                        "_score":1,
                        "_source":{
                            "name":"NotePad"
                        }
                    },
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"4",
                        "_score":1,
                        "_source":{
                            "name":"PyCharm"
                        }
                    },
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"1",
                        "_score":1,
                        "_source":{
                            "name":"Google Chrome"
                        }
                    },
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"3",
                        "_score":1,
                        "_source":{
                            "name":"Word"
                        }
                    }
                ]
            }
        }
    ]
}

也可以直接新建search template

POST /_scripts/multi_test_name_template/

{
    "script":{
        "lang":"mustache",
        "source":{
            "query":{
                "bool":{
                    "should":[
                        {
                            "match":{
                                "name":"{{name}}"
                            }
                        },
                        {
                            "wildcard":{
                                "name":"*{{name}}*"
                            }
                        }
                    ]
                }
            }
        }
    }
}

使用新建的search template进行搜索

POST /multi_test/_msearch/template
{}
{ "id": "multi_test_name_template", "params": { "name": "oo" } }
{}
{ "id": "multi_test_name_template", "params": { "name": "notepad" } }

搜索结果如下

{
    "responses":[
        {
            "took":1,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":1,
                "max_score":1,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"1",
                        "_score":1,
                        "_source":{
                            "name":"Google Chrome"
                        }
                    }
                ]
            }
        },
        {
            "took":1,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":1,
                "max_score":1.6931472,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"2",
                        "_score":1.6931472,
                        "_source":{
                            "name":"NotePad"
                        }
                    }
                ]
            }
        }
    ]
}

在search template中使用嵌套字段作为参数

POST /multi_test/_msearch/template
{}
{ "source" : "{ \"query\": { \"match\": { \"name\" : \"{{person.name}}\" } } } }", "params": { "person":{"name": "google"} } }

搜索结果如下

{
    "responses":[
        {
            "took":3,
            "timed_out":false,
            "_shards":{
                "total":5,
                "successful":5,
                "skipped":0,
                "failed":0
            },
            "hits":{
                "total":1,
                "max_score":0.2876821,
                "hits":[
                    {
                        "_index":"multi_test",
                        "_type":"_doc",
                        "_id":"1",
                        "_score":0.2876821,
                        "_source":{
                            "name":"Google Chrome"
                        }
                    }
                ]
            }
        }
    ]
}

标签:multi,name,hits,查询,score,elasticsearch,之单,test,total
From: https://www.cnblogs.com/wufengtinghai/p/17028555.html

相关文章

  • hdu: 张煊的金箍棒(3)(树状数组的区间修改,区间查询)
    ProblemDescription张煊的金箍棒升级了!升级后的金箍棒是由N段相同长度的金属棒连接而成(最开始每段金属棒的价值都是1,从1到N编号);张煊作为金箍棒的主人,可以对金箍棒任意......
  • sqlite3 cli 快速查询数据库内容
    原文链接https://www.digitalocean.com/community/tutorials/how-to-install-and-use-sqlite-on-ubuntu-20-04sudoaptupdatesudoaptinstallsqlite3sqlite3--versi......
  • oracle空间及归档查询sql
     -----查看用户表所占用表空间selectOWNER,t.segment_name,t.segment_type,sum(t.bytes/1024/1024)mmmfromdba_segmentstwhere/*t.owner='你要查询......
  • 【Oracle】Oracle查询时去除某个字段的特殊符号,可扩展
    CREATEORREPLACEFUNCTIONFUN_DEL_ENTER(DATA_NAMEVARCHAR2)RETURNVARCHAR2IS/**获取参数**/V_RESULTVARCHAR2(4000);V_RESULT1VARCHAR2(4000);V_RESU......
  • 可以综合查询的站长工具有哪些?站长工具好用吗?
    作为seo人,免不了要经常对网站进行seo诊断,比如友链、外链、权重、收录等等,这就需要用到seo查询工具。seo查询工具种类繁多,好用的查询工具可以使工作做起来事半功倍,今天就来介......
  • 如何知道自己网站的收录情况?怎么查询?
    EO人需要通过查询网站是否被收录来判断和更好的运营网站,市面上查询的工具虽然多但是特点不一,有的可以批量查但是需要付费,有的不能批量查,有的不能导出数据,有的查询速度慢,今天......
  • 21、商品服务--三级分类--查询、递归树形数据获取
    1、编写查询分类的controller2、给CategoryEntity添加一个children字段3、编写service的接口4、编写serviceImpl的接口实现(用到了递归来设置子菜单下的子菜单)......
  • 读 NebulaGraph源码 | 查询语句 LOOKUP 的一生
    本文由社区用户Milittle供稿LOOKUP是图数据库NebulaGraph的一个查询语句。它依赖索引,可以查询点或者边的信息。在本文,我将着重从源码的角度解析一下LOOKUP语句......
  • Elasticsearch学习笔记
    ​​最新ElasticSearch6实战教程​​​​全文搜索引擎Elasticsearch入门教程​​​​Elasticsearch学习,请先看这一篇!​​​​ElasticSearch简介​​​​Elasticsearch入门......
  • mybatis模糊查询
    1.第一种使用likeconcat<selectid="selectLogininforList"parameterType="SysLogininfor"resultMap="SysLogininforResult"> selectinfo_id,user_name,ipaddr,log......