From 6130ac2ee77152dd2bc9ace4843b8a8e86056978 Mon Sep 17 00:00:00 2001 From: kimchy Date: Tue, 11 Jan 2011 18:49:05 +0200 Subject: [PATCH] optimize query facet in case the query is a filtered query with match_all query to use just the filter --- .../facet/query/QueryFacetCollector.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/query/QueryFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/query/QueryFacetCollector.java index 93895482726..1b42f9aa486 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/query/QueryFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/query/QueryFacetCollector.java @@ -20,11 +20,10 @@ package org.elasticsearch.search.facet.query; import org.apache.lucene.index.IndexReader; -import org.apache.lucene.search.Filter; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.QueryWrapperFilter; +import org.apache.lucene.search.*; import org.elasticsearch.common.lucene.docset.DocSet; import org.elasticsearch.common.lucene.docset.DocSets; +import org.elasticsearch.common.lucene.search.MatchAllDocsFilter; import org.elasticsearch.index.cache.filter.FilterCache; import org.elasticsearch.search.facet.AbstractFacetCollector; import org.elasticsearch.search.facet.Facet; @@ -44,7 +43,12 @@ public class QueryFacetCollector extends AbstractFacetCollector { public QueryFacetCollector(String facetName, Query query, FilterCache filterCache) { super(facetName); - this.filter = filterCache.cache(new QueryWrapperFilter(query)); + Filter possibleFilter = extractFilterIfApplicable(query); + if (possibleFilter != null) { + this.filter = filterCache.cache(possibleFilter); + } else { + this.filter = filterCache.cache(new QueryWrapperFilter(query)); + } } @Override protected void doSetNextReader(IndexReader reader, int docBase) throws IOException { @@ -60,4 +64,23 @@ public class QueryFacetCollector extends AbstractFacetCollector { @Override public Facet facet() { return new InternalQueryFacet(facetName, count); } + + /** + * If its a filtered query with a match all, then we just need the inner filter. + */ + private Filter extractFilterIfApplicable(Query query) { + if (query instanceof FilteredQuery) { + FilteredQuery fQuery = (FilteredQuery) query; + if (fQuery.getQuery() instanceof MatchAllDocsQuery) { + return fQuery.getFilter(); + } + if (fQuery.getQuery() instanceof DeletionAwareConstantScoreQuery) { + DeletionAwareConstantScoreQuery scoreQuery = (DeletionAwareConstantScoreQuery) fQuery.getQuery(); + if (scoreQuery.getFilter() instanceof MatchAllDocsFilter) { + return fQuery.getFilter(); + } + } + } + return null; + } }