date histogram to use new field data

This commit is contained in:
Shay Banon 2013-01-20 12:17:22 +01:00
parent 37acba1b57
commit 1765b0b813
4 changed files with 71 additions and 132 deletions

View File

@ -23,14 +23,10 @@ import gnu.trove.map.hash.TLongLongHashMap;
import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.AtomicReaderContext;
import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.CacheRecycler;
import org.elasticsearch.common.joda.TimeZoneRounding; import org.elasticsearch.common.joda.TimeZoneRounding;
import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.fielddata.LongValues;
import org.elasticsearch.index.field.data.longs.LongFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.search.facet.AbstractFacetCollector; import org.elasticsearch.search.facet.AbstractFacetCollector;
import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetPhaseExecutionException;
import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException; import java.io.IOException;
@ -41,48 +37,28 @@ import java.io.IOException;
*/ */
public class CountDateHistogramFacetCollector extends AbstractFacetCollector { public class CountDateHistogramFacetCollector extends AbstractFacetCollector {
private final String indexFieldName; private final IndexNumericFieldData indexFieldData;
private final DateHistogramFacet.ComparatorType comparatorType; private final DateHistogramFacet.ComparatorType comparatorType;
private final FieldDataCache fieldDataCache; private LongValues values;
private final FieldDataType fieldDataType;
private LongFieldData fieldData;
private final DateHistogramProc histoProc; private final DateHistogramProc histoProc;
public CountDateHistogramFacetCollector(String facetName, String fieldName, TimeZoneRounding tzRounding, DateHistogramFacet.ComparatorType comparatorType, SearchContext context) { public CountDateHistogramFacetCollector(String facetName, IndexNumericFieldData indexFieldData, TimeZoneRounding tzRounding, DateHistogramFacet.ComparatorType comparatorType, SearchContext context) {
super(facetName); super(facetName);
this.comparatorType = comparatorType; this.comparatorType = comparatorType;
this.fieldDataCache = context.fieldDataCache(); this.indexFieldData = indexFieldData;
this.histoProc = new DateHistogramProc(tzRounding);
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()));
}
FieldMapper mapper = smartMappers.mapper();
indexFieldName = mapper.names().indexName();
fieldDataType = mapper.fieldDataType();
histoProc = new DateHistogramProc(tzRounding);
} }
@Override @Override
protected void doCollect(int doc) throws IOException { protected void doCollect(int doc) throws IOException {
fieldData.forEachValueInDoc(doc, histoProc); values.forEachValueInDoc(doc, histoProc);
} }
@Override @Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException { protected void doSetNextReader(AtomicReaderContext context) throws IOException {
fieldData = (LongFieldData) fieldDataCache.cache(fieldDataType, context.reader(), indexFieldName); values = indexFieldData.load(context).getLongValues();
} }
@Override @Override
@ -90,7 +66,7 @@ public class CountDateHistogramFacetCollector extends AbstractFacetCollector {
return new InternalCountDateHistogramFacet(facetName, comparatorType, histoProc.counts(), true); return new InternalCountDateHistogramFacet(facetName, comparatorType, histoProc.counts(), true);
} }
public static class DateHistogramProc implements LongFieldData.LongValueInDocProc { public static class DateHistogramProc implements LongValues.ValueInDocProc {
private final TLongLongHashMap counts = CacheRecycler.popLongLongMap(); private final TLongLongHashMap counts = CacheRecycler.popLongLongMap();
@ -100,6 +76,10 @@ public class CountDateHistogramFacetCollector extends AbstractFacetCollector {
this.tzRounding = tzRounding; this.tzRounding = tzRounding;
} }
@Override
public void onMissing(int docId) {
}
@Override @Override
public void onValue(int docId, long value) { public void onValue(int docId, long value) {
counts.adjustOrPutValue(tzRounding.calc(value), 1, 1); counts.adjustOrPutValue(tzRounding.calc(value), 1, 1);

View File

@ -28,8 +28,9 @@ import org.elasticsearch.common.joda.TimeZoneRounding;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetCollector; import org.elasticsearch.search.facet.FacetCollector;
import org.elasticsearch.search.facet.FacetPhaseExecutionException; import org.elasticsearch.search.facet.FacetPhaseExecutionException;
@ -138,21 +139,19 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa
} }
} }
if (interval == null) {
throw new FacetPhaseExecutionException(facetName, "[interval] is required to be set for histogram facet");
}
if (keyField == null) { if (keyField == null) {
throw new FacetPhaseExecutionException(facetName, "key field is required to be set for histogram facet, either using [field] or using [key_field]"); throw new FacetPhaseExecutionException(facetName, "key field is required to be set for histogram facet, either using [field] or using [key_field]");
} }
FieldMapper mapper = context.smartNameFieldMapper(keyField); FieldMapper keyMapper = context.smartNameFieldMapper(keyField);
if (mapper == null) { if (keyMapper == null) {
throw new FacetPhaseExecutionException(facetName, "(key) field [" + keyField + "] not found"); throw new FacetPhaseExecutionException(facetName, "(key) field [" + keyField + "] not found");
} }
if (mapper.fieldDataType() != FieldDataType.DefaultTypes.LONG) { IndexNumericFieldData keyIndexFieldData = context.fieldData().getForField(keyMapper);
throw new FacetPhaseExecutionException(facetName, "(key) field [" + keyField + "] is not of type date");
}
if (interval == null) {
throw new FacetPhaseExecutionException(facetName, "[interval] is required to be set for histogram facet");
}
TimeZoneRounding.Builder tzRoundingBuilder; TimeZoneRounding.Builder tzRoundingBuilder;
DateFieldParser fieldParser = dateFieldParsers.get(interval); DateFieldParser fieldParser = dateFieldParsers.get(interval);
@ -171,11 +170,17 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa
.build(); .build();
if (valueScript != null) { if (valueScript != null) {
return new ValueScriptDateHistogramFacetCollector(facetName, keyField, scriptLang, valueScript, params, tzRounding, comparatorType, context); SearchScript script = context.scriptService().search(context.lookup(), scriptLang, valueScript, params);
} else if (valueField == null) { return new ValueScriptDateHistogramFacetCollector(facetName, keyIndexFieldData, script, tzRounding, comparatorType, context);
return new CountDateHistogramFacetCollector(facetName, keyField, tzRounding, comparatorType, context); } else if (valueField != null) {
FieldMapper valueMapper = context.smartNameFieldMapper(valueField);
if (valueMapper == null) {
throw new FacetPhaseExecutionException(facetName, "(value) field [" + valueField + "] not found");
}
IndexNumericFieldData valueIndexFieldData = context.fieldData().getForField(valueMapper);
return new ValueDateHistogramFacetCollector(facetName, keyIndexFieldData, valueIndexFieldData, tzRounding, comparatorType, context);
} else { } else {
return new ValueDateHistogramFacetCollector(facetName, keyField, valueField, tzRounding, comparatorType, context); return new CountDateHistogramFacetCollector(facetName, keyIndexFieldData, tzRounding, comparatorType, context);
} }
} }

