Skip to content

Commit

Permalink
Enable trackScores support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebs-mkhludnev authored and VanRoy committed Feb 11, 2020
1 parent 52e2755 commit 7e73958
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1257,6 +1256,8 @@ private SearchSourceBuilder prepareSearch(Query query) {
if (query.getMinScore() > 0) {
searchSourceBuilder.minScore(query.getMinScore());
}

searchSourceBuilder.trackScores(query.getTrackScores());
return searchSourceBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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<SampleEntity>() {
public SampleEntity extract(SearchResult result) {
final List<Hit<SampleEntity, Void>> hits = result.getHits(SampleEntity.class);
assertThat(hits.size(), greaterThan(0));
final Hit<SampleEntity, Void> 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")
Expand Down

0 comments on commit 7e73958

Please sign in to comment.