es的批量查询(批量操作)
如果我们要查询的话,查询100条数据,然后把这100条数据拼接的话就需要发送100次请求,这个开销很大
基于mget的批量查询
mget是es的一个api可以一次性的从同一个索引或者多个索引一起检索文档
我们首先放入数据
1 | put /ecommerce/product/1 |
批量查询:
1 | GET/_mget |
结果:
1 | { |
就像上文一样,索引都是一个索引,查询的是一个索引内的多个数据
1 | GET ecommerce/_mget |
如果是docs内都是同一个index同一个type的就可以
1 | GET /ecommerce/product/_mget |
当前我们可以从不同的索引中获取文档
1 | GET /_mget |
基于bulk的批量增删改
这个bulk api对格式有严格的要求,除了delete外,每一个操作都要两个json字符串并且每一个json字符串内不能换行,非同一个json字符串必须换行
基本格式:
1 | POST /<index>/_bulk |
增加
1 | POST /_bluk |
这里我们在这个test的表的所有id为2的数据下面加上了两个字段,一个是field1,一个是field2
删除
1 | POST /bulk |
修改
1 | POST /_bulk |
filter_path
filter_path是es中可以过滤返回的响应内容的使用
可以减少es的返回数据量
下面是使用方式:
filter_path=took
: 这个请求仅返回执行请求所花费的时间(以毫秒为单位)。filter_path=items._id,items._index
: 这个请求仅返回每个 item 的 _id 和 _index 字段。filter_path=items.*.error
: 这个请求会返回所有包含 error 字段的 items。filter_path=hits.hits._source
: 这个请求仅返回搜索结果中的原始文档内容。filter_path=_shards, hits.total
: 这个请求返回关于 shards 的信息和命中的总数。filter_path=aggregations.*.value
: 这个请求仅返回每个聚合的值。- 请注意,如果你在 filter_path 中指定了多个字段,你需要使用逗号将它们分隔开。
举例
以下是关于每个 filter_path 的 Elasticsearch REST API 示例,展示如何使用这些过滤参数:
filter_path=took
仅返回请求所花费的时间。请求:
1
GET /my_index/_search?filter_path=took
响应:
1
2
3{
"took": 15
}filter_path=items._id,items._index
仅返回 items 中的 _id 和 _index 字段。请求:
1
2
3
4
5
6
7
8
9
10
11
12
13POST /_bulk?filter_path=items._id,items._index
{
"index": { "_index": "test", "_id": "1" }
}
{
"field1": "value1"
}
{
"index": { "_index": "test", "_id": "2" }
}
{
"field1": "value2"
}响应
1
2
3
4
5
6{
"items": [
{ "_id": "1", "_index": "test" },
{ "_id": "2", "_index": "test" }
]
}filter_path=items.*.error
返回 items 中所有包含 error 字段的内容。请求:
1
2
3
4
5
6
7
8
POST /_bulk?filter_path=items.*.error
{
"update": { "_id": "1", "_index": "nonexistent_index" }
}
{
"doc": { "field1": "value1" }
}响应:
1
2
3
4
5
6
7
8
9
10
11
12{
"items": [
{
"update": {
"error": {
"type": "index_not_found_exception",
"reason": "no such index [nonexistent_index]"
}
}
}
]
}filter_path=hits.hits._source
仅返回搜索结果中原始文档内容。请求:
1
2
3
4GET /my_index/_search?filter_path=hits.hits._source
{
"query": { "match_all": {} }
}响应:
1
2
3
4
5
6
7
8{
"hits": {
"hits": [
{ "_source": { "field1": "value1" } },
{ "_source": { "field2": "value2" } }
]
}
}filter_path=_shards, hits.total
返回关于分片信息和总命中数。请求:
1
2
3
4
5
GET /my_index/_search?filter_path=_shards,hits.total
{
"query": { "match": { "field1": "value" } }
}响应:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10,
"relation": "eq"
}
}
}filter_path=aggregations.*.value
仅返回每个聚合的值。请求:
1
2
3
4
5
6
7
8
9
GET /my_index/_search?filter_path=aggregations.*.value
{
"aggs": {
"avg_price": { "avg": { "field": "price" } },
"max_price": { "max": { "field": "price" } }
},
"size": 0
}响应:
1
2
3
4
5
6{
"aggregations": {
"avg_price": { "value": 100.5 },
"max_price": { "value": 200 }
}
}