Don't truncate TopDocs after rescoring

We were previously over-trimming the TopDocs such that you get
size-from hits instead of size, which is wrong when from != 0.

Closes #11127

Closes #11342
This commit is contained in:
Michael McCandless 2015-05-26 04:43:18 -04:00 committed by mikemccand
commit 4334404a20
2 changed files with 23 additions and 6 deletions

View File

@ -60,11 +60,6 @@ public class RescorePhase extends AbstractComponent implements SearchPhase {
for (RescoreSearchContext ctx : context.rescore()) {
topDocs = ctx.rescorer().rescore(topDocs, context, ctx);
}
if (context.size() < topDocs.scoreDocs.length) {
ScoreDoc[] hits = new ScoreDoc[context.size()];
System.arraycopy(topDocs.scoreDocs, 0, hits, 0, hits.length);
topDocs = new TopDocs(topDocs.totalHits, hits, topDocs.getMaxScore());
}
context.queryResult().topDocs(topDocs);
} catch (IOException e) {
throw new ElasticsearchException("Rescore Phase Failed", e);

View File

@ -45,6 +45,7 @@ import java.util.Arrays;
import java.util.Comparator;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
@ -206,7 +207,7 @@ public class QueryRescorerTests extends ElasticsearchIntegrationTest {
RescoreBuilder.queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3))
.setQueryWeight(0.6f).setRescoreQueryWeight(2.0f)).setRescoreWindow(20).execute().actionGet();
assertThat(searchResponse.getHits().hits().length, equalTo(3));
assertThat(searchResponse.getHits().hits().length, equalTo(5));
assertHitCount(searchResponse, 9);
assertFirstHit(searchResponse, hasId("3"));
}
@ -719,4 +720,25 @@ public class QueryRescorerTests extends ElasticsearchIntegrationTest {
ensureGreen();
return numDocs;
}
// #11277
public void testFromSize() throws Exception {
Builder settings = Settings.builder();
settings.put(SETTING_NUMBER_OF_SHARDS, 1);
settings.put(SETTING_NUMBER_OF_REPLICAS, 0);
assertAcked(prepareCreate("test").setSettings(settings));
for(int i=0;i<5;i++) {
client().prepareIndex("test", "type", ""+i).setSource("text", "hello world").get();
}
refresh();
SearchRequestBuilder request = client().prepareSearch();
request.setQuery(QueryBuilders.termQuery("text", "hello"));
request.setFrom(1);
request.setSize(4);
request.addRescorer(RescoreBuilder.queryRescorer(QueryBuilders.matchAllQuery()));
request.setRescoreWindow(50);
assertEquals(4, request.get().getHits().hits().length);
}
}