more tuning of field data capabilities, make term facet collector handle any type (though it really make sense mainly for string types)
This commit is contained in:
parent
0901322337
commit
9f8644472e
|
@ -94,6 +94,11 @@ public abstract class FieldData {
|
|||
void onValue(String value, int freq);
|
||||
}
|
||||
|
||||
public abstract void forEachValueInDoc(int docId, StringValueInDocProc proc);
|
||||
|
||||
public static interface StringValueInDocProc {
|
||||
void onValue(String value, int docId);
|
||||
}
|
||||
|
||||
/**
|
||||
* The type of this field data.
|
||||
|
|
|
@ -55,6 +55,16 @@ public class MultiValueDoubleFieldData extends DoubleFieldData {
|
|||
return order[docId] != null;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
return;
|
||||
}
|
||||
for (int docOrder : docOrders) {
|
||||
proc.onValue(Double.toString(values[docOrder]), docId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public double value(int docId) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
|
|
|
@ -48,6 +48,14 @@ public class SingleValueDoubleFieldData extends DoubleFieldData {
|
|||
return order[docId] != 0;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int loc = order[docId];
|
||||
if (loc == 0) {
|
||||
return;
|
||||
}
|
||||
proc.onValue(Double.toString(values[loc]), docId);
|
||||
}
|
||||
|
||||
@Override public double value(int docId) {
|
||||
return values[order[docId]];
|
||||
}
|
||||
|
|
|
@ -55,6 +55,16 @@ public class MultiValueFloatFieldData extends FloatFieldData {
|
|||
return order[docId] != null;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
return;
|
||||
}
|
||||
for (int docOrder : docOrders) {
|
||||
proc.onValue(Float.toString(values[docOrder]), docId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public float value(int docId) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
|
|
|
@ -48,6 +48,14 @@ public class SingleValueFloatFieldData extends FloatFieldData {
|
|||
return order[docId] != 0;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int loc = order[docId];
|
||||
if (loc == 0) {
|
||||
return;
|
||||
}
|
||||
proc.onValue(Float.toString(values[loc]), docId);
|
||||
}
|
||||
|
||||
@Override public float value(int docId) {
|
||||
return values[order[docId]];
|
||||
}
|
||||
|
|
|
@ -55,6 +55,16 @@ public class MultiValueIntFieldData extends IntFieldData {
|
|||
return order[docId] != null;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
return;
|
||||
}
|
||||
for (int docOrder : docOrders) {
|
||||
proc.onValue(Integer.toString(values[docOrder]), docId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public int value(int docId) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
|
|
|
@ -48,6 +48,14 @@ public class SingleValueIntFieldData extends IntFieldData {
|
|||
return order[docId] != 0;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int loc = order[docId];
|
||||
if (loc == 0) {
|
||||
return;
|
||||
}
|
||||
proc.onValue(Integer.toString(values[loc]), docId);
|
||||
}
|
||||
|
||||
@Override public int value(int docId) {
|
||||
return values[order[docId]];
|
||||
}
|
||||
|
|
|
@ -55,6 +55,16 @@ public class MultiValueLongFieldData extends LongFieldData {
|
|||
return order[docId] != null;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
return;
|
||||
}
|
||||
for (int docOrder : docOrders) {
|
||||
proc.onValue(Long.toString(values[docOrder]), docId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public long value(int docId) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
|
|
|
@ -48,6 +48,14 @@ public class SingleValueLongFieldData extends LongFieldData {
|
|||
return order[docId] != 0;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int loc = order[docId];
|
||||
if (loc == 0) {
|
||||
return;
|
||||
}
|
||||
proc.onValue(Long.toString(values[loc]), docId);
|
||||
}
|
||||
|
||||
@Override public long value(int docId) {
|
||||
return values[order[docId]];
|
||||
}
|
||||
|
|
|
@ -55,6 +55,16 @@ public class MultiValueShortFieldData extends ShortFieldData {
|
|||
return order[docId] != null;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
return;
|
||||
}
|
||||
for (int docOrder : docOrders) {
|
||||
proc.onValue(Short.toString(values[docOrder]), docId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public short value(int docId) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
|
|
|
@ -48,6 +48,14 @@ public class SingleValueShortFieldData extends ShortFieldData {
|
|||
return order[docId] != 0;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int loc = order[docId];
|
||||
if (loc == 0) {
|
||||
return;
|
||||
}
|
||||
proc.onValue(Short.toString(values[loc]), docId);
|
||||
}
|
||||
|
||||
@Override public short value(int docId) {
|
||||
return values[order[docId]];
|
||||
}
|
||||
|
|
|
@ -56,6 +56,16 @@ public class MultiValueStringFieldData extends StringFieldData {
|
|||
return order[docId] != null;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
return;
|
||||
}
|
||||
for (int docOrder : docOrders) {
|
||||
proc.onValue(values[docOrder], docId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public String value(int docId) {
|
||||
int[] docOrders = order[docId];
|
||||
if (docOrders == null) {
|
||||
|
|
|
@ -49,6 +49,14 @@ public class SingleValueStringFieldData extends StringFieldData {
|
|||
return order[docId] != 0;
|
||||
}
|
||||
|
||||
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
|
||||
int loc = order[docId];
|
||||
if (loc == 0) {
|
||||
return;
|
||||
}
|
||||
proc.onValue(values[loc], docId);
|
||||
}
|
||||
|
||||
@Override public String value(int docId) {
|
||||
return values[order[docId]];
|
||||
}
|
||||
|
|
|
@ -22,7 +22,9 @@ package org.elasticsearch.search.facets.collector.term;
|
|||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.search.Scorer;
|
||||
import org.elasticsearch.index.cache.field.FieldDataCache;
|
||||
import org.elasticsearch.index.field.strings.StringFieldData;
|
||||
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.MultiCountFacet;
|
||||
import org.elasticsearch.search.facets.collector.FacetCollector;
|
||||
|
@ -59,16 +61,27 @@ public class TermFacetCollector extends FacetCollector {
|
|||
|
||||
private final int size;
|
||||
|
||||
private StringFieldData fieldData;
|
||||
private final FieldData.Type fieldDataType;
|
||||
|
||||
private final TObjectIntHashMap<String> facets;
|
||||
private FieldData fieldData;
|
||||
|
||||
public TermFacetCollector(String name, String fieldName, FieldDataCache fieldDataCache, int size) {
|
||||
private final AggregatorValueProc aggregator;
|
||||
|
||||
public TermFacetCollector(String name, String fieldName, int size, FieldDataCache fieldDataCache, MapperService mapperService) {
|
||||
this.name = name;
|
||||
this.fieldDataCache = fieldDataCache;
|
||||
this.fieldName = fieldName;
|
||||
this.size = size;
|
||||
facets = popFacets();
|
||||
|
||||
FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName);
|
||||
if (mapper != null) {
|
||||
this.fieldName = mapper.names().indexName();
|
||||
this.fieldDataType = mapper.fieldDataType();
|
||||
} else {
|
||||
this.fieldName = fieldName;
|
||||
this.fieldDataType = FieldData.Type.STRING;
|
||||
}
|
||||
|
||||
aggregator = new AggregatorValueProc(popFacets());
|
||||
}
|
||||
|
||||
@Override public void setScorer(Scorer scorer) throws IOException {
|
||||
|
@ -80,22 +93,15 @@ public class TermFacetCollector extends FacetCollector {
|
|||
}
|
||||
|
||||
@Override public void setNextReader(IndexReader reader, int docBase) throws IOException {
|
||||
fieldData = fieldDataCache.cache(StringFieldData.class, reader, fieldName, fieldDataOptions().withFreqs(false));
|
||||
fieldData = fieldDataCache.cache(fieldDataType, reader, fieldName, fieldDataOptions().withFreqs(false));
|
||||
}
|
||||
|
||||
@Override public void collect(int doc) throws IOException {
|
||||
if (fieldData.multiValued()) {
|
||||
for (String value : fieldData.values(doc)) {
|
||||
facets.adjustOrPutValue(value, 1, 1);
|
||||
}
|
||||
} else {
|
||||
if (fieldData.hasValue(doc)) {
|
||||
facets.adjustOrPutValue(fieldData.value(doc), 1, 1);
|
||||
}
|
||||
}
|
||||
fieldData.forEachValueInDoc(doc, aggregator);
|
||||
}
|
||||
|
||||
@Override public Facet facet() {
|
||||
TObjectIntHashMap<String> facets = aggregator.facets();
|
||||
if (facets.isEmpty()) {
|
||||
pushFacets(facets);
|
||||
return new InternalMultiCountFacet<String>(name, MultiCountFacet.ValueType.STRING, MultiCountFacet.ComparatorType.COUNT, size, ImmutableList.<MultiCountFacet.Entry<String>>of());
|
||||
|
@ -127,4 +133,21 @@ public class TermFacetCollector extends FacetCollector {
|
|||
deque.add(facets);
|
||||
}
|
||||
}
|
||||
|
||||
public static class AggregatorValueProc implements FieldData.StringValueInDocProc {
|
||||
|
||||
private final TObjectIntHashMap<String> facets;
|
||||
|
||||
public AggregatorValueProc(TObjectIntHashMap<String> facets) {
|
||||
this.facets = facets;
|
||||
}
|
||||
|
||||
@Override public void onValue(String value, int docId) {
|
||||
facets.adjustOrPutValue(value, 1, 1);
|
||||
}
|
||||
|
||||
public final TObjectIntHashMap<String> facets() {
|
||||
return facets;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,6 +54,6 @@ public class TermFacetCollectorParser implements FacetCollectorParser {
|
|||
}
|
||||
}
|
||||
}
|
||||
return new TermFacetCollector(facetName, field, context.fieldDataCache(), size);
|
||||
return new TermFacetCollector(facetName, field, size, context.fieldDataCache(), context.mapperService());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue