Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix paging on strings sorted in ascending order. #9157

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -91,17 +91,32 @@ protected SortedDocValues getSortedDocValues(LeafReaderContext context, String f
}
}

@Override
public void setScorer(Scorer scorer) {
BytesRefFieldComparatorSource.this.setScorer(scorer);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed but was just added for consitency


public BytesRef value(int slot) {
// TODO: When serializing the response to the coordinating node, we lose the information about
// whether the comparator sorts missing docs first or last. We should fix it and let
// TopDocs.merge deal with it (it knows how to)
BytesRef value = super.value(slot);
if (value == null) {
assert sortMissingFirst(missingValue) || sortMissingLast(missingValue);
value = missingBytes;
}
return value;
}

public void setTopValue(BytesRef topValue) {
// symetric of value(int): if we need to feed the comparator with <tt>null</tt>
// if we overrode the value with MAX_TERM in value(int)
if (topValue == missingBytes && (sortMissingFirst(missingValue) || sortMissingLast(missingValue))) {
topValue = null;
}
super.setTopValue(topValue);
}

};
}

Expand Down
Expand Up @@ -24,13 +24,15 @@
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
Expand All @@ -39,9 +41,15 @@
import java.util.Map;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows;
import static org.hamcrest.Matchers.*;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

/**
*
Expand Down Expand Up @@ -448,4 +456,38 @@ public void testThatNonExistingScrollIdReturnsCorrectException() throws Exceptio

assertThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse.getScrollId()), RestStatus.NOT_FOUND);
}

@Test
public void testStringSortMissingAscTerminates() throws Exception {
assertAcked(prepareCreate("test")
.setSettings(ImmutableSettings.settingsBuilder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0))
.addMapping("test", "no_field", "type=string", "some_field", "type=string"));
client().prepareIndex("test", "test", "1").setSource("some_field", "test").get();
refresh();

SearchResponse response = client().prepareSearch("test")
.setTypes("test")
.addSort(new FieldSortBuilder("no_field").order(SortOrder.ASC).missing("_last"))
.setScroll("1m")
.get();
assertHitCount(response, 1);
assertSearchHits(response, "1");

response = client().prepareSearchScroll(response.getScrollId()).get();
assertSearchResponse(response);
assertHitCount(response, 1);
assertNoSearchHits(response);

response = client().prepareSearch("test")
.setTypes("test")
.addSort(new FieldSortBuilder("no_field").order(SortOrder.ASC).missing("_first"))
.setScroll("1m")
.get();
assertHitCount(response, 1);
assertSearchHits(response, "1");

response = client().prepareSearchScroll(response.getScrollId()).get();
assertHitCount(response, 1);
assertThat(response.getHits().getHits().length, equalTo(0));
}
}
Expand Up @@ -145,6 +145,10 @@ public static void assertHitCount(SearchResponse searchResponse, long expectedHi
assertVersionSerializable(searchResponse);
}

public static void assertNoSearchHits(SearchResponse searchResponse) {
assertEquals(0, searchResponse.getHits().getHits().length);
}

public static void assertSearchHits(SearchResponse searchResponse, String... ids) {
String shardStatus = formatShardStatus(searchResponse);

Expand Down