问题描述
基于这个答案和评论,我实现了代码以接收弹性搜索查询的分数.
Based on this answer and the comments I implemented the code to receive the scores of an elastic search query.
public class CustomizedHotelRepositoryImpl implements CustomizedHotelRepository {
private final ElasticsearchTemplate elasticsearchTemplate;
@Autowired
public CustomizedHotelRepositoryImpl(ElasticsearchTemplate elasticsearchTemplate) {
super();
this.elasticsearchTemplate = elasticsearchTemplate;
}
@Override
public Page<Hotel> findHotelsAndScoreByName(String name) {
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.should(QueryBuilders.queryStringQuery(name).lenient(true).defaultOperator(Operator.OR).field("name"));
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(queryBuilder)
.withPageable(PageRequest.of(0, 100)).build();
DefaultEntityMapper mapper = new DefaultEntityMapper();
ResultsExtractor<Page<Hotel>> rs = new ResultsExtractor<Page<Hotel>>() {
@Override
public Page<Hotel> extract(SearchResponse response) {
ArrayList<Hotel> hotels = new ArrayList<>();
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit : hits) {
try {
Hotel hotel = mapper.mapToObject(hit.getSourceAsString(), Hotel.class);
hotel.setScore(hit.getScore());
hotels.add(hotel);
} catch (IOException e) {
e.printStackTrace();
}
}
return new PageImpl<>(hotels, PageRequest.of(0, 100), response.getHits().getTotalHits());
}
};
return elasticsearchTemplate.query(nativeSearchQuery, rs);
}
}
正如你所看到的,我需要创建一个 DefaultEntityMapper mapper = new DefaultEntityMapper();
的新实例,这不应该是这种情况,因为它应该可以 @Autowire EntityMapper代码>.如果我这样做,我会得到没有 bean 的异常.
As you can see I needed to create a new instance of DefaultEntityMapper mapper = new DefaultEntityMapper();
which should not be the case because it should be possible to @Autowire EntityMapper
. If I do so, I get the exception that there is no bean.
Description:
Field entityMapper in com.example.elasticsearch5.es.cluster.repository.impl.CustomizedCluserRepositoryImpl required a bean of type 'org.springframework.data.elasticsearch.core.EntityMapper' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.data.elasticsearch.core.EntityMapper' in your configuration.
是否有人知道是否可以直接自动装配 EntityMapper 或者是否需要使用 @Bean
注释手动创建 bean.
So does anybody know if its possible to autowire EntityMapper directly or does it needs to create the bean manually using @Bean
annotation.
我使用 spring-data-elasticsearch-3.0.2.RELEASE.jar
其中 core
包在里面.
I use spring-data-elasticsearch-3.0.2.RELEASE.jar
where the core
package is inside.
我的 pom.xml:
My pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
推荐答案
我查看了 源代码.EntityMapper
没有 bean/组件定义.看来这个答案是错误的.我在我的项目上测试它并得到同样的错误.
I checked out the source code of spring-data-elasticsearch. There is no bean/comoponent definition for EntityMapper
. It seems this answer is wrong. I test it on my project and get the same error.
Consider defining a bean of type 'org.springframework.data.elasticsearch.core.EntityMapper' in your configuration.
除了定义@Bean 之外,我找不到任何其他选项
I couldn't find any other option by except defining a @Bean
这篇关于Spring-Boot Elasticseach EntityMapper 无法自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!