Spring-Boot Elasticseach EntityMapper无法自动装配

Pat*_*ick 6 spring spring-boot spring-data-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);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我需要创建一个新的实例,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.
Run Code Online (Sandbox Code Playgroud)

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

我用的spring-data-elasticsearch-3.0.2.RELEASE.jarcore包在里面的地方.

我的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>
Run Code Online (Sandbox Code Playgroud)

Meh*_*kur 0

我查看了spring-data-elasticsearch的源代码。没有 的 bean/组件定义EntityMapper。看来这个答案是错误的。我在我的项目上测试它并得到相同的错误。

Consider defining a bean of type 'org.springframework.data.elasticsearch.core.EntityMapper' in your configuration.
Run Code Online (Sandbox Code Playgroud)

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