1、批量查询的好处

就是一条一条的查询,比如说要查询100条数据,那么就要发送100次网络请求,这个开销还是很大的

如果进行批量查询的话,查询100条数据,就只要发送1次网络请求,网络请求的性能开销缩减100倍

mget的语法

mget批量查询

GET /_mget

{

"docs" : [

{

"_index" : "test_index",

"_type" :  "test_type",

"_id" :    1

},

{

"_index" : "test_index",

"_type" :  "test_type",

"_id" :    2

}

]

}

{

"docs": [

{

"_index": "test_index",

"_type": "test_type",

"_id": "1",

"_version": 2,

"found": true,

"_source": {

"test_field1": "test field1",

"test_field2": "test field2"

}

},

{

"_index": "test_index",

"_type": "test_type",

"_id": "2",

"_version": 1,

"found": true,

"_source": {

"test_content": "my test"

}

}

]

}

(3)如果查询的document是一个index下的不同type种的话

GET /test_index/_mget

{

"docs" : [

{

"_type" :  "test_type",

"_id" :    1

},

{

"_type" :  "test_type",

"_id" :    2

}

]

}

(4)如果查询的数据都在同一个index下的同一个type下,最简单了

GET /test_index/test_type/_mget

{

"ids": [1, 2]

}

3、mget的重要性

可以说mget是很重要的,一般来说,在进行查询的时候,如果一次性要查询多条数据的话,那么一定要用batch批量操作的api

尽可能减少网络开销次数,可能可以将性能提升数倍,甚至数十倍,非常非常之重要

bulk语法

POST /_bulk

{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "3" }}

{ "create": { "_index": "test_index", "_type": "test_type", "_id": "12" }}

{ "test_field":    "test12" }

{ "index":  { "_index": "test_index", "_type": "test_type", "_id": "2" }}

{ "test_field":    "replaced test2" }

{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }

{ "doc" : {"test_field2" : "bulk test1"} }

每一个操作要两个json串,语法如下:

{"action": {"metadata"}}

{"data"}

举例,比如你现在要创建一个文档,放bulk里面,看起来会是这样子的:

{"index": {"_index": "test_index", "_type", "test_type", "_id": "1"}}

{"test_field1": "test1", "test_field2": "test2"}

有哪些类型的操作可以执行呢?

(1)delete:删除一个文档,只要1个json串就可以了

{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "3" }}

(2)create:PUT /index/type/id/_create,强制创建

{ "create": { "_index": "test_index", "_type": "test_type", "_id": "12" }}

{ "test_field":    "test12" }

(3)index:普通的put操作,可以是创建文档,也可以是全量替换文档

{ "index":  { "_index": "test_index", "_type": "test_type", "_id": "2" }}

{ "test_field":    "replaced test2" }

(4)update:执行的partial update操作

{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }

{ "doc" : {"test_field2" : "bulk test1"} }

注意:

bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会告诉你异常日志。bulk request会加载到内存里,如果太大的话,性能反而会下降,因此需要反复尝试一个最佳的bulk size。一般从1000~5000条数据开始,尝试逐渐增加。另外,如果看大小的话,最好是在5~15MB之间。

multi-index和multi-type搜索模式

告诉你如何一次性搜索多个index和多个type下的数据

/_search:所有索引,所有type下的所有数据都搜索出来

/index1/_search:指定一个index,搜索其下所有type的数据

/index1,index2/_search:同时搜索两个index下的数据

/test1_*,test2_*/_search:按照通配符去匹配多个索引

/index1/type1/_search:搜索一个index下指定的type的数据

/index1/type1,type2/_search:可以搜索一个index下多个type的数据

/index1,index2/type1,type2/_search:搜索多个index下的多个type的数据

/_all/type1,type2/_search:_all,可以代表搜索所有index下的指定type的数据

分页搜索

将这9条数据分成3页,每一页是3条数据

GET /test_index/test_type/_search?from=0&size=3

转载于:https://www.cnblogs.com/kesimin/p/9559958.html

