Elasticsearch 参考指南(Multi Get API)
Multi Get API
Multi Get API允许基于索引、类型(可选)和id(可能还有路由)获得多个文档,响应包括一个docs数组,其中包含所有获取的文档,以便与原始的multi-get请求相对应(如果特定get失败,则在响应中包括包含此错误的对象),成功get的结构在结构上类似于get API提供的文档。
这里有一个例子:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1"
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2"
}
]
}mget端点还可以用于索引(在这种情况下,body中不需要索引):
GET /test/_mget
{
"docs" : [
{
"_type" : "_doc",
"_id" : "1"
},
{
"_type" : "_doc",
"_id" : "2"
}
]
}和类型:
GET /test/_doc/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}在这种情况下,可以直接使用ids元素来简化请求:
GET /test/_doc/_mget
{
"ids" : ["1", "2"]
}源过滤
默认情况下,每个文档将返回_source字段(如果存储了的话),与get API类似,通过使用_source参数,你可以只检索_source的一部分(或者完全没有),你还可以使用url参数_source、_source_include和_source_exclude来指定默认值,当没有针对每个文档指令时将使用默认值。
例如:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_source" : false
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_source" : ["field3", "field4"]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
]
}字段
可以指定特定的存储字段,以便对每个要获取的文档进行检索,类似于get API的stored_fields参数,例如:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"stored_fields" : ["field1", "field2"]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"stored_fields" : ["field3", "field4"]
}
]
}或者,你可以将查询字符串中的stored_fields参数指定为应用于所有文档的默认参数。
GET /test/_doc/_mget?stored_fields=field1,field2
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2",
"stored_fields" : ["field3", "field4"]
}
]
}"_id" : "1"=> 返回field1、field2。"stored_fields" : ["field3", "field4"]=> 返回field3、field4。
路由
还可以将路由值指定为参数:
GET /_mget?routing=key1
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"routing" : "key2"
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2"
}
]
}在本例中,文档test/_doc/2将从与路由键key1对应的碎片中获取,但是,文档test/_doc/1将从与路由键key2对应的碎片中获取。
相关推荐
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。