Spring-Boot Elasticseach EntityMapper 无法自动装配

2024-02-04

基于这个答案 https://stackoverflow.com/a/44523398/3493036以及我实现代码以接收弹性搜索查询分数的评论。

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.

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

I use 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>

我检查了源代码 https://github.com/spring-projects/spring-data-elasticsearch/tree/master/src/main/java/org/springframework/data/elasticsearch/corespring-data-elasticsearch 的。没有 bean/组件定义EntityMapper。看来这个answer https://stackoverflow.com/questions/44496243/score-of-each-hit-with-spring-searchquery-elasticsearch是错的。我在我的项目上测试它并得到相同的错误。

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring-Boot Elasticseach EntityMapper 无法自动装配 的相关文章

随机推荐