diff --git a/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java b/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java index f9337c76..40074ee4 100644 --- a/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java +++ b/spring-data-jest/src/main/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplate.java @@ -40,7 +40,6 @@ import io.searchbox.params.Parameters; import io.searchbox.params.SearchType; import org.elasticsearch.action.DocWriteResponse; -import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.common.Nullable; @@ -1257,6 +1256,8 @@ private SearchSourceBuilder prepareSearch(Query query) { if (query.getMinScore() > 0) { searchSourceBuilder.minScore(query.getMinScore()); } + + searchSourceBuilder.trackScores(query.getTrackScores()); return searchSourceBuilder; } diff --git a/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java b/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java index 56eabb88..734827b1 100755 --- a/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java +++ b/spring-data-jest/src/test/java/com/github/vanroy/springdata/jest/JestElasticsearchTemplateTests.java @@ -31,6 +31,7 @@ import com.github.vanroy.springdata.jest.internal.MultiDocumentResult; import com.github.vanroy.springdata.jest.internal.SearchScrollResult; import com.github.vanroy.springdata.jest.mapper.JestMultiGetResultMapper; +import com.github.vanroy.springdata.jest.mapper.JestResultsExtractor; import com.github.vanroy.springdata.jest.mapper.JestResultsMapper; import com.github.vanroy.springdata.jest.mapper.JestSearchResultMapper; import com.github.vanroy.springdata.jest.provider.CustomSearchSourceBuilderProvider; @@ -41,6 +42,8 @@ import io.searchbox.client.JestResult; import io.searchbox.core.DocumentResult; import io.searchbox.core.SearchResult; +import io.searchbox.core.SearchResult.Hit; + import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -64,7 +67,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.ElasticsearchException; -import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.core.EntityMapper; import org.springframework.data.elasticsearch.core.ScrolledPage; import org.springframework.data.elasticsearch.core.query.*; @@ -555,6 +557,37 @@ public void shouldExecuteStringQuery() { assertThat(sampleEntities.getTotalElements(), equalTo(1L)); assertThat(sampleEntities.getContent().get(0), equalTo(sampleEntity)); } + + @Test + public void shouldTrackScores() { + // given + String documentId = String.valueOf(ThreadLocalRandom.current().nextLong()).substring(1, 6); + SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message("some message") + .version(System.currentTimeMillis()).build(); + + IndexQuery indexQuery = getIndexQuery(sampleEntity); + + elasticsearchTemplate.index(indexQuery); + elasticsearchTemplate.refresh(SampleEntity.class); + + // when + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(termQuery("message", "message")) + .withSort(new FieldSortBuilder("message").order(SortOrder.ASC)) + .withTrackScores(true).build(); + SampleEntity result = elasticsearchTemplate.query(searchQuery, new JestResultsExtractor() { + public SampleEntity extract(SearchResult result) { + final List> hits = result.getHits(SampleEntity.class); + assertThat(hits.size(), greaterThan(0)); + final Hit hit = hits.get(0); + assertThat(hit.score, notNullValue(Double.class)); + assertThat(result.getMaxScore(), notNullValue(Float.class)); + return hit.source; + } + }); + // then + assertThat(result, notNullValue()); + } @Test @Ignore("Find how to activate plugins")