refactor some facet support

This commit is contained in:
kimchy 2010-06-08 11:46:30 +03:00
parent 874993557c
commit d36cea07f3
4 changed files with 66 additions and 49 deletions

View File

@ -20,10 +20,13 @@
package org.elasticsearch.search.facets.query;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.*;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
import org.elasticsearch.index.cache.filter.FilterCache;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.support.AbstractFacetCollector;
import org.elasticsearch.util.lucene.docset.DocSet;
import org.elasticsearch.util.lucene.docset.DocSets;
@ -32,25 +35,19 @@ import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public class QueryFacetCollector extends FacetCollector {
public class QueryFacetCollector extends AbstractFacetCollector {
private final Filter filter;
private final String name;
private DocSet docSet;
private int count = 0;
public QueryFacetCollector(String name, Query query, FilterCache filterCache) {
this.name = name;
public QueryFacetCollector(String facetName, Query query, FilterCache filterCache) {
super(facetName);
this.filter = filterCache.cache(new QueryWrapperFilter(query));
}
@Override public void setScorer(Scorer scorer) throws IOException {
// ignore
}
@Override public void setNextReader(IndexReader reader, int docBase) throws IOException {
DocIdSet docIdSet = filter.getDocIdSet(reader);
if (docIdSet instanceof DocSet) {
@ -66,11 +63,7 @@ public class QueryFacetCollector extends FacetCollector {
}
}
@Override public boolean acceptsDocsOutOfOrder() {
return true;
}
@Override public Facet facet() {
return new InternalQueryFacet(name, count);
return new InternalQueryFacet(facetName, count);
}
}

View File

@ -20,7 +20,6 @@
package org.elasticsearch.search.facets.statistical;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.index.cache.field.FieldDataCache;
import org.elasticsearch.index.field.FieldData;
import org.elasticsearch.index.field.NumericFieldData;
@ -28,7 +27,7 @@ import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.FacetPhaseExecutionException;
import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.support.AbstractFacetCollector;
import java.io.IOException;
@ -37,9 +36,7 @@ import static org.elasticsearch.index.field.FieldDataOptions.*;
/**
* @author kimchy (shay.banon)
*/
public class StatisticalFacetCollector extends FacetCollector {
private final String name;
public class StatisticalFacetCollector extends AbstractFacetCollector {
private final String fieldName;
@ -51,14 +48,14 @@ public class StatisticalFacetCollector extends FacetCollector {
private final StatsProc statsProc = new StatsProc();
public StatisticalFacetCollector(String name, String fieldName, FieldDataCache fieldDataCache, MapperService mapperService) {
this.name = name;
public StatisticalFacetCollector(String facetName, String fieldName, FieldDataCache fieldDataCache, MapperService mapperService) {
super(facetName);
this.fieldName = fieldName;
this.fieldDataCache = fieldDataCache;
FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName);
if (mapper == null) {
throw new FacetPhaseExecutionException(name, "No mapping found for field [" + fieldName + "]");
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldName + "]");
}
fieldDataType = mapper.fieldDataType();
}
@ -71,15 +68,8 @@ public class StatisticalFacetCollector extends FacetCollector {
fieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, fieldName, fieldDataOptions().withFreqs(false));
}
@Override public void setScorer(Scorer scorer) throws IOException {
}
@Override public boolean acceptsDocsOutOfOrder() {
return true;
}
@Override public Facet facet() {
return new InternalStatisticalFacet(name, statsProc.min(), statsProc.max(), statsProc.total(), statsProc.sumOfSquares(), statsProc.count());
return new InternalStatisticalFacet(facetName, statsProc.min(), statsProc.max(), statsProc.total(), statsProc.sumOfSquares(), statsProc.count());
}
public static class StatsProc implements NumericFieldData.DoubleValueInDocProc {

View File

@ -0,0 +1,45 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.facets.support;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.search.facets.collector.FacetCollector;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public abstract class AbstractFacetCollector extends FacetCollector {
protected final String facetName;
public AbstractFacetCollector(String facetName) {
this.facetName = facetName;
}
@Override public void setScorer(Scorer scorer) throws IOException {
// usually, there is nothing to do here
}
@Override public boolean acceptsDocsOutOfOrder() {
return true; // when working on FieldData, docs can be out of order
}
}

View File

@ -20,13 +20,12 @@
package org.elasticsearch.search.facets.terms;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.index.cache.field.FieldDataCache;
import org.elasticsearch.index.field.FieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.support.AbstractFacetCollector;
import org.elasticsearch.util.BoundedTreeSet;
import org.elasticsearch.util.ThreadLocals;
import org.elasticsearch.util.collect.ImmutableList;
@ -42,7 +41,7 @@ import static org.elasticsearch.index.field.FieldDataOptions.*;
/**
* @author kimchy (shay.banon)
*/
public class TermsFacetCollector extends FacetCollector {
public class TermsFacetCollector extends AbstractFacetCollector {
private static ThreadLocal<ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>>> cache = new ThreadLocal<ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>>>() {
@Override protected ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>> initialValue() {
@ -53,8 +52,6 @@ public class TermsFacetCollector extends FacetCollector {
private final FieldDataCache fieldDataCache;
private final String name;
private final String fieldName;
private final int size;
@ -65,8 +62,8 @@ public class TermsFacetCollector extends FacetCollector {
private final AggregatorValueProc aggregator;
public TermsFacetCollector(String name, String fieldName, int size, FieldDataCache fieldDataCache, MapperService mapperService) {
this.name = name;
public TermsFacetCollector(String facetName, String fieldName, int size, FieldDataCache fieldDataCache, MapperService mapperService) {
super(facetName);
this.fieldDataCache = fieldDataCache;
this.size = size;
@ -82,14 +79,6 @@ public class TermsFacetCollector extends FacetCollector {
aggregator = new AggregatorValueProc(popFacets());
}
@Override public void setScorer(Scorer scorer) throws IOException {
// nothing to do here
}
@Override public boolean acceptsDocsOutOfOrder() {
return true;
}
@Override public void setNextReader(IndexReader reader, int docBase) throws IOException {
fieldData = fieldDataCache.cache(fieldDataType, reader, fieldName, fieldDataOptions().withFreqs(false));
}
@ -102,7 +91,7 @@ public class TermsFacetCollector extends FacetCollector {
TObjectIntHashMap<String> facets = aggregator.facets();
if (facets.isEmpty()) {
pushFacets(facets);
return new InternalTermsFacet(name, InternalTermsFacet.ComparatorType.COUNT, size, ImmutableList.<InternalTermsFacet.Entry>of());
return new InternalTermsFacet(facetName, InternalTermsFacet.ComparatorType.COUNT, size, ImmutableList.<InternalTermsFacet.Entry>of());
} else {
BoundedTreeSet<InternalTermsFacet.Entry> ordered = new BoundedTreeSet<InternalTermsFacet.Entry>(InternalTermsFacet.ComparatorType.COUNT.comparator(), size);
for (TObjectIntIterator<String> it = facets.iterator(); it.hasNext();) {
@ -110,7 +99,7 @@ public class TermsFacetCollector extends FacetCollector {
ordered.add(new InternalTermsFacet.Entry(it.key(), it.value()));
}
pushFacets(facets);
return new InternalTermsFacet(name, InternalTermsFacet.ComparatorType.COUNT, size, ordered);
return new InternalTermsFacet(facetName, InternalTermsFacet.ComparatorType.COUNT, size, ordered);
}
}