Elasticsearch(036):es中批量操作之mget(批量查询)
mget(批量查询)
一、介绍
mget
是es
提供的一种批量的读取操作的API。
mget
API允许根据索引,类型(可选)
和id(以及可能的路由)
获取多个文档。响应包括一个docs数组
。该数组具有与原始多重获取请求相对应的所有提取文档(如果特定的get失败,则在响应中包含包含此错误的对象)。成功获取的结构与get API提供的文档的结构相似。
二、示例用法
2.1 测试数据准备
示例索引结构如下
PUT example
PUT example/docs/_mapping
{
"properties": {
"id": { "type": "long"},
"name": { "type": "text"},
"counter": { "type": "integer"},
"tags": { "type": "text"}
}
}
PUT example/docs/1
{
"id": 1,
"name": "admin",
"counter": 1,
"tags": ["gray"]
}
PUT example/docs/2
{
"id": 2,
"name": "zhangsan",
"counter": 1,
"tags": ["black"]
}
PUT example/docs/3
{
"id": 3,
"name": "lisi",
"counter": 1,
"tags": ["purple"]
}
PUT example_test
PUT example_test/docs/_mapping
{
"properties": {
"id": { "type": "long"},
"name": { "type": "text"},
"counter": { "type": "integer"},
"tags": { "type": "text"}
}
}
PUT example_test/docs/1
{
"id": 1,
"name": "test-admin",
"counter": 1,
"tags": ["gray"]
}
PUT example_test/docs/2
{
"id": 2,
"name": "test-zhangsan",
"counter": 1,
"tags": ["black"]
}
PUT example_test/docs/3
{
"id": 3,
"name": "test-lisi",
"counter": 1,
"tags": ["purple"]
}
#查询example所有数据
GET example/docs/_search
{
"query": {
"match_all": { }
}
}
#查询example_test所有数据
GET example_test/docs/_search
{
"query": {
"match_all": { }
}
}
2.2 传统的单条查询,在面对需要查询多个时,需要一次次发送请求到es
示例查询
GET example/docs/1
GET example/docs/2
GET example_test/docs/1
GET example_test/docs/2
这样其实局限性,多次的网络请求很容易造成资源浪费和降低性能。
2.3 mget之查询(index不同)
示例如下
GET _mget
{
"docs": [
{
"_index": "example",
"_type": "docs",
"_id": "1"
},
{
"_index": "example",
"_type": "docs",
"_id": "2"
},
{
"_index": "example_test",
"_type": "docs",
"_id": "1"
},
{
"_index": "example_test",
"_type": "docs",
"_id": "2"
}
]
}
返回数据如下
{
"docs": [
{
"_index": "example",
"_type": "docs",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"name": "admin",
"counter": 1,
"tags": [
"gray"
]
}
},
{
"_index": "example",
"_type": "docs",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"id": 2,
"name": "zhangsan",
"counter": 1,
"tags": [
"black"
]
}
},
{
"_index": "example_test",
"_type": "docs",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"name": "test-admin",
"counter": 1,
"tags": [
"gray"
]
}
},
{
"_index": "example_test",
"_type": "docs",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"id": 2,
"name": "test-zhangsan",
"counter": 1,
"tags": [
"black"
]
}
}
]
}
2.4 mget之查询(index相同,type不同)
不建议掌握,type有可能在后续的版本废除。这里只做一下简单示例。
GET _mget
{
"docs": [
{
"_index": "example",
"_type": "docs1",
"_id": "1"
},
{
"_index": "example",
"_type": "docs2",
"_id": "1"
},
]
2.5 mget之查询(index相同,type相同)。
示例如下
GET _mget
{
"docs": [
{
"_index": "example",
"_type": "docs",
"_id": "1"
},
{
"_index": "example",
"_type": "docs",
"_id": "2"
},
{
"_index": "example",
"_type": "docs",
"_id": "3"
}
]
}
#index和type相同的话可以简化成如下
GET example/docs/_mget
{
"docs": [
{
"_id": "1"
},
{
"_id": "2"
},
{
"_id": "3"
}
]
}
返回结果
{
"docs": [
{
"_index": "example",
"_type": "docs",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"name": "admin",
"counter": 1,
"tags": [
"gray"
]
}
},
{
"_index": "example",
"_type": "docs",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"id": 2,
"name": "zhangsan",
"counter": 1,
"tags": [
"black"
]
}
},
{
"_index": "example",
"_type": "docs",
"_id": "3",
"_version": 1,
"found": true,
"_source": {
"id": 3,
"name": "lisi",
"counter": 1,
"tags": [
"purple"
]
}
}
]
}
mget
是很重要的,一般来说,进行查询的时候,如果一次性查询多条数据的话,那么一定要用batch
批量操作的api,尽可能减少网络开销次数,可以将性能大幅度提升。
还没有评论,来说两句吧...