gpt4 book ai didi

Spring-Boot Elasticseach EntityMapper 无法 Autowiring

转载 作者:行者123 更新时间:2023-12-04 16:04:55 28 4
gpt4 key购买 nike

基于 this answer以及我实现代码以接收 Elasticsearch 查询分数的评论。

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 的异常。
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.

那么有人知道是否可以直接 Autowiring EntityMapper 或者是否需要使用 @Bean 手动创建 bean注解。

我用 spring-data-elasticsearch-3.0.2.RELEASE.jar哪里 core包裹在里面。

我的 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>

最佳答案

我查看了 source code Spring 数据 Elasticsearch 。 EntityMapper 没有 bean/组件定义.好像是这个answer是错的。我在我的项目上测试它并得到同样的错误。

Consider defining a bean of type 'org.springframework.data.elasticsearch.core.EntityMapper' in your configuration.

除了定义@Bean,我找不到任何其他选项

关于Spring-Boot Elasticseach EntityMapper 无法 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48830308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com