Error[8]: Undefined offset: 8, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

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

	@Test
    public 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);
        
        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)

	@Test
    public 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();
    }

第三步:创建索引 (仅供参考

    @Test
    public void testCreateIndex() throws IOException {
        // 创建索引
        CreateIndexRequest request = new CreateIndexRequest("demo-user");

        Map properties = new HashMap<>();
        Map titleType = new HashMap<>();
        titleType.put("type", "text");
        titleType.put("store", true);
        titleType.put("analyzer", "ik_max_word");
        properties.put("name", titleType);

        Map sexType = new HashMap<>();
        sexType.put("type", "keyword");
        properties.put("sex", sexType);

        Map ageType = new HashMap<>();
        ageType.put("type", "integer");
        ageType.put("store", true);
        properties.put("age", ageType);

        Map tagType = new HashMap<>();
        tagType.put("type", "text");
        properties.put("tags", tagType);

        Map 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 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
	request.order(0);
	request.patterns(Collections.singletonList("demo-*"));
(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke[+++](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(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(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(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(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(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(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(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(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(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(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(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 进行设值即可

[+++]

写的不好,望各位指正!!! 欢迎评论补充。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 9, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

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

	@Test
    public 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);
        
        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)

	@Test
    public 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();
    }

第三步:创建索引 (仅供参考

    @Test
    public void testCreateIndex() throws IOException {
        // 创建索引
        CreateIndexRequest request = new CreateIndexRequest("demo-user");

        Map properties = new HashMap<>();
        Map titleType = new HashMap<>();
        titleType.put("type", "text");
        titleType.put("store", true);
        titleType.put("analyzer", "ik_max_word");
        properties.put("name", titleType);

        Map sexType = new HashMap<>();
        sexType.put("type", "keyword");
        properties.put("sex", sexType);

        Map ageType = new HashMap<>();
        ageType.put("type", "integer");
        ageType.put("store", true);
        properties.put("age", ageType);

        Map tagType = new HashMap<>();
        tagType.put("type", "text");
        properties.put("tags", tagType);

        Map 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 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
	request.order(0);
	request.patterns(Collections.singletonList("demo-*"));
(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke(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(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(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(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(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(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(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(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(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(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(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(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 进行设值即可

[+++]

写的不好,望各位指正!!! 欢迎评论补充。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
es7 创建模板时,报错 Validation Failed: 1: index patterns are missing_随笔_内存溢出

es7 创建模板时,报错 Validation Failed: 1: index patterns are missing

es7 创建模板时,报错 Validation Failed: 1: index patterns are missing,第1张

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

	@Test
    public 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);
        
        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)

	@Test
    public 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();
    }

第三步:创建索引 (仅供参考

    @Test
    public void testCreateIndex() throws IOException {
        // 创建索引
        CreateIndexRequest request = new CreateIndexRequest("demo-user");

        Map properties = new HashMap<>();
        Map titleType = new HashMap<>();
        titleType.put("type", "text");
        titleType.put("store", true);
        titleType.put("analyzer", "ik_max_word");
        properties.put("name", titleType);

        Map sexType = new HashMap<>();
        sexType.put("type", "keyword");
        properties.put("sex", sexType);

        Map ageType = new HashMap<>();
        ageType.put("type", "integer");
        ageType.put("store", true);
        properties.put("age", ageType);

        Map tagType = new HashMap<>();
        tagType.put("type", "text");
        properties.put("tags", tagType);

        Map 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 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
	request.order(0);
	request.patterns(Collections.singletonList("demo-*"));
(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke(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(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(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(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(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(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(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(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(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(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(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(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 进行设值即可

写的不好,望各位指正!!! 欢迎评论补充。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5705002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存