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.elasticsearch.common.CacheRecycler;
import org.elasticsearch.common.joda.TimeZoneRounding;
import org.elasticsearch.index.cache.field.data.FieldDataCache;
import org.elasticsearch.index.field.data.FieldDataType;
import org.elasticsearch.index.field.data.longs.LongFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.LongValues;
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;
@ -41,48 +37,28 @@ import java.io.IOException;
*/
public class CountDateHistogramFacetCollector extends AbstractFacetCollector {
private final String indexFieldName;
private final IndexNumericFieldData indexFieldData;
private final DateHistogramFacet.ComparatorType comparatorType;
private final FieldDataCache fieldDataCache;
private final FieldDataType fieldDataType;
private LongFieldData fieldData;
private LongValues values;
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);
this.comparatorType = comparatorType;
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()));
}
FieldMapper mapper = smartMappers.mapper();
indexFieldName = mapper.names().indexName();
fieldDataType = mapper.fieldDataType();
histoProc = new DateHistogramProc(tzRounding);
this.indexFieldData = indexFieldData;
this.histoProc = new DateHistogramProc(tzRounding);
}
@Override
protected void doCollect(int doc) throws IOException {
fieldData.forEachValueInDoc(doc, histoProc);
values.forEachValueInDoc(doc, histoProc);
}
@Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException {
fieldData = (LongFieldData) fieldDataCache.cache(fieldDataType, context.reader(), indexFieldName);
values = indexFieldData.load(context).getLongValues();
}
@Override
@ -90,7 +66,7 @@ public class CountDateHistogramFacetCollector extends AbstractFacetCollector {
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();
@ -100,6 +76,10 @@ public class CountDateHistogramFacetCollector extends AbstractFacetCollector {
this.tzRounding = tzRounding;
}
@Override
public void onMissing(int docId) {
}
@Override
public void onValue(int docId, long value) {
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.unit.TimeValue;
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.script.SearchScript;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetCollector;
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) {
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);
if (mapper == null) {
FieldMapper keyMapper = context.smartNameFieldMapper(keyField);
if (keyMapper == null) {
throw new FacetPhaseExecutionException(facetName, "(key) field [" + keyField + "] not found");
}
if (mapper.fieldDataType() != FieldDataType.DefaultTypes.LONG) {
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");
}
IndexNumericFieldData keyIndexFieldData = context.fieldData().getForField(keyMapper);
TimeZoneRounding.Builder tzRoundingBuilder;
DateFieldParser fieldParser = dateFieldParsers.get(interval);
@ -171,11 +170,17 @@ public class DateHistogramFacetProcessor extends AbstractComponent implements Fa
.build();
if (valueScript != null) {
return new ValueScriptDateHistogramFacetCollector(facetName, keyField, scriptLang, valueScript, params, tzRounding, comparatorType, context);
} else if (valueField == null) {
return new CountDateHistogramFacetCollector(facetName, keyField, tzRounding, comparatorType, context);
SearchScript script = context.scriptService().search(context.lookup(), scriptLang, valueScript, params);
return new ValueScriptDateHistogramFacetCollector(facetName, keyIndexFieldData, script, 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 {
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.joda.TimeZoneRounding;
import org.elasticsearch.common.trove.ExtTLongObjectHashMap;
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.field.data.longs.LongFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.fielddata.DoubleValues;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.LongValues;
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;
@ -41,57 +37,31 @@ import java.io.IOException;
*/
public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
private final String keyIndexFieldName;
private final String valueIndexFieldName;
private final IndexNumericFieldData keyIndexFieldData;
private final IndexNumericFieldData valueIndexFieldData;
private final DateHistogramFacet.ComparatorType comparatorType;
private final FieldDataCache fieldDataCache;
private final FieldDataType keyFieldDataType;
private LongFieldData keyFieldData;
private final FieldDataType valueFieldDataType;
private LongValues keyValues;
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);
this.comparatorType = comparatorType;
this.fieldDataCache = context.fieldDataCache();
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.keyIndexFieldData = keyIndexFieldData;
this.valueIndexFieldData = valueIndexFieldData;
this.histoProc = new DateHistogramProc(tzRounding);
}
@Override
protected void doCollect(int doc) throws IOException {
keyFieldData.forEachValueInDoc(doc, histoProc);
keyValues.forEachValueInDoc(doc, histoProc);
}
@Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException {
keyFieldData = (LongFieldData) fieldDataCache.cache(keyFieldDataType, context.reader(), keyIndexFieldName);
histoProc.valueFieldData = (NumericFieldData) fieldDataCache.cache(valueFieldDataType, context.reader(), valueIndexFieldName);
keyValues = keyIndexFieldData.load(context).getLongValues();
histoProc.valueValues = valueIndexFieldData.load(context).getDoubleValues();
}
@Override
@ -99,13 +69,13 @@ public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
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();
private final TimeZoneRounding tzRounding;
NumericFieldData valueFieldData;
DoubleValues valueValues;
final ValueAggregator valueAggregator = new ValueAggregator();
@ -113,6 +83,10 @@ public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
this.tzRounding = tzRounding;
}
@Override
public void onMissing(int docId) {
}
@Override
public void onValue(int docId, long value) {
long time = tzRounding.calc(value);
@ -124,13 +98,17 @@ public class ValueDateHistogramFacetCollector extends AbstractFacetCollector {
}
entry.count++;
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;
@Override
public void onMissing(int docId) {
}
@Override
public void onValue(int docId, double value) {
entry.totalCount++;

View File

@ -24,19 +24,14 @@ import org.apache.lucene.search.Scorer;
import org.elasticsearch.common.CacheRecycler;
import org.elasticsearch.common.joda.TimeZoneRounding;
import org.elasticsearch.common.trove.ExtTLongObjectHashMap;
import org.elasticsearch.index.cache.field.data.FieldDataCache;
import org.elasticsearch.index.field.data.FieldDataType;
import org.elasticsearch.index.field.data.longs.LongFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.LongValues;
import org.elasticsearch.script.SearchScript;
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;
import java.util.Map;
/**
* 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 {
private final String indexFieldName;
private final IndexNumericFieldData keyIndexFieldData;
private final DateHistogramFacet.ComparatorType comparatorType;
private final FieldDataCache fieldDataCache;
private final FieldDataType fieldDataType;
private LongFieldData fieldData;
private LongValues keyValues;
private final SearchScript valueScript;
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);
this.comparatorType = comparatorType;
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()));
}
this.valueScript = context.scriptService().search(context.lookup(), scriptLang, valueScript, params);
FieldMapper mapper = smartMappers.mapper();
indexFieldName = mapper.names().indexName();
fieldDataType = mapper.fieldDataType();
this.keyIndexFieldData = keyIndexFieldData;
this.valueScript = valueScript;
histoProc = new DateHistogramProc(tzRounding, this.valueScript);
}
@Override
protected void doCollect(int doc) throws IOException {
fieldData.forEachValueInDoc(doc, histoProc);
keyValues.forEachValueInDoc(doc, histoProc);
}
@Override
@ -95,7 +67,7 @@ public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollect
@Override
protected void doSetNextReader(AtomicReaderContext context) throws IOException {
fieldData = (LongFieldData) fieldDataCache.cache(fieldDataType, context.reader(), indexFieldName);
keyValues = keyIndexFieldData.load(context).getLongValues();
valueScript.setNextReader(context);
}
@ -104,7 +76,7 @@ public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollect
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;
@ -117,6 +89,10 @@ public class ValueScriptDateHistogramFacetCollector extends AbstractFacetCollect
this.valueScript = valueScript;
}
@Override
public void onMissing(int docId) {
}
@Override
public void onValue(int docId, long value) {
valueScript.setNextDocId(docId);