diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java index 7669a5120f3..5a773590ec9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java @@ -72,10 +72,11 @@ public class FacetsPhase implements SearchPhase { Filter facetFilter = new QueryWrapperFilter(queryFacet.query()); facetFilter = context.filterCache().cache(facetFilter); long count; - if (contextFacets.queryType() == SearchContextFacets.QueryExecutionType.COLLECT) { - count = executeQueryCollectorCount(context, queryFacet, facetFilter); - } else if (contextFacets.queryType() == SearchContextFacets.QueryExecutionType.IDSET) { + // if we already have the doc id set, then use idset since its faster + if (context.searcher().docIdSet() != null || contextFacets.queryType() == SearchContextFacets.QueryExecutionType.IDSET) { count = executeQueryIdSetCount(context, queryFacet, facetFilter); + } else if (contextFacets.queryType() == SearchContextFacets.QueryExecutionType.COLLECT) { + count = executeQueryCollectorCount(context, queryFacet, facetFilter); } else { throw new ElasticSearchIllegalStateException("No matching for type [" + contextFacets.queryType() + "]"); }