From 5df2257319ce2da15bb788431d4e5d70208d9030 Mon Sep 17 00:00:00 2001 From: kimchy Date: Fri, 25 Jun 2010 22:46:07 +0300 Subject: [PATCH] add test for facets with search size 0 --- .../search/query/QueryPhase.java | 9 ++- .../search/facets/SimpleFacetsTests.java | 57 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java index f6078fa6bef..7bac10f283a 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -94,10 +94,15 @@ public class QueryPhase implements SearchPhase { } 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) { - topDocs = searchContext.searcher().search(query, null, searchContext.from() + searchContext.size(), searchContext.sort()); + topDocs = searchContext.searcher().search(query, null, numDocs, searchContext.sort()); } else { - topDocs = searchContext.searcher().search(query, searchContext.from() + searchContext.size()); + topDocs = searchContext.searcher().search(query, numDocs); } searchContext.queryResult().topDocs(topDocs); } catch (Exception e) { diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java index 9333c1e77dc..0adb138c3dc 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.test.integration.search.facets; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.Client; import org.elasticsearch.search.facets.histogram.HistogramFacet; @@ -58,6 +59,62 @@ public class SimpleFacetsTests extends AbstractNodesTests { 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 { try { client.admin().indices().prepareDelete("test").execute().actionGet();