本篇文章主要介绍了"Elasticsearch 220 文档篇:多文档操作",主要涉及到方面的内容,对于企业开发感兴趣的同学可以参考一下:
在Elasticsearch对文档的操作中,之前介绍的都是对单个文档进行操作,其实Elasticsearch同时可以对多个文档同时操作。下面介绍多文档查...
在Elasticsearch对文档的操作中,之前介绍的都是对单个文档进行操作,其实Elasticsearch同时可以对多个文档同时操作。下面介绍多文档查询。
多文档查询
多文档查询可以在同一个接口中查询多个文档,可以分别指定index,type,id来进行多个文档的查询,响应包括所有查询到文档数组,每个元素在结构上类似于单个文档查询,例如:
请求:POST http://localhost:9200/_mget?pretty
参数:
{
"docs" : [
{
"_index" : "secilog",
"_type" : "log",
"_id" : "1"
},
{
"_index" : "secilog",
"_type" : "log",
"_id" : "2"
}
]
}
返回结果:
{
"docs" : [ {
"_index" : "secilog",
"_type" : "log",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"collect_type" : "syslog",
"collect_date" : "2016-01-11T09:32:12",
"message" : "Failed password for root from 192.168.21.2 port 50790 ssh2"
}
}, {
"_index" : "secilog",
"_type" : "log",
"_id" : "2",
"_version" : 1,
"found" : true,
"_source" : {
"collect_type" : "syslog",
"collect_date" : "2016-01-12T09:32:12",
"message" : "secisland mget test!"
}
} ]
}
从中可以看出,一次查询了两个文档。
在查询的时候,index,type可以在url中直接填写。例如下面两个请求和之前的是等价的。
请求:POST http://localhost:9200/secilog/_mget?pretty
参数:
{
"docs" : [
{
"_type" : "log",
"_id" : "1"
},
{
"_type" : "log",
"_id" : "2"
}
]
}
请求:POST http://localhost:9200/secilog/log/_mget?pretty
参数:
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
对于上一种,可以用更加简化的方式查询: