在Sping Boot 项目中,启动期间无法使用Spring Data Commons创建Elasticsearch索引

tf7tbtn2  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(161)

我正在使用Sping Boot 框架构建一个项目。这段代码已经在开发环境中通过了测试。
我的JDK版本是1.8,Sping Boot 版本是2.6.3,Spring Data Commons版本是2.3.2.RELEASE,Elasticsearch版本是7.4。
我的做法如下:
1.创建ElasticsearchMap模板。

PUT _template/excluded-time-record-template
{
    "template":"excluded-time-record*",
    "mappings": {
        "properties" : {
        "_class" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "createTime" : {
          "type" : "long"
        },
        "dimension" : {
          "type" : "long"
        },
        "excludedEndTime" : {
          "type" : "long"
        },
        "excludedStartTime" : {
          "type" : "long"
        },
        "id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isLeaf" : {
          "type" : "boolean"
        },
        "path" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "pathLabel" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "reason" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "repeatPeriod" : {
          "type" : "long"
        },
        "sellerId" : {
          "type" : "long"
        }
      }
    }
}

1.使用Spring Data自动创建此索引。DTO数据结构如下所示:

@Data
@Accessors(chain = true)
@Document(indexName = " excluded-time-record" + DYNAMIC_INDEX_SUFFIX)
public class ExcludedTimeRecordDTO {

    @Id
    private String id;

    @Field(type = FieldType.Byte)
    private Integer dimension;

    @Field(type = FieldType.Keyword)
    private String rangeId;

    @Field(type = FieldType.Text)
    private String rangeName;

    @Field(type = FieldType.Text)
    private String reason;
    
    @Field(type = FieldType.Byte)
    private Integer repeatPeriod;

    @Field(type = FieldType.Long)
    private Long repeatStartTime;

   
    @Field(type = FieldType.Long)
    private Long repeatEndTime;

    @Field(type = FieldType.Long)
    private Long excludedStartTime;

    @Field(type = FieldType.Long)
    private Long excludedEndTime;

    @Field(type = FieldType.Text)
    private String cascadeInfo;

    @Field(type = FieldType.Long)
    private Long createTime;

    @Field(type = FieldType.Long)
    private Long sellerId;

    private CascadeNode node;

    @Field(type = FieldType.Nested)
    private List<String> path;

    @Field(type = FieldType.Nested)
    private List<String> pathLabel;

    @Field(type = FieldType.Boolean)
    private Boolean isLeaf;

}

我已经在四分之三的生产环境中部署了这段代码,没有任何错误。但是,在最后一个环境中部署代码时,Sping Boot 项目无法启动。
错误消息如下:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'excludedTimeRecordRepository' defined in com.service.repository.ExcludedTimeRecordRepository defined in @EnableElasticsearchRepositories declared on ElasticsearchRepositoriesRegistrar.EnableElasticsearchRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is RestStatusException{status=400} org.springframework.data.elasticsearch.RestStatusException: Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]];
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(Default
SingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:479)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:550)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:520)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:673)
        at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:228)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:329)
        ... 41 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is RestStatusException{status=400} org.springframework.data.elasticsearch.RestStatusException: Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]];
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:224)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.lambda$instantiateClass$5(RepositoryFactorySupport.java:578)
        at java.util.Optional.map(Optional.java:215)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.instantiateClass(RepositoryFactorySupport.java:578)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepositoryViaReflection(RepositoryFactorySupport.java:543)
        at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getTargetRepository(ElasticsearchRepositoryFactory.java:74)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:324)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:322)
        at org.springframework.data.util.Lazy.getNullable(Lazy.java:230)
        at org.springframework.data.util.Lazy.get(Lazy.java:114)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328)
        at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean.afterPropertiesSet(ElasticsearchRepositoryFactoryBean.java:69)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
        ... 54 more
Caused by: RestStatusException{status=400} org.springframework.data.elasticsearch.RestStatusException: Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]];
        at org.springframework.data.elasticsearch.core.ElasticsearchExceptionTranslator.translateExceptionIfPossible(ElasticsearchExceptionTranslator.java:69)
        at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.translateException(ElasticsearchRestTemplate.java:427)
        at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:410)
        at org.springframework.data.elasticsearch.core.RestIndexTemplate.doCreate(RestIndexTemplate.java:83)
        at org.springframework.data.elasticsearch.core.AbstractIndexTemplate.createWithMapping(AbstractIndexTemplate.java:103)
        at org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.<init>(SimpleElasticsearchRepository.java:93)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211)
        ... 67 more
Caused by: ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]];
        at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:176)
        at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:2011)
        at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1988)
        at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1745)
        at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1717)
        at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1684)
        at org.elasticsearch.client.IndicesClient.create$original$so0vxYbT(IndicesClient.java:134)
        at org.elasticsearch.client.IndicesClient.create$original$so0vxYbT$accessor$EhbBKuxs(IndicesClient.java)
        at org.elasticsearch.client.IndicesClient$auxiliary$qpXIPEQB.call(Unknown Source)
        at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86)
        at org.elasticsearch.client.IndicesClient.create(IndicesClient.java)
        at org.springframework.data.elasticsearch.core.RestIndexTemplate.lambda$doCreate$0(RestIndexTemplate.java:83)
        at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:408)
        ... 75 more
        Suppressed: org.elasticsearch.client.ResponseException: method [PUT], host [http://internal-xxx.com], URI [/excluded-time-record-2023-08?master_timeout=30s&timeout=30s], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [_doc]: Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]","caused_by":{"type":"mapper_parsing_exception","reason":"Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]"}},"status":400}
                at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:326)
                at org.elasticsearch.client.RestClient.performRequest(RestClient.java:296)
                at org.elasticsearch.client.RestClient.performRequest(RestClient.java:270)
                at org.elasticsearch.client.RestHighLevelClient.performClientRequest(RestHighLevelClient.java:2082)
                at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1732)
                ... 84 more
Caused by: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Mapping definition for [pathLabel] has unsupported parameters:  [fields : {keyword={ignore_above=256, type=keyword}}]]]
        at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:485)
        at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:396)
        at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:426)
        at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:592)
        at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:168)
        ... 87 more

为什么会出现这个问题?我知道在版本7.X中,在创建Map时不需要指定“Document”。任何建议都会对我很有帮助。谢谢
关于这个问题,我应该考虑哪些方面?会不会是版本冲突?

mbzjlibv

mbzjlibv1#

我在你的代码中看到了两个问题:
你有一个前导空格在索引名称,这意味着你的模板将永远不会匹配

@Document(indexName = " excluded-time-record" + DYNAMIC_INDEX_SUFFIX)
                        ^^^

B.这是错误的真实的原因:你声明pathLabel在你的DTO中嵌套,但在你的Map中作为普通的文本字段。path的问题。您需要将这两个字段都声明为@Field(type = FieldType.Text),或者确保您的Map具有声明为包含pathpathLabelnested字段

相关问题