add test for facets with search size 0

This commit is contained in:
kimchy 2010-06-25 22:46:07 +03:00
parent 83010f7ee1
commit 5df2257319
2 changed files with 64 additions and 2 deletions

View File

@ -94,10 +94,15 @@ public class QueryPhase implements SearchPhase {
} }
TopDocs topDocs; TopDocs topDocs;
int numDocs = searchContext.from() + searchContext.size();
if (numDocs == 0) {
// if 0 was asked, change it to 1 since 0 is not allowed
numDocs = 1;
}
if (searchContext.sort() != null) { if (searchContext.sort() != null) {
topDocs = searchContext.searcher().search(query, null, searchContext.from() + searchContext.size(), searchContext.sort()); topDocs = searchContext.searcher().search(query, null, numDocs, searchContext.sort());
} else { } else {
topDocs = searchContext.searcher().search(query, searchContext.from() + searchContext.size()); topDocs = searchContext.searcher().search(query, numDocs);
} }
searchContext.queryResult().topDocs(topDocs); searchContext.queryResult().topDocs(topDocs);
} catch (Exception e) { } catch (Exception e) {

View File

@ -20,6 +20,7 @@
package org.elasticsearch.test.integration.search.facets; package org.elasticsearch.test.integration.search.facets;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.search.facets.histogram.HistogramFacet; import org.elasticsearch.search.facets.histogram.HistogramFacet;
@ -58,6 +59,62 @@ public class SimpleFacetsTests extends AbstractNodesTests {
return client("server1"); return client("server1");
} }
@Test public void testFacetsWithSize0() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client.admin().indices().prepareCreate("test").execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("stag", "111")
.startArray("tag").value("xxx").value("yyy").endArray()
.endObject()).execute().actionGet();
client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet();
client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("stag", "111")
.startArray("tag").value("zzz").value("yyy").endArray()
.endObject()).execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
SearchResponse searchResponse = client.prepareSearch()
.setSize(0)
.setQuery(termQuery("stag", "111"))
.addFacetTerms("facet1", "stag", 10)
.addFacetTerms("facet2", "tag", 10)
.execute().actionGet();
assertThat(searchResponse.hits().hits().length, equalTo(0));
TermsFacet facet = searchResponse.facets().facet(TermsFacet.class, "facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
searchResponse = client.prepareSearch()
.setSearchType(SearchType.QUERY_AND_FETCH)
.setSize(0)
.setQuery(termQuery("stag", "111"))
.addFacetTerms("facet1", "stag", 10)
.addFacetTerms("facet2", "tag", 10)
.execute().actionGet();
assertThat(searchResponse.hits().hits().length, equalTo(0));
facet = searchResponse.facets().facet(TermsFacet.class, "facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(1));
assertThat(facet.entries().get(0).term(), equalTo("111"));
assertThat(facet.entries().get(0).count(), equalTo(2));
}
@Test public void testTermsFacets() throws Exception { @Test public void testTermsFacets() throws Exception {
try { try {
client.admin().indices().prepareDelete("test").execute().actionGet(); client.admin().indices().prepareDelete("test").execute().actionGet();