Elasticsearch增删改查 之 —— mget多文档查询

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介:

之前说过了针对单一文档的增删改查,基本也算是达到了一个基本数据库的功能。本篇主要描述的是多文档的查询,通过这个查询语法,可以根据多个文档的查询条件,返回多个文档集合。
更多内容可以参考我整理的ELK文档教程

multi Get

多字段查询可以设置多个文档查询条件,每个查询条件在结构上都比较类似:

curl 'localhost:9200/_mget' -d '{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2"
        }
    ]
}'

当然,在查询条件中,body中_index字段也可以放在查询字符串中:

curl 'localhost:9200/test/_mget' -d '{
    "docs" : [
        {
            "_type" : "type",
            "_id" : "1"
        },
        {
            "_type" : "type",
            "_id" : "2"
        }
    ]
}'

对于type也是一样:

curl 'localhost:9200/test/type/_mget' -d '{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}'

如果索引和类型都放在查询URL中,那么字段ID就可以放在一个数组中:

curl 'localhost:9200/test/type/_mget' -d '{
    "ids" : ["1", "2"]
}'

type可选

mget查询中类型type是可选的。如果设置_all或者不设置,就会匹配所有的类型,那么仅仅会返回第一个匹配的文档。

但是如果没有设置type,然后查询的id里面又出现两个一样的id,就会返回第一次匹配的文档两次:

curl 'localhost:9200/test/_mget' -d '{
    "ids" : ["1", "1"]
}'

因此如果想要查询到不同类型的id,就需要指定类型名称:

GET /test/_mget/
{
  "docs" : [
        {
            "_type":"typeA",
            "_id" : "1"
        },
        {
            "_type":"typeB",
            "_id" : "1"
        }
    ]
}

_source过滤

默认_source字段会返回所有的内容,你也可以通过_source进行过滤。比如使用_source,_source_include,_source_exclude.
比如:

curl 'localhost:9200/_mget' -d '{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1",
            "_source" : false
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2",
            "_source" : ["field3", "field4"]
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "3",
            "_source" : {
                "include": ["user"],
                "exclude": ["user.location"]
            }
        }
    ]
}'

Fields过滤

与其他的普通查询差不多,mget查询也支持Fields过滤。

curl 'localhost:9200/_mget' -d '{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1",
            "fields" : ["field1", "field2"]
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2",
            "fields" : ["field3", "field4"]
        }
    ]
}'

也可以在URL中的查询字符串中设置默认的过滤,然后在Body中进行特殊的修改:

curl 'localhost:9200/test/type/_mget?fields=field1,field2' -d '{
    "docs" : [
        {
            "_id" : "1" 
        },
        {
            "_id" : "2",
            "fields" : ["field3", "field4"] 
        }
    ]
}'

id1的文档就会返回field1和field2,id2的文档就会返回field3和field4.

路由

在mget查询中也会涉及到路由的问题。可以在url中设置默认的路由,然后在Body中修改:

curl 'localhost:9200/_mget?routing=key1' -d '{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1",
            "_routing" : "key2"
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2"
        }
    ]
}'

在上面的例子中,test/type/1按照key2这个路由锁定分片进行查询;test/type/2按照key1这个路由锁定分片进行查询。

实际演练

首先创建两个文档:

curl -XPOST localhost:9200/test/_mget?pretty -d '{"ids":["1"]}'
curl -XPOST localhost:9200/test/testb/1?pretty -d '{"name":"b","age":122}'

如果不指定type,那么返回的仅仅是一个最先匹配的结果:

$ curl -XPOST localhost:9200/test/_mget?pretty -d '{"ids":["1"]}'                                   {
  "docs" : [ {
    "_index" : "test",
    "_type" : "testb",
    "_id" : "1",
    "_version" : 2,
    "found" : true,
    "_source" : {
      "name" : "b",
      "age" : 122
    }
  } ]
}

如果指定了重复的id,则返回的是多次第一次匹配的文档:

$ curl -XPOST localhost:9200/test/_mget?pretty -d '{"ids":["1","1"]}'                               {
  "docs" : [ {
    "_index" : "test",
    "_type" : "testb",
    "_id" : "1",
    "_version" : 2,
    "found" : true,
    "_source" : {
      "name" : "b",
      "age" : 122
    }
  }, {
    "_index" : "test",
    "_type" : "testb",
    "_id" : "1",
    "_version" : 2,
    "found" : true,
    "_source" : {
      "name" : "b",
      "age" : 122
    }
  } ]
}

如果指定了类型,再去查询,则返回的是各自的id:

$ curl -XPOST localhost:9200/test/_mget?pretty -d '{"docs":[{"_type":"testa","_id":"1"},{"_type":"testb","_id":"1"}]}'
{
  "docs" : [ {
    "_index" : "test",
    "_type" : "testa",
    "_id" : "1",
    "_version" : 1,
    "found" : true,
    "_source" : {
      "name" : "a",
      "age" : 31
    }
  }, {
    "_index" : "test",
    "_type" : "testb",
    "_id" : "1",
    "_version" : 2,
    "found" : true,
    "_source" : {
      "name" : "b",
      "age" : 122
    }
  } ]
}
本文转自博客园xingoo的博客,原文链接:Elasticsearch增删改查 之 —— mget多文档查询,如需转载请自行联系原博主。
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
2月前
|
存储 固态存储 Java
Elasticsearch中查询性能优化
Elasticsearch中查询性能优化
196 0
|
3月前
Elasticsearch之RestClient查询文档
Elasticsearch之RestClient查询文档
139 1
|
3月前
|
缓存 JSON API
Elasticsearch-05Elasticsearch之查询与过滤
Elasticsearch-05Elasticsearch之查询与过滤
165 0
|
2月前
|
缓存 算法 索引
【Elasticsearch专栏 07】深入探索:Elasticsearch的倒排索引如何进行模糊查询和通配符查询
Elasticsearch的倒排索引支持模糊查询和通配符查询,通过特定的算法和数据结构,能够实现对关键词的模糊匹配和通配符匹配。这两种查询类型提供了更灵活的搜索功能,但可能影响查询性能,需结合优化策略使用。
|
2月前
|
缓存 自然语言处理 数据挖掘
一篇文章让你学会Elasticsearch中的查询
一篇文章让你学会Elasticsearch中的查询
137312 118
|
2月前
|
测试技术 定位技术 API
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
94614 140
|
2月前
|
JSON 前端开发 API
【Elasticsearch】搜索结果处理和RestClient查询文档
【Elasticsearch】搜索结果处理和RestClient查询文档
338 0
|
2月前
|
JSON 自然语言处理 算法
【Elasticsearch】DSL查询文档
【Elasticsearch】DSL查询文档
315 0
|
3月前
|
Java
Springboot整合Elasticsearch 7.X 复杂查询
这里使用Springboot 2.7.12版本,Elasticsearch为7.15.0。
Springboot整合Elasticsearch 7.X 复杂查询
|
3月前
|
Java
elasticsearch在Java中查询指定列的方法
elasticsearch在Java中查询指定列的方法
86 0

热门文章

最新文章