前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch增删改查 之 —— mget多文档查询

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

作者头像
用户1154259
发布2018-01-17 15:49:31
6500
发布2018-01-17 15:49:31
举报

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

multi Get

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

代码语言:javascript
复制
curl 'localhost:9200/_mget' -d '{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2"
        }
    ]
}'

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

代码语言:javascript
复制
curl 'localhost:9200/test/_mget' -d '{
    "docs" : [
        {
            "_type" : "type",
            "_id" : "1"
        },
        {
            "_type" : "type",
            "_id" : "2"
        }
    ]
}'

对于type也是一样:

代码语言:javascript
复制
curl 'localhost:9200/test/type/_mget' -d '{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}'

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

代码语言:javascript
复制
curl 'localhost:9200/test/type/_mget' -d '{
    "ids" : ["1", "2"]
}'

type可选

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

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

代码语言:javascript
复制
curl 'localhost:9200/test/_mget' -d '{
    "ids" : ["1", "1"]
}'

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

代码语言:javascript
复制
GET /test/_mget/
{
  "docs" : [
        {
            "_type":"typeA",
            "_id" : "1"
        },
        {
            "_type":"typeB",
            "_id" : "1"
        }
    ]
}

_source过滤

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

代码语言:javascript
复制
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过滤。

代码语言:javascript
复制
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中进行特殊的修改:

代码语言:javascript
复制
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中修改:

代码语言:javascript
复制
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这个路由锁定分片进行查询。

实际演练

首先创建两个文档:

代码语言:javascript
复制
curl -XPOST localhost:9200/test/_mget?pretty -d '{"ids":["1"]}'
curl -XPOST localhost:9200/test/testb/1?pretty -d '{"name":"b","age":122}'

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

代码语言:javascript
复制
$ 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,则返回的是多次第一次匹配的文档:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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
    }
  } ]
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • multi Get
  • type可选
  • _source过滤
  • Fields过滤
  • 路由
  • 实际演练
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档