optimize query facet in case the query is a filtered query with match_all query to use just the filter

This commit is contained in:
kimchy 2011-01-11 18:49:05 +02:00
parent ba9a12e201
commit 6130ac2ee7
1 changed files with 27 additions and 4 deletions

View File

@ -20,11 +20,10 @@
package org.elasticsearch.search.facet.query; package org.elasticsearch.search.facet.query;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.*;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.elasticsearch.common.lucene.docset.DocSet; import org.elasticsearch.common.lucene.docset.DocSet;
import org.elasticsearch.common.lucene.docset.DocSets; import org.elasticsearch.common.lucene.docset.DocSets;
import org.elasticsearch.common.lucene.search.MatchAllDocsFilter;
import org.elasticsearch.index.cache.filter.FilterCache; import org.elasticsearch.index.cache.filter.FilterCache;
import org.elasticsearch.search.facet.AbstractFacetCollector; import org.elasticsearch.search.facet.AbstractFacetCollector;
import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.Facet;
@ -44,7 +43,12 @@ public class QueryFacetCollector extends AbstractFacetCollector {
public QueryFacetCollector(String facetName, Query query, FilterCache filterCache) { public QueryFacetCollector(String facetName, Query query, FilterCache filterCache) {
super(facetName); 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 { @Override protected void doSetNextReader(IndexReader reader, int docBase) throws IOException {
@ -60,4 +64,23 @@ public class QueryFacetCollector extends AbstractFacetCollector {
@Override public Facet facet() { @Override public Facet facet() {
return new InternalQueryFacet(facetName, count); 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;
}
} }