6

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>
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • Do you depend on "spring-boot-starter-data-elasticsearch" or on the "spring-data-elasticsearch"? – hovanessyan Feb 19 '18 at 10:42
  • @hovanessyan I have both on the classpath. Could that be the issue? – Patrick Feb 19 '18 at 12:02
  • I don't think that's the issue, but the starter should be enough - the starter brings spring-data-elasticsearch as a transparent dependency. – hovanessyan Feb 19 '18 at 12:17
  • Did you add the @EnableElasticsearchRepositories ? – reos Feb 21 '18 at 19:44
  • @mehmet is right, there is currently no autowiring implemented neither in springboot-starter-data-elasticsearch nor in spring-data-elasticsearch. (checked version 3.1) – rfelgent Mar 22 '19 at 20:24

1 Answers1

0

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

Mehmet Sunkur
  • 2,373
  • 16
  • 22