Spring-Boot Elasticseach EntityMapper can not be autowired

戏子无情 提交于 2019-12-08 21:11:41

问题


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);
    }
}

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.

So does anybody know if its possible to autowire EntityMapper directly or does it needs to create the bean manually using @Bean annotation.

I use spring-data-elasticsearch-3.0.2.RELEASE.jar where the core package is inside.

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>

回答1:


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.

I couldn't find any other option by except defining a @Bean



来源:https://stackoverflow.com/questions/48830308/spring-boot-elasticseach-entitymapper-can-not-be-autowired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!