一、前提:项目之前使用springboot+spring-data-mongodb。现在需要加入elasticsearch做搜索引擎,这样mongo和elasticsearch共存了。

二、报错信息:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'TTSAudioInfoEsRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property index found for type TTSAudioInfo!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
at org.springframework.data.repository.support.Repositories.cacheRepositoryFactory(Repositories.java:95)
at org.springframework.data.repository.support.Repositories.populateRepositoryFactoryInformation(Repositories.java:88)
at org.springframework.data.repository.support.Repositories.<init>(Repositories.java:81)
at org.springframework.data.repository.support.DomainClassConverter.setApplicationContext(DomainClassConverter.java:98)
at org.springframework.data.web.config.SpringDataWebConfiguration.addFormatters(SpringDataWebConfiguration.java:87)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.addFormatters(WebMvcConfigurerComposite.java:49)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.addFormatters(DelegatingWebMvcConfiguration.java:117)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.mvcConversionService(WebMvcConfigurationSupport.java:542)
at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$f6948e02.CGLIB$mvcConversionService$45(<generated>)
at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$f6948e02$$FastClassBySpringCGLIB$$43c05aaf.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$f6948e02.mvcConversionService(<generated>)
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.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 73 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property index found for type TTSAudioInfo!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84)
at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.<init>(PartTreeMongoQuery.java:60)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory$MongoQueryLookupStrategy.resolveQuery(MongoRepositoryFactory.java:168)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:435)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:220)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 98 common frames omitted

三、错误分析:

  1、TTSAudioInfo类是pojo类,既是mongo的document也是elasticsearch的document

/**
* @author: george
* @date: 2018/6/26-16-39
* @description:
*/
@org.springframework.data.elasticsearch.annotations.Document(indexName = "tts_audio_info", type = "tts_audio_info") //spring-data-elasticsearch注解
@org.springframework.data.mongodb.core.mapping.Document(collection = "TTSAudioInfo") //spring-data-mongodb注解
@Data //lombok注解
public class TTSAudioInfo extends BaseModel {
private static final long serialVersionUID = 1L;
public static final String FIELD_QUESTION = "question";
public static final String FIELD_ANSWER = "answer";
public static final String FIELD_FILE_URI = "fileUri";
public static final String FIELD_FILE_PATH = "filePath"; private String question;
@Field(type = FieldType.String, index = FieldIndex.analyzed)
private String answer;
private String fileUri;
private String filePath; public TTSAudioInfo() {
}
}

  2、然后是elasticsearch的repository接口,继承的是ElasticsearchRepository

import ai.deepbrain.admin.domain.TTSAudioInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /**
* @author: george
* @date: 2018/6/26-17-05
* @description:
*/
public interface TTSAudioInfoEsRepository extends ElasticsearchRepository<TTSAudioInfo, String> {
}

  

分析:spring-data-mongo和spring-data-elasticsearch都属于spring的spring-data项目,提供各种数据库数据访问的封装(mongo、redis、oracle、mysql、jpa、elasticsearch等等等等)。他们封装的格式基本一样;

而在本文的错误中,我们使用了mongodb和elasticsearch,我们在查看TTSAudioInfoEsRepository继承的ElasticsearchCrudRepository的继承关系后得知,他和MongoRepository都继承spring-data-commons的PagingAndSortingRepository。

(这里还有一点就是所有repository接口写在同一包下)所以导致了在mongodb扫描repository的时候也会扫描elasticsearch的repository,spring-data-mongodb会试图代理TTSAudioInfoEsRepository ,但是发现TTSAudioInfoEsRepository 使用的TTSAudioInfo中没有index属性,从而报错No property index found for type。

stackoverflow中有一篇类似的错误文章:https://stackoverflow.com/questions/36252099/no-property-index-found-for-type-user。

四、解决办法:

既然知道错误的原因了,那解决起来就相对简单了。无非就是让spring-data-mongo扫描mongodb的repository,spring-data-elasticsearch扫描elasticsearch的repository。

1、把不同类型的repository分别放在不同包下,然后在配置的地方指定扫描位置

  mongodb的配置类

@Configuration
@Import(value = MongoAutoConfiguration.class)
@EnableMongoRepositories(basePackages = "com.test.mongodb")
public class MongoConfiguration extends AbstractMongoConfiguration { @Autowired
private MongoProperties mongoProperties; @Override
protected String getDatabaseName() {
return mongoProperties.getDatabase();
} @Override
public Mongo mongo() throws Exception {
MongoClientURI clientURI = new MongoClientURI(mongoProperties.getUri());
return new MongoClient(clientURI);
} @Bean
public ValidatingMongoEventListener validatingMongoEventListener() {
return new ValidatingMongoEventListener(validator());
} @Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
} @Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext());
// remove _class field
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
converter.setCustomConversions(customConversions());
return converter;
}
}

  elasticsearch的配置类

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.test.elasticsearch")
public class ElasticsearchConfiguration { @Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client,
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
MappingElasticsearchConverter converter = new MappingElasticsearchConverter(
new SimpleElasticsearchMappingContext());
DefaultResultMapper mapper = new DefaultResultMapper(converter.getMappingContext(),
new CustomEntityMapper(jackson2ObjectMapperBuilder.createXmlMapper(false).build()));
return new ElasticsearchTemplate(client, converter, mapper);
} public class CustomEntityMapper implements EntityMapper { private ObjectMapper objectMapper; public CustomEntityMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
} @Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
} @Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
}
}

