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

之前说过了针对单一文档的增删改查,基本也算是达到了一个基本数据库的功能。本篇主要描述的是多文档的查询,通过这个查询语法,可以根据多个文档的查询条件,返回多个文档集合。
更多内容可以参考我整理的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多文档查询,如需转载请自行联系原博主。

时间: 2024-08-17 04:49:26

Elasticsearch增删改查 之 —— mget多文档查询的相关文章

Elasticsearch增删改查 之 —— Get查询

GET API是Elasticsearch中常用的操作,一般用于验证文档是否存在:或者执行CURD中的文档查询.与检索不同的是,GET查询是实时查询,可以实时查询到索引结果.而检索则是需要经过处理,一般默认是1秒钟吧...才能搜索到.合理利用这些方法,可以更灵活的使用Elasticsearch. 更多内容参考ELK教程 阅读这篇文档,发现自己对很多地方不是很理解.比如存储机制.版本维护等等.暂时先做为阶段性的学习吧...后续更新在回来补补.... 查询样例 Get API允许基于ID字段从Ela

Elasticsearch增删改查 之 —— Delete删除

删除文档也算是常用的操作了...如果把Elasticsearch当做一款普通的数据库,那么删除操作自然就很常用了.如果仅仅是全文检索,可能就不会太常用到删除. Delete API 删除API,可以根据特定的ID删除文档. $ curl -XDELETE 'http://localhost:9200/twitter/tweet/1' 会返回下面的消息: { "_shards" : { "total" : 10, "failed" : 0, &qu

求一份Struts2+Spring+mybatis整合的增删改查及分页和多条件查询的源代码

问题描述 求一份Struts2+Spring+mybatis整合的增删改查及分页和多条件查询的源代码 小白求一份Struts2+Spring+mybatis整合的增删改查及分页和多条件查询的源代码,用jQuery实现jsp页面 解决方案 SpringMVC+Spring+mybatis是否需要呢?笔者之前写了一些相关的文章,也有源代码,你可以参考下:http://blog.csdn.net/evankaka/article/details/49452201http://blog.csdn.net

Elasticsearch Javascript API增删改查

查询 根据索引.类型.id进行查询: client.get({ index:'myindex', type:'mytype', id:1 },function(error, response){// ...}); 根据某个查询条件,查询某个索引的所有数据 client.search({ index:'myindex', q:'title:test' },function(error, response){// ...}); 复杂一点的查询: client.search({ index:'myin

sharepoint做数据的增删改查,统计

问题描述 用户之前自己用excel表在维护他们的数据,还用到了excel的宏:实现简单的数据增删改和查询统计:但因为想多人同时编辑,也希望更好用的一点:找到我希望基于工厂的sharepoint实现此功能:如果用vs.net之类+sqlserver,分分钟就轻松搞定:但由于某些原因,只能用sharepoint来做:我用sharepoint做过一些应用,比如文档库.工作流等等:但上述需求没做过,谁有好的建议吗:sharepoint能否实现的了这个需求:谢谢: 解决方案 解决方案二:建议尝试用Shar

Mongodb c#增删改查

写在前面 最近项目需要,就研究了下mongodb,也是为了快速上手,就自己弄了一个简单的例子,这里记录一下. Mongodb 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(document)三个层次组成.MongoDB对于关系型数据库里的表,但是集合中没有列.行和关系概念,这体现了模式自由的特点. 那么在c#如何使用呢?下面看个例子,你会发现上手非常简

cassandra入门(一):jdbc连接cassandra作增删改查

先分享一个最新的cassandra-java-driver文档,点击电子书分享里的链接,找到javaDriver21.pdf. 该文档内容比较全,包含:jdbc连接cassandra集群,执行cql增删改查,批量查询,异步查询,cql的类型和java类型的映射关系及用户自定义类型使用,ORM等. Cassandra是一个NoSql数据库,纯java编写,apache的顶级项目,主页:http://cassandra.apache.org/(简介不多说网上有). 入门步骤:(我的jdk版本是1.7

现在在学hibernate可是总是不会写各种增删改查方法有什么方法能让我快速搞明白呢

问题描述 现在在学hibernate可是总是不会写各种增删改查方法有什么方法能让我快速搞明白呢 现在在学hibernate可是总是不会写各种增删改查方法有什么方法能让我快速搞明白呢 不会写方法啊 求速教 解决方案 增删查改就4个方法,都学不会?那么基本上你得问问自己,基本的编程知识是不是都没有.建议从基础的学.比如说开发环境怎么搭建,怎么写基本的java程序. 解决方案二: hibernate不难的,他就一个框架.把sql语句研究下就会了.主要发是环镜的搭建,配置spring+springMVC

Servlet实现对SQLServer数据库的增删改查(含工程源码)

本文实现了用MyEclipse,编写Servlet,实现对SQLServer数据库的增删改查,适合新手入门,文末提供工程文件源码下载. 1.新建数据库test以及表users 表users共四列(id,name,psd,tel) 具体操作步骤见上篇博文.具体操作步骤 2.新建工程Web Project工程0623p 3. 编辑WebRoot目录下的index.jsp 创建表单(序号.账号.密码.电话.操作),并读取当前数据库内容. 注意: 此处要导入sql_data.java包用于连接数据库(