es7 创建模板时,报错 Validation Failed: 1: index patterns are missing
es7 創建模板時,報錯 Validation Failed: 1: index patterns are missing
在使用elasticsearch 7.15 創建template時,程序報 Validation Failed: 1: index patterns are missing
在使用spring boot 集成 elasticsearch 7,每次往 elasticsearch 中索引插入數據時,為這條數據自動添加一個默認的創建時間、更新時間,又不想在數據對象中指定此字段。
上圖是來自官網對 ingest pipelines(管道)的說明,意思就是在進行文檔插入時,文檔首先要經過 ingest pipelines(管道)進行加工處理后,再插入到索引庫中。
配置自動添加默認字段的腳本程序 參考:
https://www.freesion.com/article/99011432714/
文中變量(snowflakeService):生成 雪花算法id 的一個工具類 (可以參考 hutool 工具包)
第一步:首先需要創建一個 ingest pipelines
@Testpublic void testPipeline() throws IOException {XContentBuilder jsonBuilder = jsonBuilder().startObject().field("description", "inner pipeline-demo-date").startArray("processors").startObject().field("script").startObject().field("lang", "painless").field("source", "" +" def imp = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss.SSS\");\n" +" imp.setTimeZone(TimeZone.getTimeZone(\"Asia/Shanghai\"));\n" +" def ts = imp.format(new Date((new Date().getTime())));\n" +" if (ctx.create_time==null ){\n" +" ctx.create_time = ts;\n" +" }\n" +" if (ctx.delete_time==null ){\n" +" ctx.delete_time = 0;\n" +" }\n" +" ctx.update_time = ts;" +"").endObject().endObject().endArray().endObject();BytesReference reference = BytesReference.bytes(jsonBuilder);/** 第一個參數:管道唯一id* 第二個參數:配置管道* 第三個參數:管道數據類型*/PutPipelineRequest request = new PutPipelineRequest("pipeline-demo-date", reference, XContentType.JSON);request.timeout(TimeValue.timeValueSeconds(2));AcknowledgedResponse response = restHighLevelClient.ingest().putPipeline(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());// 關閉連接restHighLevelClient.close();}第二步:創建一個模板,綁定上面創建的管道(ingest pipelines)
@Testpublic void testPutIndexTemplate() throws IOException {PutIndexTemplateRequest request = new PutIndexTemplateRequest("demo");Settings.Builder builder = Settings.builder().put("default_pipeline", "pipeline-demo-date");request.settings(builder);XContentBuilder jsonBuilder = jsonBuilder().startObject().startObject("properties").startObject("date_time").field("type", "date").field("format", "epoch_millis").endObject().startObject("create_time").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss.SSS").endObject().startObject("update_time").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss.SSS").endObject().endObject().endObject();BytesReference reference = BytesReference.bytes(jsonBuilder);request.mapping(reference, XContentType.JSON);AcknowledgedResponse response = restHighLevelClient.indices().putTemplate(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());// 關閉連接restHighLevelClient.close();}第三步:創建索引 (僅供參考)
@Testpublic void testCreateIndex() throws IOException {// 創建索引CreateIndexRequest request = new CreateIndexRequest("demo-user");Map<String, Object> properties = new HashMap<>();Map<String, Object> titleType = new HashMap<>();titleType.put("type", "text");titleType.put("store", true);titleType.put("analyzer", "ik_max_word");properties.put("name", titleType);Map<String, Object> sexType = new HashMap<>();sexType.put("type", "keyword");properties.put("sex", sexType);Map<String, Object> ageType = new HashMap<>();ageType.put("type", "integer");ageType.put("store", true);properties.put("age", ageType);Map<String, Object> tagType = new HashMap<>();tagType.put("type", "text");properties.put("tags", tagType);Map<String, Object> dateType = new HashMap<>();dateType.put("type", "date");dateType.put("store", true);dateType.put("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis");properties.put("createTime", dateType);Map<String, Object> mappings = new HashMap<>();mappings.put("properties", properties);request.mapping(mappings);CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);System.out.println(response);// 關閉連接restHighLevelClient.close();}在執行上面 第二步 方法時會出現一個錯誤(Validation Failed: 1: index patterns are missing;)
org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: index patterns are missing;at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:15)at org.elasticsearch.client.indices.PutIndexTemplateRequest.validate(PutIndexTemplateRequest.java:88)at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1698)at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1672)at org.elasticsearch.client.IndicesClient.putTemplate(IndicesClient.java:1408)at com.baobao.ElasticSearchTests.testPutIndexTemplate(ElasticSearchTests.java:208)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)at java.util.ArrayList.forEach(ArrayList.java:1255)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)at java.util.ArrayList.forEach(ArrayList.java:1255)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)這個錯誤的意思是在進行參數校驗時,發現索引 patterns 缺失
解決方法如下:
在第二步代碼中對request 的 patterns 進行設值即可
request.order(0);request.patterns(Collections.singletonList("demo-*"));寫的不好,望各位指正!!! 歡迎評論補充。
總結
以上是生活随笔為你收集整理的es7 创建模板时,报错 Validation Failed: 1: index patterns are missing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人、技术、情怀
- 下一篇: ant design + react带有