View File

@ -23,15 +23,11 @@ import org.apache.lucene.index.AtomicReaderContext;
import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.CacheRecycler;
import org.elasticsearch.common.joda.TimeZoneRounding; import org.elasticsearch.common.joda.TimeZoneRounding;
import org.elasticsearch.common.trove.ExtTLongObjectHashMap; import org.elasticsearch.common.trove.ExtTLongObjectHashMap;
import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.fielddata.DoubleValues;
import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.fielddata.LongValues;
import org.elasticsearch.index.field.data.longs.LongFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.search.facet.AbstractFacetCollector; import org.elasticsearch.search.facet.AbstractFacetCollector;
import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetPhaseExecutionException;
import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException; import java.io.IOException;
@ -41,57 +37,31 @@ import java.io.IOException;
*/ */
public class ValueDateHistogramFacetCollector extends AbstractFacetCollector { public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
private final String keyIndexFieldName; private final IndexNumericFieldData keyIndexFieldData;
private final String valueIndexFieldName; private final IndexNumericFieldData valueIndexFieldData;
private final DateHistogramFacet.ComparatorType comparatorType; private final DateHistogramFacet.ComparatorType comparatorType;
private final FieldDataCache fieldDataCache; private LongValues keyValues;
private final FieldDataType keyFieldDataType;
private LongFieldData keyFieldData;
private final FieldDataType valueFieldDataType;
private final DateHistogramProc histoProc; private final DateHistogramProc histoProc;
public ValueDateHistogramFacetCollector(String facetName, String keyFieldName, String valueFieldName, TimeZoneRounding tzRounding, DateHistogramFacet.ComparatorType comparatorType, SearchContext context) { public ValueDateHistogramFacetCollector(String facetName, IndexNumericFieldData keyIndexFieldData, IndexNumericFieldData valueIndexFieldData, TimeZoneRounding tzRounding, DateHistogramFacet.ComparatorType comparatorType, SearchContext context) {
super(facetName); super(facetName);
this.comparatorType = comparatorType; this.comparatorType = comparatorType;
this.fieldDataCache = context.fieldDataCache(); this.keyIndexFieldData = keyIndexFieldData;
this.valueIndexFieldData = valueIndexFieldData;
MapperService.SmartNameFieldMappers smartMappers = context.smartFieldMappers(keyFieldName);
if (smartMappers == null || !smartMappers.hasMapper()) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + keyFieldName + "]");
}
// add type filter if there is exact doc mapper associated with it
if (smartMappers.explicitTypeInNameWithDocMapper()) {
setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter()));
}
keyIndexFieldName = smartMappers.mapper().names().indexName();
keyFieldDataType = smartMappers.mapper().fieldDataType();
FieldMapper mapper = context.smartNameFieldMapper(valueFieldName);
if (mapper == null) {
throw new FacetPhaseExecutionException(facetName, "No mapping found for value_field [" + valueFieldName + "]");
}
valueIndexFieldName = mapper.names().indexName();
valueFieldDataType = mapper.fieldDataType();
this.histoProc = new DateHistogramProc(tzRounding); this.histoProc = new DateHistogramProc(tzRounding);
} }
@Override @Override
protected void doCollect(int doc) throws IOException { protected void doCollect(int doc) throws IOException {
keyFieldData.forEachValueInDoc(doc, histoProc); keyValues.forEachValueInDoc(doc, histoProc);
} }
@Override @Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException { protected void doSetNextReader(AtomicReaderContext context) throws IOException {
keyFieldData = (LongFieldData) fieldDataCache.cache(keyFieldDataType, context.reader(), keyIndexFieldName); keyValues = keyIndexFieldData.load(context).getLongValues();
histoProc.valueFieldData = (NumericFieldData) fieldDataCache.cache(valueFieldDataType, context.reader(), valueIndexFieldName); histoProc.valueValues = valueIndexFieldData.load(context).getDoubleValues();
} }
@Override @Override
@ -99,13 +69,13 @@ public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
return new InternalFullDateHistogramFacet(facetName, comparatorType, histoProc.entries, true); return new InternalFullDateHistogramFacet(facetName, comparatorType, histoProc.entries, true);
} }
public static class DateHistogramProc implements LongFieldData.LongValueInDocProc { public static class DateHistogramProc implements LongValues.ValueInDocProc {
final ExtTLongObjectHashMap<InternalFullDateHistogramFacet.FullEntry> entries = CacheRecycler.popLongObjectMap(); final ExtTLongObjectHashMap<InternalFullDateHistogramFacet.FullEntry> entries = CacheRecycler.popLongObjectMap();
private final TimeZoneRounding tzRounding; private final TimeZoneRounding tzRounding;
NumericFieldData valueFieldData; DoubleValues valueValues;
final ValueAggregator valueAggregator = new ValueAggregator(); final ValueAggregator valueAggregator = new ValueAggregator();
@ -113,6 +83,10 @@ public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
this.tzRounding = tzRounding; this.tzRounding = tzRounding;
} }
@Override
public void onMissing(int docId) {
}
@Override @Override
public void onValue(int docId, long value) { public void onValue(int docId, long value) {
long time = tzRounding.calc(value); long time = tzRounding.calc(value);
@ -124,13 +98,17 @@ public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
} }
entry.count++; entry.count++;
valueAggregator.entry = entry; valueAggregator.entry = entry;
valueFieldData.forEachValueInDoc(docId, valueAggregator); valueValues.forEachValueInDoc(docId, valueAggregator);
} }
public static class ValueAggregator implements NumericFieldData.DoubleValueInDocProc { public static class ValueAggregator implements DoubleValues.ValueInDocProc {
InternalFullDateHistogramFacet.FullEntry entry; InternalFullDateHistogramFacet.FullEntry entry;
@Override
public void onMissing(int docId) {
}
@Override @Override
public void onValue(int docId, double value) { public void onValue(int docId, double value) {
entry.totalCount++; entry.totalCount++;

View File

@ -24,19 +24,14 @@ import org.apache.lucene.search.Scorer;
import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.CacheRecycler;
import org.elasticsearch.common.joda.TimeZoneRounding; import org.elasticsearch.common.joda.TimeZoneRounding;
import org.elasticsearch.common.trove.ExtTLongObjectHashMap; import org.elasticsearch.common.trove.ExtTLongObjectHashMap;
import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.fielddata.LongValues;
import org.elasticsearch.index.field.data.longs.LongFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.script.SearchScript; import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.facet.AbstractFacetCollector; import org.elasticsearch.search.facet.AbstractFacetCollector;
import org.elasticsearch.search.facet.Facet; import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetPhaseExecutionException;
import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
/** /**
* A histogram facet collector that uses the same field as the key as well as the * A histogram facet collector that uses the same field as the key as well as the
@ -44,48 +39,25 @@ import java.util.Map;
*/ */
public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollector { public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollector {
private final String indexFieldName; private final IndexNumericFieldData keyIndexFieldData;
private final DateHistogramFacet.ComparatorType comparatorType; private final DateHistogramFacet.ComparatorType comparatorType;
private final FieldDataCache fieldDataCache; private LongValues keyValues;
private final FieldDataType fieldDataType;
private LongFieldData fieldData;
private final SearchScript valueScript; private final SearchScript valueScript;
private final DateHistogramProc histoProc; private final DateHistogramProc histoProc;
public ValueScriptDateHistogramFacetCollector(String facetName, String fieldName, String scriptLang, String valueScript, Map<String, Object> params, TimeZoneRounding tzRounding, DateHistogramFacet.ComparatorType comparatorType, SearchContext context) { public ValueScriptDateHistogramFacetCollector(String facetName, IndexNumericFieldData keyIndexFieldData, SearchScript valueScript, TimeZoneRounding tzRounding, DateHistogramFacet.ComparatorType comparatorType, SearchContext context) {
super(facetName); super(facetName);
this.comparatorType = comparatorType; this.comparatorType = comparatorType;
this.fieldDataCache = context.fieldDataCache(); this.keyIndexFieldData = keyIndexFieldData;
this.valueScript = valueScript;
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()));
}
this.valueScript = context.scriptService().search(context.lookup(), scriptLang, valueScript, params);
FieldMapper mapper = smartMappers.mapper();
indexFieldName = mapper.names().indexName();
fieldDataType = mapper.fieldDataType();
histoProc = new DateHistogramProc(tzRounding, this.valueScript); histoProc = new DateHistogramProc(tzRounding, this.valueScript);
} }
@Override @Override
protected void doCollect(int doc) throws IOException { protected void doCollect(int doc) throws IOException {
fieldData.forEachValueInDoc(doc, histoProc); keyValues.forEachValueInDoc(doc, histoProc);
} }
@Override @Override
@ -95,7 +67,7 @@ public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollect
@Override @Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException { protected void doSetNextReader(AtomicReaderContext context) throws IOException {
fieldData = (LongFieldData) fieldDataCache.cache(fieldDataType, context.reader(), indexFieldName); keyValues = keyIndexFieldData.load(context).getLongValues();
valueScript.setNextReader(context); valueScript.setNextReader(context);
} }
@ -104,7 +76,7 @@ public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollect
return new InternalFullDateHistogramFacet(facetName, comparatorType, histoProc.entries, true); return new InternalFullDateHistogramFacet(facetName, comparatorType, histoProc.entries, true);
} }
public static class DateHistogramProc implements LongFieldData.LongValueInDocProc { public static class DateHistogramProc implements LongValues.ValueInDocProc {
private final TimeZoneRounding tzRounding; private final TimeZoneRounding tzRounding;
@ -117,6 +89,10 @@ public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollect
this.valueScript = valueScript; this.valueScript = valueScript;
} }
@Override
public void onMissing(int docId) {
}
@Override @Override
public void onValue(int docId, long value) { public void onValue(int docId, long value) {
valueScript.setNextDocId(docId); valueScript.setNextDocId(docId);