{"type":"doc","content":[{"type":"heading","attrs":{"id":"8d50aa2b-cfe9-4288-a757-beef6b82a0a4","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"说明"}]},{"type":"paragraph","attrs":{"id":"59d81a61-4327-412c-8197-5376933c5d5f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"本文描述问题及解决方法同样适用于 "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://cloud.tencent.com/document/product/845?from=20421&from_column=20421","target":"_blank","rel":"noopener noreferrer nofollow","class":""}}],"text":"腾讯云 Elasticsearch Service(ES)"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"id":"f6bd4785-b4ee-47df-a21a-675ebbfcc7df","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"adb12145-6d18-46ed-9361-aacf7bfefc59","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"id":"0faca12c-df94-42b3-8db1-aa0a3f8c754e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"最近在回顾一些曾经在 ES 上踩过的坑,想起了这么一个问题:Elasticsearch 索引突然无法写入新文档了,日志里报错说文档数量超过了限制。这个问题在 7.x 版本的集群中尤其常见,因为从 7.x 开始,Elasticsearch 默认索引只有 1 个分片。今天就来回顾一下这个问题的来龙去脉。"}]},{"type":"paragraph","attrs":{"id":"57cb99d6-56bd-47d4-a2c3-b197712fd8ff","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"9cb2b0be-962d-4934-9176-004d29f1e398","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"问题现象"}]},{"type":"paragraph","attrs":{"id":"db0673ff-ccda-4606-b250-d6148679a870","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"当索引的单个分片文档数达到上限时,会看到类似这样的错误:"}]},{"type":"codeBlock","attrs":{"id":"e79aa74d-bf36-496c-9079-44fab63426e1","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"Number of documents in the index can't exceed [2147483519]"}]},{"type":"paragraph","attrs":{"id":"d797d960-bfca-4b1e-8297-dd63ca313466","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"这个数字看起来很眼熟对吧?没错,它非常接近 Java 的"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"code"}],"text":"Integer.MAX_VALUE"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"(2147483647)。实际上,这个限制来自于 Lucene 的"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"code"}],"text":"IndexWriter.MAX_DOCS"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"。"}]},{"type":"heading","attrs":{"id":"d3edf961-e0f3-4c3c-80f4-ca14495cc866","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"为什么会有这个限制?"}]},{"type":"paragraph","attrs":{"id":"98e09b4b-a6c3-40ff-9ac6-3382660da1a1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"这个限制其实是 Lucene 层面的硬性约束。我们来看看源码中的关键部分:"}]},{"type":"paragraph","attrs":{"id":"bcb2fb79-be23-4fb1-88dc-bea2e6c35c43","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"在"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"code"}],"text":"InternalEngine.java"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"中,有这样一段代码:"}]},{"type":"codeBlock","attrs":{"id":"c293d748-ac9f-4e3e-b769-d6fabd53d0a3","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"private Exception tryAcquireInFlightDocs(Operation operation, int addingDocs) {\n assert operation.origin() == Operation.Origin.PRIMARY : operation;\n assert operation.seqNo() == SequenceNumbers.UNASSIGNED_SEQ_NO : operation;\n assert addingDocs > 0 : addingDocs;\n final long totalDocs = indexWriter.getPendingNumDocs() + inFlightDocCount.addAndGet(addingDocs);\n if (totalDocs > maxDocs) {\n releaseInFlightDocs(addingDocs);\n return new IllegalArgumentException(\"Number of documents in the index can't exceed [\" + maxDocs + \"]\");\n } else {\n return null;\n }\n}\n"}]},{"type":"paragraph","attrs":{"id":"9777ef5a-99f2-43db-8cd5-9dab579edea2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"这里的"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"code"}],"text":"maxDocs"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"默认值就是"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"code"}],"text":"IndexWriter.MAX_DOCS"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":",也就是"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"bold"}],"text":"2147483519"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"(约 21.47 亿)。"}]},{"type":"paragraph","attrs":{"id":"40f4ca5b-7117-42cf-a569-7eb127b37d06","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"10b40495-d486-4ec0-b747-bd5ba8253826","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"为什么 7.x 版本更容易遇到?"}]},{"type":"paragraph","attrs":{"id":"a1d5e0fc-9be5-4ce0-9d13-948b57ffb155","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"在 Elasticsearch 7.x 之前,默认索引会创建 5 个主分片。但从 7.x 开始,为了优化资源使用和提升性能,"},{"type":"text","marks":[{"type":"bold"}],"text":"默认改为只创建 1 个主分片"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"。"}]},{"type":"paragraph","attrs":{"id":"85aa097e-771e-436a-9c0e-50fae285d553","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"这意味着:"}]},{"type":"bulletList","attrs":{"id":"32632fca-f105-484a-bdca-fc84a841118a","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"72bec24c-3617-4ebd-b2b0-f08257c30eaa"},"content":[{"type":"paragraph","attrs":{"id":"3d322744-ee10-4d09-b8dc-b618be245fb4","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"6.x 及之前"},{"type":"text","text":":21.47 亿 × 5 = 约 107 亿文档才会遇到问题"}]}]},{"type":"listItem","attrs":{"id":"90a97c63-a5b3-46b7-a393-f8defdc0beeb"},"content":[{"type":"paragraph","attrs":{"id":"f65695b1-c741-4c4c-979b-c991a35e013f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"7.x 及之后"},{"type":"text","text":":21.47 亿文档就会遇到问题"}]}]}]},{"type":"paragraph","attrs":{"id":"cda55da2-f3f5-45e1-873c-7e060883eda2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"对于日志类、时序类数据,如果没有合理规划索引策略,很容易就会触碰到这个天花板。"}]},{"type":"paragraph","attrs":{"id":"79b3cc32-acac-4429-8955-290559309fed","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"661ff8bb-4ac7-4049-8fc0-e67596ac023f","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"代码流程分析"}]},{"type":"paragraph","attrs":{"id":"8fe454ab-78b2-4c86-b513-7150ad673f08","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"让我们通过一个流程图来理解文档写入时的检查机制:"}]},{"type":"image","attrs":{"id":"80fa67b0-7a73-45f4-805f-10d3b8b13aab","src":"https://developer.qcloudimg.com/http-save/audit-7348459/3f99760546fac7257cd55cb24e924afb.png","extension":"png","align":"left","alt":"","showAlt":false,"href":"","boxShadow":"","width":433,"aspectRatio":"0.472064","status":"success","showText":true,"isPercentage":false,"percentage":0,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"0735919c-8bbd-4186-b87a-74573353cad4","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"关键代码解析"}]},{"type":"heading","attrs":{"id":"885e4fda-0515-4140-bd9b-e7f2c7aa4983","textAlign":"inherit","indent":0,"level":4,"isHoverDragHandle":false},"content":[{"type":"text","text":"1. 文档数量检查(InternalEngine.java)"}]},{"type":"codeBlock","attrs":{"id":"f6f682bb-026c-44ae-b9d0-a2c91e251e39","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"private final AtomicLong inFlightDocCount = new AtomicLong();\nprivate final int maxDocs;\n\npublic InternalEngine(EngineConfig engineConfig) {\n this(engineConfig, IndexWriter.MAX_DOCS, LocalCheckpointTracker::new);\n}\n"}]},{"type":"paragraph","attrs":{"id":"5f1ee08f-f2e1-460d-818d-41929b2d5734","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"引擎初始化时,"},{"type":"text","marks":[{"type":"code"}],"text":"maxDocs"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"被设置为"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"code"}],"text":"IndexWriter.MAX_DOCS"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"。在每次写入操作前,都会检查:"}]},{"type":"codeBlock","attrs":{"id":"5a372db2-d3da-41d6-97c2-d24f2cc0a0f9","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"final long totalDocs = indexWriter.getPendingNumDocs() + inFlightDocCount.addAndGet(addingDocs);\nif (totalDocs > maxDocs) {\n // 超限,拒绝写入\n}\n"}]},{"type":"paragraph","attrs":{"id":"fc5a9ad1-0ed2-499b-9ddd-ded35fc617c2","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"这里有两个计数器:"}]},{"type":"bulletList","attrs":{"id":"1c66196d-38fa-4d23-91d3-2c91ce1a77ca","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"28f011db-9f5e-4227-b3ad-c5eb8541ff0a"},"content":[{"type":"paragraph","attrs":{"id":"df5aeaa0-0764-485b-9c5b-6cb06e9b3088","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"code"}],"text":"indexWriter.getPendingNumDocs()"},{"type":"text","text":":IndexWriter 中待提交的文档数"}]}]},{"type":"listItem","attrs":{"id":"6a254131-32ea-4195-a5d9-3bc524e304f1"},"content":[{"type":"paragraph","attrs":{"id":"e7536d28-4010-443b-9a81-2ac967820d51","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"code"}],"text":"inFlightDocCount"},{"type":"text","text":":正在处理中但还未提交到 IndexWriter 的文档数"}]}]}]},{"type":"heading","attrs":{"id":"c9cacbf2-2c05-47a5-b83c-f72cc44c3416","textAlign":"inherit","indent":0,"level":4,"isHoverDragHandle":false},"content":[{"type":"text","text":"2. Shrink 操作的检查(TransportResizeAction.java)"}]},{"type":"paragraph","attrs":{"id":"db1c083e-8694-4865-8569-71fb491cdf0e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"在执行索引收缩(shrink)操作时,也会检查合并后的分片是否会超过限制:"}]},{"type":"codeBlock","attrs":{"id":"bb51671a-f13c-4362-a1e3-0896980e6b74","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"for (int i = 0; i < numShards; i++) {\n if (resizeRequest.getResizeType() == ResizeType.SHRINK) {\n Set<ShardId> shardIds = IndexMetadata.selectShrinkShards(i, sourceMetadata, numShards);\n long count = 0;\n for (ShardId id : shardIds) {\n DocsStats docsStats = perShardDocStats.apply(id.id());\n if (docsStats != null) {\n count += docsStats.getCount();\n }\n if (count > IndexWriter.MAX_DOCS) {\n throw new IllegalStateException(\"Can't merge index with more than [\" + IndexWriter.MAX_DOCS\n + \"] docs - too many documents in shards \" + shardIds);\n }\n }\n }\n}\n"}]},{"type":"paragraph","attrs":{"id":"e856bd5a-de68-4580-bcfc-d329b22f805b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"这确保了在 shrink 操作时,不会将多个分片合并成一个超过限制的大分片。"}]},{"type":"paragraph","attrs":{"id":"d86754c2-3c5b-4f26-b86d-ebb540ec93fd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"5bd60b61-8325-4adc-a359-4c388fd6998f","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"解决方案"}]},{"type":"heading","attrs":{"id":"93b42ee0-8363-453a-bd4a-5d7a71b1d771","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"方案一:增加分片数(推荐用于新索引)"}]},{"type":"paragraph","attrs":{"id":"5c94498c-d983-45a4-a6b9-5ed8e0de7241","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"对于新创建的索引,可以在索引模板中设置合适的分片数:"}]},{"type":"codeBlock","attrs":{"id":"f8643cca-d126-44d7-96e2-4be474d7c350","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"PUT _index_template/my_template\n{\n \"index_patterns\": [\"logs-*\"],\n \"template\": {\n \"settings\": {\n \"number_of_shards\": 3,\n \"number_of_replicas\": 1\n }\n }\n}\n"}]},{"type":"paragraph","attrs":{"id":"7874089e-fb78-451c-bdfa-7bf619c959fb","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"注意"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":":这个设置只对新创建的索引生效,已存在的索引不会受影响。"}]},{"type":"heading","attrs":{"id":"4ef40b97-6f84-47f4-8416-f3a7f0f9806b","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"方案二:使用 Split API(用于已存在的索引)"}]},{"type":"paragraph","attrs":{"id":"c757a763-92ce-43aa-a449-f4690e6ea7ca","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"对于已经达到限制的索引,可以使用 Split API 来增加分片数:"}]},{"type":"codeBlock","attrs":{"id":"0e8b0482-7971-4538-b2f0-9ee80c19134b","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"# 1. 先将索引设置为只读\nPUT /my_index/_settings\n{\n \"settings\": {\n \"index.blocks.write\": true\n }\n}\n\n# 2. 执行 split 操作\nPOST /my_index/_split/my_index_new\n{\n \"settings\": {\n \"index.number_of_shards\": 3\n }\n}\n\n# 3. 等待 split 完成后,可以删除旧索引\nDELETE /my_index\n"}]},{"type":"paragraph","attrs":{"id":"baa2b8ab-c6f0-4585-b1bc-497287fc4024","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"重要提示"},{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":":"}]},{"type":"bulletList","attrs":{"id":"300b8633-5add-4611-91ed-86e62e097767","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"cb8bdb32-e980-43cc-8f8e-133001aaf999"},"content":[{"type":"paragraph","attrs":{"id":"ba1a0ef5-05d8-457b-aeaf-59528fe9c0c3","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Split 操作要求新分片数必须是原分片数的倍数"}]}]},{"type":"listItem","attrs":{"id":"fb09c0da-6964-438b-96d9-1c9d2da593e1"},"content":[{"type":"paragraph","attrs":{"id":"341f628a-2bcb-4da2-a0c1-dd5b3bbc16aa","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"Split 过程会消耗大量磁盘空间和 I/O 资源"}]}]},{"type":"listItem","attrs":{"id":"f83e8286-6684-439d-b9d5-d7a00261ec4c"},"content":[{"type":"paragraph","attrs":{"id":"1443c1fa-9f79-4cba-a831-6d03994a9640","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"建议在业务低峰期执行"}]}]}]},{"type":"heading","attrs":{"id":"5964643e-bcb8-4eb4-8f65-5ef4a6a4d2f2","textAlign":"inherit","indent":0,"level":3,"isHoverDragHandle":false},"content":[{"type":"text","text":"方案三:使用索引生命周期管理(ILM)"}]},{"type":"paragraph","attrs":{"id":"f3b2e090-4631-45f7-a462-f3f06294d545","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"对于日志类、时序类数据,最佳实践是使用 ILM 自动管理索引:"}]},{"type":"codeBlock","attrs":{"id":"34ee8aa1-48cd-4dd0-acfc-36411a1830d8","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"PUT _ilm/policy/logs_policy\n{\n \"policy\": {\n \"phases\": {\n \"hot\": {\n \"actions\": {\n \"rollover\": {\n \"max_docs\": 2000000000,\n \"max_age\": \"7d\",\n \"max_size\": \"50gb\"\n }\n }\n },\n \"delete\": {\n \"min_age\": \"30d\",\n \"actions\": {\n \"delete\": {}\n }\n }\n }\n }\n}\n"}]},{"type":"paragraph","attrs":{"id":"917d27c0-962f-4dc7-9003-98972c58ea26","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"这样可以在文档数接近限制前自动滚动到新索引,避免触碰天花板。"}]},{"type":"paragraph","attrs":{"id":"e98c74c2-9729-4051-841a-278bcf17500e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"dcb95542-9025-4ddd-be70-38ac6788da83","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"最佳实践建议"}]},{"type":"orderedList","attrs":{"id":"ed61b88c-8975-44c5-be4e-ea896e629027","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"28342026-dd87-4b43-b515-094ae86f4600"},"content":[{"type":"paragraph","attrs":{"id":"afa98304-7694-4f28-be29-7379eaeb2a8b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"合理规划分片数"},{"type":"text","text":":根据数据量和增长速度,预估合适的分片数。一般建议单个分片文档数控制在 10 亿以内。"}]}]},{"type":"listItem","attrs":{"id":"13618601-32ca-4f3b-a301-456397544923"},"content":[{"type":"paragraph","attrs":{"id":"d0c17eb3-9539-4aea-a12b-49664f00522b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"使用索引模板"},{"type":"text","text":":为不同类型的数据设置不同的索引模板,避免使用默认的 1 个分片。"}]}]},{"type":"listItem","attrs":{"id":"aaa4864c-40a9-4114-8628-106aa32d63fe"},"content":[{"type":"paragraph","attrs":{"id":"eaaa832c-48fb-4b4c-8e80-9ab346980f40","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"监控文档数量"},{"type":"text","text":":定期监控各分片的文档数,在接近限制前采取措施。可以使用以下 API:"}]}]}]},{"type":"codeBlock","attrs":{"id":"5af51b6b-7b0f-4cb7-97d9-734dbc88cbe5","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":""},"content":[{"type":"text","text":"GET /_cat/shards?v&h=index,shard,docs&s=docs:desc\n"}]},{"type":"orderedList","attrs":{"id":"787e58ea-47a5-4417-a15f-c26401fb668b","start":4,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"c3c7d970-cb17-42ca-9aa1-cdd7b72f2c00"},"content":[{"type":"paragraph","attrs":{"id":"16f8842f-06b0-41ab-82f4-75b46cc26ca7","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"时序数据使用 ILM"},{"type":"text","text":":对于日志、监控等时序数据,强烈建议使用 ILM 自动管理索引生命周期。"}]}]},{"type":"listItem","attrs":{"id":"c33b2265-36b7-43ae-a4b7-2fa005956a92"},"content":[{"type":"paragraph","attrs":{"id":"7be975a7-4891-480a-b74a-229baefefc9f","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"避免单分片过大"},{"type":"text","text":":即使没有达到文档数限制,单个分片过大(如超过 50GB)也会影响性能和恢复速度。"}]}]}]},{"type":"paragraph","attrs":{"id":"8ae17d86-4a0d-4640-b606-86c446d98163","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false}},{"type":"heading","attrs":{"id":"6ce7a8e2-75f3-44d0-abd1-901e39ac243d","textAlign":"inherit","indent":0,"level":2,"isHoverDragHandle":false},"content":[{"type":"text","text":"总结"}]},{"type":"paragraph","attrs":{"id":"f39c51ee-2322-4bb8-8819-6acad51dc569","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"Elasticsearch 的单分片文档数限制是 Lucene 层面的硬性约束,这个限制在 7.x 版本中因为默认分片数的改变而变得更容易触碰。理解这个限制的原理和检查机制,可以帮助我们更好地规划索引策略,避免生产环境中出现意外。"}]},{"type":"paragraph","attrs":{"id":"95d71282-d88e-4154-83a2-edf6f0b721a5","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"记住几个关键点:"}]},{"type":"bulletList","attrs":{"id":"67ad9d13-685a-45ac-80ef-55aea3a99464","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"eb019ab1-96ee-4fd6-80a2-a7385731d7e0"},"content":[{"type":"paragraph","attrs":{"id":"a55f641b-eac1-409b-950f-82a5ee81bb62","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"单分片最大文档数:"},{"type":"text","marks":[{"type":"bold"}],"text":"2147483519"},{"type":"text","text":"(约 21.47 亿)"}]}]},{"type":"listItem","attrs":{"id":"14997eec-8462-4b81-a074-3a35cf295243"},"content":[{"type":"paragraph","attrs":{"id":"32f62335-e4b2-44a4-9873-78355ff79f84","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"7.x 默认只有 "},{"type":"text","marks":[{"type":"bold"}],"text":"1 个分片"},{"type":"text","text":",更容易达到限制"}]}]},{"type":"listItem","attrs":{"id":"6c6c118a-437a-46a3-9463-bdf4f7f35773"},"content":[{"type":"paragraph","attrs":{"id":"2b090d5e-8c8b-416a-ade1-7d3bf59b53d0","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"提前规划比事后补救更重要"}]}]},{"type":"listItem","attrs":{"id":"d92ced2b-a4b4-4055-b4d7-f1f1ae91291a"},"content":[{"type":"paragraph","attrs":{"id":"cc65619a-85f4-413f-8bf3-1b9f05bf736c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"对于时序数据,ILM 是最佳选择"}]}]}]},{"type":"paragraph","attrs":{"id":"e56a918a-680d-489c-b306-50c2b1b338bc","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","marks":[{"type":"textStyle","attrs":{"color":"","background":""}}],"text":"希望这篇文章能帮助你更好地理解和应对这个问题。如果你的集群中有大量数据,不妨现在就检查一下各个分片的文档数吧!"}]}]}