move statistical facet to use new field data

This commit is contained in:
Shay Banon 2013-01-20 06:00:57 +01:00
parent 699ff2782e
commit f1f86efed5
4 changed files with 38 additions and 73 deletions

View File

@ -35,15 +35,10 @@ import java.util.Map;
public class ScriptStatisticalFacetCollector extends AbstractFacetCollector {
private final SearchScript script;
private double min = Double.POSITIVE_INFINITY;
private double max = Double.NEGATIVE_INFINITY;
private double total = 0;
private double sumOfSquares = 0.0;
private long count;
public ScriptStatisticalFacetCollector(String facetName, String scriptLang, String script, Map<String, Object> params, SearchContext context) {

View File

@ -20,13 +20,10 @@
package org.elasticsearch.search.facet.statistical;
import org.apache.lucene.index.AtomicReaderContext;
import org.elasticsearch.index.cache.field.data.FieldDataCache;
import org.elasticsearch.index.field.data.FieldDataType;
import org.elasticsearch.index.field.data.NumericFieldData;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.fielddata.DoubleValues;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.search.facet.AbstractFacetCollector;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetPhaseExecutionException;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
@ -36,42 +33,25 @@ import java.io.IOException;
*/
public class StatisticalFacetCollector extends AbstractFacetCollector {
private final String indexFieldName;
private final IndexNumericFieldData indexFieldData;
private final FieldDataCache fieldDataCache;
private final FieldDataType fieldDataType;
private NumericFieldData fieldData;
private DoubleValues values;
private final StatsProc statsProc = new StatsProc();
public StatisticalFacetCollector(String facetName, String fieldName, SearchContext context) {
public StatisticalFacetCollector(String facetName, IndexNumericFieldData indexFieldData, SearchContext context) {
super(facetName);
this.fieldDataCache = context.fieldDataCache();
MapperService.SmartNameFieldMappers smartMappers = context.smartFieldMappers(fieldName);
if (smartMappers == null || !smartMappers.hasMapper()) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldName + "]");
}
// add type filter if there is exact doc mapper associated with it
if (smartMappers.explicitTypeInNameWithDocMapper()) {
setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter()));
}
indexFieldName = smartMappers.mapper().names().indexName();
fieldDataType = smartMappers.mapper().fieldDataType();
this.indexFieldData = indexFieldData;
}
@Override
protected void doCollect(int doc) throws IOException {
fieldData.forEachValueInDoc(doc, statsProc);
values.forEachValueInDoc(doc, statsProc);
}
@Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException {
fieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, context.reader(), indexFieldName);
values = indexFieldData.load(context).getDoubleValues();
}
@Override
@ -79,7 +59,7 @@ public class StatisticalFacetCollector extends AbstractFacetCollector {
return new InternalStatisticalFacet(facetName, statsProc.min(), statsProc.max(), statsProc.total(), statsProc.sumOfSquares(), statsProc.count());
}
public static class StatsProc implements NumericFieldData.MissingDoubleValueInDocProc {
public static class StatsProc implements DoubleValues.ValueInDocProc {
double min = Double.POSITIVE_INFINITY;

View File

@ -24,6 +24,8 @@ import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetCollector;
import org.elasticsearch.search.facet.FacetPhaseExecutionException;
@ -87,13 +89,26 @@ public class StatisticalFacetProcessor extends AbstractComponent implements Face
}
}
if (fieldsNames != null) {
return new StatisticalFieldsFacetCollector(facetName, fieldsNames, context);
IndexNumericFieldData[] indexFieldDatas = new IndexNumericFieldData[fieldsNames.length];
for (int i = 0; i < fieldsNames.length; i++) {
FieldMapper fieldMapper = context.mapperService().smartNameFieldMapper(fieldsNames[i]);
if (fieldMapper == null) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldsNames[i] + "]");
}
indexFieldDatas[i] = context.fieldData().getForField(fieldMapper);
}
return new StatisticalFieldsFacetCollector(facetName, indexFieldDatas, context);
}
if (script == null && field == null) {
throw new FacetPhaseExecutionException(facetName, "statistical facet requires either [script] or [field] to be set");
}
if (field != null) {
return new StatisticalFacetCollector(facetName, field, context);
FieldMapper fieldMapper = context.mapperService().smartNameFieldMapper(field);
if (fieldMapper == null) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + field + "]");
}
IndexNumericFieldData indexFieldData = context.fieldData().getForField(fieldMapper);
return new StatisticalFacetCollector(facetName, indexFieldData, context);
} else {
return new ScriptStatisticalFacetCollector(facetName, scriptLang, script, params, context);
}

View File

@ -20,13 +20,10 @@
package org.elasticsearch.search.facet.statistical;
import org.apache.lucene.index.AtomicReaderContext;
import org.elasticsearch.index.cache.field.data.FieldDataCache;
import org.elasticsearch.index.field.data.FieldDataType;
import org.elasticsearch.index.field.data.NumericFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.fielddata.DoubleValues;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.search.facet.AbstractFacetCollector;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetPhaseExecutionException;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
@ -36,46 +33,29 @@ import java.io.IOException;
*/
public class StatisticalFieldsFacetCollector extends AbstractFacetCollector {
private final String[] indexFieldsNames;
private final IndexNumericFieldData[] indexFieldDatas;
private final FieldDataCache fieldDataCache;
private final FieldDataType[] fieldsDataType;
private NumericFieldData[] fieldsData;
private DoubleValues[] values;
private final StatsProc statsProc = new StatsProc();
public StatisticalFieldsFacetCollector(String facetName, String[] fieldsNames, SearchContext context) {
public StatisticalFieldsFacetCollector(String facetName, IndexNumericFieldData[] indexFieldDatas, SearchContext context) {
super(facetName);
this.fieldDataCache = context.fieldDataCache();
fieldsDataType = new FieldDataType[fieldsNames.length];
fieldsData = new NumericFieldData[fieldsNames.length];
indexFieldsNames = new String[fieldsNames.length];
for (int i = 0; i < fieldsNames.length; i++) {
FieldMapper mapper = context.smartNameFieldMapper(fieldsNames[i]);
if (mapper == null) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldsNames[i] + "]");
}
indexFieldsNames[i] = mapper.names().indexName();
fieldsDataType[i] = mapper.fieldDataType();
}
this.indexFieldDatas = indexFieldDatas;
this.values = new DoubleValues[indexFieldDatas.length];
}
@Override
protected void doCollect(int doc) throws IOException {
for (NumericFieldData fieldData : fieldsData) {
fieldData.forEachValueInDoc(doc, statsProc);
for (DoubleValues value : values) {
value.forEachValueInDoc(doc, statsProc);
}
}
@Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException {
for (int i = 0; i < indexFieldsNames.length; i++) {
fieldsData[i] = (NumericFieldData) fieldDataCache.cache(fieldsDataType[i], context.reader(), indexFieldsNames[i]);
for (int i = 0; i < indexFieldDatas.length; i++) {
values[i] = indexFieldDatas[i].load(context).getDoubleValues();
}
}
@ -84,18 +64,13 @@ public class StatisticalFieldsFacetCollector extends AbstractFacetCollector {
return new InternalStatisticalFacet(facetName, statsProc.min(), statsProc.max(), statsProc.total(), statsProc.sumOfSquares(), statsProc.count());
}
public static class StatsProc implements NumericFieldData.MissingDoubleValueInDocProc {
public static class StatsProc implements DoubleValues.ValueInDocProc {
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
double total = 0;
double sumOfSquares = 0.0;
long count;
int missing;
@Override