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

批量查询

1.如果查询的document是不同index下的不同type种的话

GET /_mget
{
   "docs" : [
      {
         "_index" : "test_index",
         "_type" :  "test_type",
         "_id" :    1
      },
      {
         "_index" : "test_index",
         "_type" :  "test_type",
         "_id" :    2
      }
   ]
}

2.如果查询的document是一个index下的不同type种的话

GET /test_index/_mget
{
   "docs" : [
      {
         "_type" :  "test_type",
         "_id" :    1
      },
      {
         "_type" :  "test_type",
         "_id" :    2
      }
   ]
}

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

GET /test_index/test_type/_mget
{
   "ids": [1, 2]
}

mget的重要性:

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

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

bulk语法

  bulk api对json的语法,有严格的要求,每个json串不能换行,只能放一行,同时一个json串和一个json串之间,必须有一个换行

  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"} }

第二种

POST /test_index/_bulk
{ "delete": { "_type": "test_type", "_id": "3" }} 
{ "create": { "_type": "test_type", "_id": "12" }}
{ "test_field":    "test12" }
{ "index":  { "_type": "test_type" }}
{ "test_field":    "auto-generate id test" }
{ "index":  { "_type": "test_type", "_id": "2" }}
{ "test_field":    "replaced test2" }
{ "update": { "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }

第三种

POST /test_index/test_type/_bulk
{ "delete": { "_id": "3" }} 
{ "create": { "_id": "12" }}
{ "test_field":    "test12" }
{ "index":  { }}
{ "test_field":    "auto-generate id test" }
{ "index":  { "_id": "2" }}
{ "test_field":    "replaced test2" }
{ "update": { "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }

bulk size最佳大小

  bulk request会加载到内存里,如果太大的话,性能反而会下降,因此需要反复尝试一个最佳的bulk size。一般从1000~5000条数据开始,尝试逐渐增加。另外,如果看大小的话,最好是在5~15MB之间。

转载于:https://www.cnblogs.com/cnki/p/7507309.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个基于Lucene的分布式搜索和分析引擎,其源码解析与优化可以帮助我们更好地理解和利用Elasticsearch的功能和性能。下面是对Elasticsearch源码解析与优化实战的一些探讨: 1. 源码解析: - 阅读和理解Elasticsearch的核心模块、数据结构和算法,如索引、搜索、分片、路由等。 - 掌握Elasticsearch的分布式架构,包括节点之间的通信、协调和负载均衡等机制。 - 理解Elasticsearch的查询语法和DSL,了解查询执行的流程和原理。 - 深入研究Elasticsearch使用的Lucene库,掌握其索引和搜索的底层实现细节。 2. 性能优化: - 通过源码分析,找到性能瓶颈所在,如磁盘IO、网络通信、GC等。 - 针对性优化各个模块的代码,比如索引模块、搜索模块、聚合模块等。 - 优化查询性能,例如通过合理的查询DSL设计、索引优化、缓存策略等提升查询效率。 - 配置优化,如调整线程池大小、内存限制、缓冲区设置等,以平衡性能和资源消耗之间的关系。 3. 扩展与定制: - 根据业务需求,定制Elasticsearch的功能和行为,比如自定义插件、脚本、分析器等。 - 扩展Elasticsearch的集群规模和数据容量,实现水平扩展和负载均衡。 - 集成其他工具和技术,如Logstash、Kibana、Beats等,以构建完整的数据处理和可视化平台。 需要注意的是,对于大部分用户而言,直接阅读和修改Elasticsearch源码并不是必要的,因为Elasticsearch提供了丰富的配置选项和插件机制,可以通过合理的配置和扩展来满足大部分需求。源码解析与优化更多地是为那些对Elasticsearch内部工作原理有深入需求的开发人员和运维人员而言。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值