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

elasticsearch批量操作的更多相关文章

  1. Python Elasticsearch批量操作客户端

    基于Python实现的Elasticsearch批量操作客户端 by:授客 QQ:1033553122   1. 代码用途 1 2. 测试环境 1 3. 使用方法 1 3.1 配置ES服务器信息 1 ...

  2. Elasticsearch批量操作API用法介绍

    Elasticsearch的Bulk API允许批量提交index和delete请求,有如下两种用法: 用法1 BulkRequestBuilder requestBuilder = client.p ...

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

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

  4. Elasticsearch(5)--- 基本命令(集群相关命令、索引CRUD命令、文档CRUD命令)

    Elasticsearch(5)--- 基本命令 这篇博客的命令分为ES集群相关命令,索引CRUD命令,文档CRUD命令.这里不包括Query查询命令,它单独写一篇博客. 一.ES集群相关命令 ES集 ...

  5. 第三百六十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mget和bulk批量操作

    第三百六十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mget和bulk批量操作 注意:前面讲到的各种操作都是一次http请求操作一条数据,如果想 ...

  6. Elasticsearch之CURL命令的bulk批量操作

    大家,也可去看看我下面的博客 Elasticsearch之批量操作bulk 官网上,是举例了新建一个requests文件. [hadoop@master elasticsearch-]$ pwd /h ...

  7. Elasticsearch之批量操作bulk

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

  8. ElasticSearch(二):文档的基本CRUD与批量操作

    ElasticSearch(二):文档的基本CRUD与批量操作 学习课程链接<Elasticsearch核心技术与实战> Create 文档 支持自动生成文档_id和指定文档_id两种方式 ...

  9. 【Elasticsearch 7 探索之路】(二)文档的 CRUD 和批量操作

    上一篇,我们介绍了什么是 Elasticsearch,它能做什么用以及基本概念(索引 Index.文档 Document.类型 Type)理解.这篇主要对 文档的基本 CRUD 和 倒排索引进行讲解. ...

随机推荐

  1. VirtualBox虚拟机下 解决centos系统无法上网的问题

    首先,在VirtualBox中设置网卡连接方式:点“设置”,在弹出的界面中点“网络”,最后选择“连接方式”为“桥接网卡”或者网都可以络地址转换  这两种我试了试都可以 接下来修改一个文件就行: 1.打 ...

  2. hibernate反向生成

    1.在Myeclipse Database Explore中创建一个库的链接. 2.在Myeclipse中创建一个web工程,添加反向生成相关的引用.即在项目上右键,弹出Myeclipse选择add  ...

  3. cocos2d-android学习四 ---- 精灵的创建

    上篇文章我们创建了一个黑乎乎的界面.以下我们就给它增加一个精灵. 我们这次就一起来学习精灵的基础知识. 1.什么是精灵 游戏中全部会动的对象都是精灵,能够是主人公,背景元素,一个子弹或者是敌人. 一个 ...

  4. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 总结

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers  总结         本章节向你介绍了SP平台上eve ...

  5. 【有奖征资源,分享有内涵】贡献你的CSDN博文和下载资源,不断更新中

    我们收集了CSDN热心博主的博文 和相关下载资源.这些可爱博主上传了免积分的CSDN资源,并贡献了相关的用法,改进策略,进行了翔实的分析.感谢博主的贡献.并期待有很多其它这种好人! 我们特推出活动&q ...

  6. 【转】坑爹的AsyncTask之根本停不下来

    原文网址:http://www.jianshu.com/p/0c6f4b6ed558 上篇<坑爹的AsyncTask之内存泄露>已经简单的探讨过线程使用不当会造成内存泄露的问题,在Acti ...

  7. 【BZOJ1597】【Usaco2008 Mar】土地购买 斜率优化DP

    题目: 题目在这里 思路与做法: 这题如果想要直接dp的话不太好处理. 不过, 我们发现如果\(a[i].x>=a[j].x\)且\(a[i].y>=a[j].y\) \((\)a是输入的 ...

  8. C# 开放式并发冲突报错处理

    1.调用DataSet.GetChanges()获取数据源中改变的数据 var data = ViewData.GetChanges() as ReleaseData; 2.为新增的数据 data 加 ...

  9. golang二维码

    package main import ( "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr ...

  10. C - Young Physicist

    Problem description A guy named Vasya attends the final grade of a high school. One day Vasya decide ...