elasticsearch批量操作相关推荐

  1. 用python批量更新es数据根据id_Python Elasticsearch批量操作客户端

    基于Python实现的Elasticsearch批量操作客户端 by:授客 QQ:1033553122 1.代码用途 Elasticsearch客户端,目的在于实现批量操作,如下: <1> ...

  2. 跟乐乐学ES!(三)ElasticSearch 批量操作与高级查询

    上一篇文章:跟乐乐学ES!(二)ElasticSearch基础. 下一篇文章:跟乐乐学ES!(四) java中ElasticSearch客户端的使用. 批量操作 有些增删改查操作是可以进行批量操作的, ...

  3. Elasticsearch技术解析与实战(七)Elasticsearch批量操作

    批量查询 1.如果查询的document是不同index下的不同type种的话 GET /_mget {"docs" : [{"_index" : " ...

  4. elasticsearch批量操作之bulk

    文章目录 前言 一.bulk概述 1.1 基础概念 1.2 bulk语法 1.3 操作类型 二.使用示例 2.1 批量插入 2.2 批量更新 2.3 批量删除 总结 前言 本篇文章主要总结如何使用bu ...

  5. 《深入理解ElasticSearch》——2.4 批量操作

    本节书摘来自华章计算机<深入理解ElasticSearch>一书中的第2章,第2.4节,作者:[美] 拉斐尔·酷奇(Rafa Ku) 马雷克·罗戈任斯基(Marek Rogoziński) ...

  6. Elasticsearch之批量操作bulk

    1.bulk相当于数据库里的bash操作. 2.引入批量操作bulk,提高工作效率,你想啊,一批一批添加与一条一条添加,谁快? 3.bulk API可以帮助我们同时执行多个请求 4.bulk的格式: ...

  7. elasticsearch索引的初始化操作以及marvel操作(增删改查),批量查询_mget,批量操作_bulk

    文中的简洁版都是使用marvel操作的 安装marvel插件的教程 https://blog.csdn.net/u013294097/article/details/100144725 1.创建索引之 ...

  8. python elasticsearch bulk_Elasticsearch之批量操作bulk

    1.bulk相当于数据库里的bash操作. 2.引入批量操作bulk,提高工作效率,你想啊,一批一批添加与一条一条添加,谁快? 3.bulk API可以帮助我们同时执行多个请求 4.bulk的格式: ...

  9. elasticsearch 基础 —— _mget取回多个文档及_bulk批量操作

    取回多个文档 Elasticsearch 的速度已经很快了,但甚至能更快. 将多个请求合并成一个,避免单独处理每个请求花费的网络延时和开销. 如果你需要从 Elasticsearch 检索很多文档,那 ...

最新文章

  1. ASP.NET图象处理详解
  2. pandas为dataframe添加新的数据行(rows)、在dataframe后面纵向添加一行数据(数据为列表list形式)、列有不匹配将会使用NA值进行填补
  3. ORA-04031 错误
  4. 银联Apple Pay 总结
  5. ideajava目录显示类成员_面试:C++不可继承类
  6. 第十一章 图形视图、动画、状态机框架
  7. c语言编写ocr软件,开源OCR引擎Tesseract
  8. 第2关:HTML结构:自我简介网页
  9. 计算机炫酷功能,【实用】上班族必备!10个实用电脑炫酷小技巧~
  10. 谷歌主页浏览器被流氓软件绑架设置成好123或者7456怎么办
  11. python 知乎 合并 pdf_有什么比较好的 PDF 分割、合并软件?
  12. 名帖337 张旭 草书《古诗四帖》
  13. 最大公约数和最小公倍数,你知道有几种求法吗?
  14. TA游戏推荐:Android益智游戏《戳青蛙》
  15. python dataframe去掉索引_python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)...
  16. PL/SQL批量运行SQL语句
  17. 历年数学建模大赛优秀论文解读
  18. 快速排序-QuickSort
  19. android人脸识别应用架构,基于Android平台人脸识别系统的设计与实现
  20. 国家政策!大有广阔天地的商城结算平台

热门文章

  1. java红黑树_JAVA学习-红黑树详解
  2. css 图片居中放大,不同比例图片居中缩放显示的三种方法
  3. linux maps,linux下/proc/pid/maps和pmap命令详解
  4. matlab 多项式拟合 ployval
  5. 固态器件理论(10)半导体制造技术
  6. 读论文之《基于EV10AQ190的高速ADC接口设计》
  7. 14、四大组件--Service
  8. C++11:POD数据类型
  9. Eclipse中配置Tomcat
  10. 新概念英语(1-73)The way to King Street