2、在配置enabled注解的时候添加includeFilters

mongo:

@Configuration
@Import(value = MongoAutoConfiguration.class)
@EnableMongoRepositories(basePackages = "com.test",includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = MongoRepository.class))
public class MongoConfiguration extends AbstractMongoConfiguration {

elasticsearch:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.test",includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchRepository.class))
public class ElasticsearchConfiguration {

springboot使用elasticsearch报No property index found for type错误的更多相关文章

  1. Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]

    Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...

  2. springboot集成swagger2报Illegal DefaultValue null for parameter type integer

    springboot集成swagger2,实体类中有int类型,会报" Illegal DefaultValue null for parameter type integer"的 ...

  3. SpringBoot+MyBatis整合报错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    项目启动的时候报这个错误,这个问题我百度了一天,果然不出意外的还是没能解决,其中有一篇文章相对来说还是有点用的:https://blog.csdn.net/qq8693/article/details ...

  4. Elasticsearch 报错:Fielddata is disabled on text fields by default. Set `fielddata=true` on [`your_field_name`] in order to load fielddata in memory by uninverting the inverted index.

    Elasticsearch 报错: Fielddata is disabled on text fields by default. Set `fielddata=true` on [`your_fi ...

  5. ElasticSearch(八):springboot集成ElasticSearch集群并使用

    1. 集群的搭建 见:ElasticSearch(七) 2. springboot配置集群 2.1 创建springboot项目,使用idea创建,不过多介绍(创建项目时候建议不要勾选elastics ...

  6. Springboot整合elasticSearch的官方API实例

    前言:在上一篇博客中,我介绍了从零开始安装ElasticSearch,es是可以理解为一个操作数据的中间件,可以把它作为数据的存储仓库来对待,它具备强大的吞吐能力和计算能力,其基于Lucene服务器开 ...

  7. SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)

    准备工作 环境准备 JAVA版本 java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121 ...

  8. SpringBoot 集成 Elasticsearch

    前面在 ubuntu 完成安装 elasticsearch,现在我们SpringBoot将集成elasticsearch. 1.创建SpringBoot项目 我们这边直接引入NoSql中Spring ...

  9. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

随机推荐

  1. BZOJ4832[Lydsy1704月赛]抵制克苏恩——期望DP

    题目描述 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一个游戏,每个玩家拥有一个 30 点血量的 ...

  2. BZOJ3075[USACO 2013 Mar Gold 3.Necklace]——AC自动机+DP

    题目描述 给你一个长度为n的字符串A,再给你一个长度为m的字符串B,求至少在A中删去多少个字符才能使得B不是A的子串.注:该题只读入A和B,不读入长度,先读入A,再读入B.数据保证A和B中只含小写字母 ...

  3. 【Linux】Centos6.8下一键安装Lamp环境

    [下载地址] 以下三种都是快捷安装环境的工具,都提供相应的脚本,原理都相同,一个会了其他的也就都会了,我用的比较多的会是lnmp和oneinstack,最近在用的都是oneinstack,挺好用的. ...

  4. BUPT2017 wintertraining(15) #1 题解

    拖了一周才完成的题解,抛出一个可爱的表情 (っ'-')╮ =͟͟͞͞❤️.对我来说E.F比较难,都是线段树的题,有点久没写了. A - Infinite Sequence CodeForces - 6 ...

  5. Hdoj 1008.Elevator 题解

    Problem Description The highest building in our city has only one elevator. A request list is made u ...

  6. 自学Aruba5.1.2-带宽限制

    点击返回:自学Aruba之路 自学Aruba5.1.2-带宽限制 1 针对role --可以限制所有数据     注:带宽限制需要PEFNG许可证 单位可以是kbits或是mbits 可以是上传(up ...

  7. [poj2528]Mayor's posters

    题目描述 The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campa ...

  8. CRM 2013 生成自动编号

    1. 建立二个实体: 单据规则: 用于创建单据的规则 规则流水号: 用于记录当前的流水号   2. 创建规则: 创建一条相关的记录后,就会显示对应的流水号:   最终效果:

  9. STM32 堆栈使用解析

    安富莱_STM32-V5开发板_μCOS-III教程.pdf 第4章

  10. poco logging

    http://pocoproject.org/slides/110-Logging.pdf