From 83c26eb74abe7d3c4550a927ee78fe3ee4010d49 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Sat, 10 Aug 2013 19:09:19 +0200 Subject: [PATCH] NPE for POST mode facets if facet_filter gives no document. Closes #3479. --- .../search/facet/FacetExecutor.java | 12 ++++++---- .../search/facet/SimpleFacetsTests.java | 24 ++++++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/facet/FacetExecutor.java b/src/main/java/org/elasticsearch/search/facet/FacetExecutor.java index 05c0179423c..fc2571e0918 100644 --- a/src/main/java/org/elasticsearch/search/facet/FacetExecutor.java +++ b/src/main/java/org/elasticsearch/search/facet/FacetExecutor.java @@ -66,11 +66,13 @@ public abstract class FacetExecutor { for (int i = 0; i < docSets.size(); i++) { ContextDocIdSet entry = docSets.get(i); DocIdSet filteredSet = filter.getDocIdSet(entry.context, null); - filteredEntries.add(new ContextDocIdSet( - entry.context, - // TODO: can we be smart here, maybe AndDocIdSet is not always fastest? - new AndDocIdSet(new DocIdSet[]{entry.docSet, filteredSet}) - )); + if (filteredSet != null) { + filteredEntries.add(new ContextDocIdSet( + entry.context, + // TODO: can we be smart here, maybe AndDocIdSet is not always fastest? + new AndDocIdSet(new DocIdSet[]{entry.docSet, filteredSet}) + )); + } } post.executePost(filteredEntries); } diff --git a/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java b/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java index 0a1f2212c67..67fae4c7c6f 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java @@ -58,7 +58,6 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.termFilter; import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.search.facet.FacetBuilders.*; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; /** @@ -2282,6 +2281,29 @@ public class SimpleFacetsTests extends AbstractSharedClusterTest { } } + @Test // #3479: Null pointer exception for POST mode facets if facet_filter accepts no documents + public void testFilterFacetWithFacetFilterPostMode() throws IOException { + client().prepareIndex("test", "type1").setSource(jsonBuilder().startObject() + .field("field", "xxx") + .endObject()).execute().actionGet(); + + client().admin().indices().prepareRefresh().execute().actionGet(); + + for (int i = 0; i < numberOfRuns(); i++) { + SearchResponse searchResponse = client().prepareSearch() + .setQuery(matchAllQuery()) + .addFacet( + termsFacet("facet1").field("field").mode(FacetBuilder.Mode.POST).facetFilter(termFilter("tag", "doesnotexist")) + ) + .execute().actionGet(); + + assertThat(searchResponse.getFailedShards(), equalTo(0)); + TermsFacet facet = searchResponse.getFacets().facet("facet1"); + assertThat(facet.getName(), equalTo("facet1")); + assertThat(facet.getEntries().size(), equalTo(0)); + } + } + private long utcTimeInMillis(String time) { return timeInMillis(time, DateTimeZone.UTC); }