Spring-Boot Elasticseach EntityMapper无法自动装配

问题描述 投票:6回答:1

基于this answer和评论我实现了代码以接收弹性搜索查询的分数。

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。

我使用spring-data-elasticsearch-3.0.2.RELEASE.jar包装里面的core

我们pom.khml:

<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>
spring spring-boot spring-data-elasticsearch
1个回答
0
投票

我查看了spring-data-elasticsearch的source codeEntityMapper没有bean / comoponent定义。看来这个answer是错的。我在我的项目上测试它并得到相同的错误。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.