elasticsearch使用脚本 滚动关闭索引,更新index setting
一 问题
在旧的索引中更新mapping时,新增了分词器(分词器已经在模板中添加),但是在更新mapping时报错:
查看elasticsearch官网,发现不允许在已经存在的索引中动态更新分词器,只能先将索引close,更新分词器,然后再打开
Update index settings API | Elasticsearch Guide [8.3] | Elastic
You can only define new analyzers on closed indices.To add an analyzer, you must close the index, define the analyzer, and reopen the index. |
二 问题解决方式步骤(已经验证)
2.1 由暂停数据写入&关闭分片分配
暂停数据写入,可以避免恢复阶段translog中大量数据回放,提升索引恢复速度。
关闭分片分配
PUT _cluster/settings{ "persistent" : { "cluster.routing.rebalance.enable": "none" } } |
2.2 对所有需要进行更新分词器的索引执行flush操作。
作用是将内存buffer flush到硬盘中,避免恢复阶段translog中大量数据回放,提升索引恢复速度。
(1)获取所有索引
GET /_cat/indices
(2)获取指定索引对应的setting
GET /moka.prod_candidatev2_chunk_{i}/_settings
根据setting内容判断索引是否包含分词器 back_edge_ngram_analyzer word_analyzer 作为更新索引判断依据
(3)对指定索引进行flush操作以及sync操作,加速恢复,避免从主分片全量拉取数据同步至副本。
POST /index{i}/_flush
POST /index{i}/_flush/synced
2.3 对索引进行关闭操作
POST /index{i}/_close
2.4 更新索引分词器
PUT index{i}/_settings {"analysis": {"filter":{"back_edge_ngram_filter": {"min_gram": "1","side": "back","type": "edgeNGram","max_gram": "256"}},"analyzer": {"back_edge_ngram_analyzer": {"filter": ["standard","lowercase","back_edge_ngram_filter"],"tokenizer": "keyword"},"word_analyzer": {"filter": ["standard","lowercase"],"tokenizer": "keyword"},"mk_edge2": { "type": "mk_edge","extra_prarm": "cb,3,-1"},"mk_edge1": { "type": "mk_edge","extra_prarm": "wb,1,-1"}}}} |
2.5 轮询等待集群恢复green状态
GET _cluster/health
2.6 重复2.2至2.5步骤,直至所有索引恢复
python处理脚本:
import timefrom tqdm import trangefrom elasticsearch import ElasticsearchES_HOST = ["http://elastic:"]client = Elasticsearch(ES_HOST)# index_pattern = "thoth_candidatev2_chunk_*"
index_pattern = "moka.prod_candidatev2_chunk_*"
# index_pattern = "test_candidatev2_chunk_*"put_all_index = Falsedef main():all_indices = get_all_indices()print("Number of indices: ", len(all_indices))all_indices = [index for index in all_indices if not is_updated_index_settings(index) ]print("Number of not updated indices: ", len(all_indices))for index in all_indices:if put_all_index or select():print(f"Start put {index} settings")put_index_settings(index)check_cluster_health(index)else:breakprint('Finished')def select():global put_all_indextext = input("continue(all/y/n): ")if text == 'y':return Trueelif text == 'n':return Falseelif text == 'all':put_all_index = Truereturn Truedef is_updated_index_settings(index):settings = client.indices.get_settings(index=index)analysis = settings[index]["settings"]["index"]["analysis"]if "word_analyzer" in analysis["analyzer"] and "back_edge_ngram_analyzer" in analysis["analyzer"] and "back_edge_ngram_filter" in analysis["filter"]:print(f"{index} done")return Trueelse:return Falsedef put_index_settings(index):if client.cat.indices(index=index,params={"h": "status"}).strip() != 'close':print(f"flush {index}")client.indices.flush(index=index)print(f"flush {index} done")close_index(index)body = '{"analysis":{"filter":{"back_edge_ngram_filter":{"min_gram":"1","side":"back","type":"edgeNGram","max_gram":"256"}},"analyzer":{"back_edge_ngram_analyzer":{"filter":["standard","lowercase","back_edge_ngram_filter"],"tokenizer":"keyword"},"word_analyzer":{"filter":["standard","lowercase"],"tokenizer":"keyword"},"mk_edge2":{"type":"mk_edge","extra_prarm":"cb,3,-1"},"mk_edge1":{"type":"mk_edge","extra_prarm":"wb,1,-1"}}}}'client.indices.put_settings(index=index, body=body)if not is_updated_index_settings(index):print(f"put index error: {index}")put_index_settings(index)open_index(index)def close_index(index):print(f"{index} status: ", client.cat.indices(index=index,params={"h": "status"}).strip())client.indices.close(index)print(f"{index} status: ", client.cat.indices(index=index,params={"h": "status"}).strip())def open_index(index):print(f"{index} status: ", client.cat.indices(index=index,params={"h": "status"}).strip())client.indices.open(index)print(f"{index} status: ", client.cat.indices(index=index,params={"h": "status"}).strip())def check_cluster_health(index):t = trange(100, desc="recover: ", leave=True)last_progress = 0while client.cluster.health()["status"] != "green":t.set_description(client.cluster.health()["status"])current_progress = client.cluster.health()["active_shards_percent_as_number"]t.update(current_progress - last_progress)last_progress = current_progressrecovery_status = client.cat.recovery(index=index, params={"h": "index,shard,translog_ops_percent,files_percent,stage", "v": "true"})output = []for idx, item in enumerate(recovery_status.split('\n')):if idx == 0:output.append(item)else:output.append(item) if not item.endswith('done') else Noneif len(output) > 1:print('\n'.join(output))time.sleep(2)t.set_description(client.cluster.health()["status"])t.update(100 - last_progress)t.close()def get_all_indices():return client.indices.get_alias(index_pattern) def test_put_index():index = "test_candidatev2_chunk_{chunk}"body = ''for chunk in range(0, 10):client.indices.create(index=index.format(chunk=chunk), body=body)if __name__ == "__main__":# test_put_index()main()
elasticsearch使用脚本 滚动关闭索引,更新index setting相关推荐
- 高效管理 Elasticsearch 中基于时间的索引——本质是在利用滚动模式做数据的冷热分离,热索引可以用ssd...
高效管理 Elasticsearch 中基于时间的索引 转自:http://stormluke.me/es-managing-time-based-indices-efficiently/ 用 Ela ...
- ElasticSearch优化系列三:索引过程
大家可能会遇到索引数据比较慢的过程.其实明白索引的原理就可以有针对性的进行优化.ES索引的过程到相对Lucene的索引过程多了分布式数据的扩展,而这ES主要是用tranlog进行各节点之间的数据平衡. ...
- elasticsearch删除索引_一文带您了解 Elasticsearch 中,如何进行索引管理(图文教程)
在 Elasticsearch 中,索引是一个非常重要的概念,它是具有相同结构的文档集合.类比关系型数据库,比如 Mysql, 你可以把它对标看成和库同级别的概念. 今天小哈将带着大家了解, 在 El ...
- Elasticsearch基础(三)索引和文档操作
1.api种类 1.1 TransportClient 是Elasticsearch官方的api TransportClient可以支持2.x,5.x版本,TransportClient将会在Elas ...
- elasticsearch实战三部曲之一:索引操作
从本章开始,我们一起来实战elasticsearch,熟悉相关操作和命令,为后续的深入学习打好基础: 三部曲介绍 整个系列由以下三篇文章构成: 索引操作实战,也就是本文的主要内容: 文档操作实战: 搜 ...
- SQL Server 性能调优3 之索引(Index)的维护
SQL Server 性能调优3 之索引(Index)的维护 热度1 评论 16 作者:溪溪水草 SQL Server 性能调优3 之索引(Index)的维护 前言 前一篇的文章介绍了通过建立索引来提 ...
- ElasticSearch-Hadoop:从Hadoop到ElasticSearch的产品视图计数索引和客户顶部搜索查询...
这篇文章涵盖了如何使用ElasticSearch-Hadoop从Hadoop系统读取数据并在ElasticSearch中对其进行索引. 它涵盖的功能是在最近n天中为每个客户的产品浏览量计数和热门搜索查 ...
- Elasticsearch是如何做到快速索引的
最近在参与一个基于Elasticsearch作为底层数据框架提供大数据量(亿级)的实时统计查询的方案设计工作,花了些时间学习Elasticsearch的基础理论知识,整理了一下,希望能对Elastic ...
- coreseek实时索引更新之增量索引
coreseek实时索引更新有两种选择: 1.使用基于磁盘的索引,手动分区,然后定期重建较小的分区(被称为"增量").通过尽可能的减小重建部分的大小,可以将平均索引滞后时间降低到3 ...
最新文章
- 【性能优化】纳尼?内存又溢出了?!是时候总结一波了!!
- 哈佛牙学院博士后:教你口腔保健基本功之刷牙篇
- 【深度学习笔记】深度学习中关于epoch
- 各种SQL在Pig中实现
- Java 单列模式(Singleton)
- sql floor 取整函数
- Anaconda :利用Anaconda Prompt (Anaconda3)建立、设计不同python版本及对应库函数环境之详细攻略
- SQL Server游标的使用
- JS实现2,8,10,16进制的相互转换
- SAP登陆界面TITLE修改方法(Method of SAP Logon GUI Title Modification)
- 笔记 英语二 考研先导课 0126
- python lxml解析html,当使用lxml.html解析html时,等同于InnerHTML
- jquert ajax文件 mvc,jquery ajax file upload NET MVC 无刷新文件上传
- 诺顿无法启动扫描,扫描引擎返回错误0x20000058错误
- c语言模拟键盘自动按键,C语言实现模拟键盘按键事件
- c语言语法口诀,一般过去时语法口诀
- c语言实现小球跳动的效果
- python化学模拟_rdkit 化学反应高级功能
- 获取jqGrid中每行的子元素td并修改其属性
- MAC 无法重启或关机