Skip to content

Commit

Permalink
Merge pull request searchbox-io#507 from akshayukey/TrackScores
Browse files Browse the repository at this point in the history
Support track_scores in Search while performing Sort
  • Loading branch information
ferhatsb authored May 5, 2017
2 parents f704328 + 124d386 commit 02e8ee2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions jest-common/src/main/java/io/searchbox/core/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ public Builder setSearchType(SearchType searchType) {
return setParameter(Parameters.SEARCH_TYPE, searchType);
}

public Builder enableTrackScores() {
this.setParameter(Parameters.TRACK_SCORES, true);
return this;
}

public Builder addSort(Sort sort) {
sortList.add(sort);
return this;
Expand Down
2 changes: 2 additions & 0 deletions jest-common/src/main/java/io/searchbox/params/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ private Parameters() {

public static final String RETRY_ON_CONFLICT = "retry_on_conflict";

public static final String TRACK_SCORES = "track_scores";

public static final List<String> ACCEPTED_IN_BULK = Arrays.asList(
ROUTING,
PERCOLATOR,
Expand Down
11 changes: 11 additions & 0 deletions jest-common/src/test/java/io/searchbox/core/SearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.Arrays;
import java.util.List;

import io.searchbox.action.AbstractAction;
import io.searchbox.params.Parameters;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand Down Expand Up @@ -231,6 +233,15 @@ public void sortTest() {
test = test.getAsJsonObject("population");
assertFalse(test.has("order"));
assertFalse(test.has("order"));
AbstractAction srch = (AbstractAction) search;
assertTrue(srch.getParameter(Parameters.TRACK_SCORES).size() == 0);
search = new Search.Builder(query)
.addSort(Arrays.asList(sortByPopulationAsc, sortByPopulationDesc, sortByPopulation))
.enableTrackScores()
.build();
srch = (AbstractAction) search;
assertTrue(srch.getParameter(Parameters.TRACK_SCORES).size() == 1);
assertTrue((Boolean)srch.getParameter(Parameters.TRACK_SCORES).iterator().next());
}

@Test
Expand Down
20 changes: 17 additions & 3 deletions jest/src/test/java/io/searchbox/core/SearchIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import io.searchbox.params.SearchType;
import org.apache.lucene.search.Explanation;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

Expand Down Expand Up @@ -116,20 +116,34 @@ public void searchWithSourceFilterByParam() throws Exception {

@Test
public void searchWithSort() throws Exception {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"user\":{\"type\":\"keyword\"}}}"));
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"user\":{\"type\":\"keyword\"}}}", XContentType.JSON));
assertTrue(index(INDEX, TYPE, "sws1", "{\"user\":\"kimchy1\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(INDEX, TYPE, "sws2", "{\"user\":\"kimchy2\"}").getResult().equals(DocWriteResponse.Result.CREATED));
refresh();
ensureSearchable(INDEX);

SearchResult result = client.execute(new Search.Builder("").setParameter("sort", "user").build());
Search search = new Search.Builder("").setParameter("sort", "user").build();
SearchResult result = client.execute(search);
assertTrue(result.getErrorMessage(), result.isSucceeded());

List<SearchResult.Hit<Object, Void>> hits = result.getHits(Object.class);
assertEquals(1, hits.get(0).sort.size());
assertEquals("kimchy1", hits.get(0).sort.get(0));
assertEquals(null, hits.get(0).score);
assertEquals(1, hits.get(1).sort.size());
assertEquals("kimchy2", hits.get(1).sort.get(0));
assertEquals(null, hits.get(1).score);

search = new Search.Builder("").setParameter("sort", "user").enableTrackScores().build();
result = client.execute(search);
hits = result.getHits(Object.class);
assertTrue(result.getErrorMessage(), result.isSucceeded());
assertEquals(1, hits.get(0).sort.size());
assertEquals("kimchy1", hits.get(0).sort.get(0));
assertEquals(new Double(1.0), hits.get(0).score);
assertEquals(1, hits.get(1).sort.size());
assertEquals("kimchy2", hits.get(1).sort.get(0));
assertEquals(new Double(1.0), hits.get(1).score);
}

@Test
Expand Down

0 comments on commit 02e8ee2

Please sign in to comment.