Histogram Facet: Add min/max stats when providing value field / script, closes #830.
This commit is contained in:
parent
46088b9f8a
commit
8d1e9dbd3c
|
@ -108,12 +108,18 @@ public class FullHistogramFacetCollector extends AbstractFacetCollector {
|
||||||
long bucket = bucket(value, interval);
|
long bucket = bucket(value, interval);
|
||||||
InternalFullHistogramFacet.FullEntry entry = entries.get(bucket);
|
InternalFullHistogramFacet.FullEntry entry = entries.get(bucket);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, 1, value);
|
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, value, value, 1, value);
|
||||||
entries.put(bucket, entry);
|
entries.put(bucket, entry);
|
||||||
} else {
|
} else {
|
||||||
entry.count++;
|
entry.count++;
|
||||||
entry.totalCount++;
|
entry.totalCount++;
|
||||||
entry.total += value;
|
entry.total += value;
|
||||||
|
if (value < entry.min) {
|
||||||
|
entry.min = value;
|
||||||
|
}
|
||||||
|
if (value > entry.max) {
|
||||||
|
entry.max = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -186,5 +186,25 @@ public interface HistogramFacet extends Facet, Iterable<HistogramFacet.Entry> {
|
||||||
* The mean of this facet interval.
|
* The mean of this facet interval.
|
||||||
*/
|
*/
|
||||||
double getMean();
|
double getMean();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum value.
|
||||||
|
*/
|
||||||
|
double min();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum value.
|
||||||
|
*/
|
||||||
|
double getMin();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum value.
|
||||||
|
*/
|
||||||
|
double max();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum value.
|
||||||
|
*/
|
||||||
|
double getMax();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -105,6 +105,22 @@ public class InternalCountHistogramFacet extends InternalHistogramFacet {
|
||||||
@Override public double getMean() {
|
@Override public double getMean() {
|
||||||
return mean();
|
return mean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public double min() {
|
||||||
|
return Double.NaN;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public double getMin() {
|
||||||
|
return Double.NaN;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public double max() {
|
||||||
|
return Double.NaN;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public double getMax() {
|
||||||
|
return Double.NaN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
|
@ -61,10 +61,14 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet {
|
||||||
long count;
|
long count;
|
||||||
long totalCount;
|
long totalCount;
|
||||||
double total;
|
double total;
|
||||||
|
double min = Double.MAX_VALUE;
|
||||||
|
double max = Double.MIN_VALUE;
|
||||||
|
|
||||||
public FullEntry(long key, long count, long totalCount, double total) {
|
public FullEntry(long key, long count, double min, double max, long totalCount, double total) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.count = count;
|
this.count = count;
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
this.totalCount = totalCount;
|
this.totalCount = totalCount;
|
||||||
this.total = total;
|
this.total = total;
|
||||||
}
|
}
|
||||||
|
@ -108,6 +112,22 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet {
|
||||||
@Override public double getMean() {
|
@Override public double getMean() {
|
||||||
return total / totalCount;
|
return total / totalCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public double min() {
|
||||||
|
return this.min;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public double getMin() {
|
||||||
|
return this.min;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public double max() {
|
||||||
|
return this.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public double getMax() {
|
||||||
|
return this.max;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
@ -178,6 +198,12 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet {
|
||||||
current.count += fullEntry.count;
|
current.count += fullEntry.count;
|
||||||
current.total += fullEntry.total;
|
current.total += fullEntry.total;
|
||||||
current.totalCount += fullEntry.totalCount;
|
current.totalCount += fullEntry.totalCount;
|
||||||
|
if (fullEntry.min < current.min) {
|
||||||
|
current.min = fullEntry.min;
|
||||||
|
}
|
||||||
|
if (fullEntry.max > current.max) {
|
||||||
|
current.max = fullEntry.max;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
map.put(fullEntry.key, fullEntry);
|
map.put(fullEntry.key, fullEntry);
|
||||||
}
|
}
|
||||||
|
@ -209,6 +235,8 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet {
|
||||||
static final XContentBuilderString TOTAL = new XContentBuilderString("total");
|
static final XContentBuilderString TOTAL = new XContentBuilderString("total");
|
||||||
static final XContentBuilderString TOTAL_COUNT = new XContentBuilderString("total_count");
|
static final XContentBuilderString TOTAL_COUNT = new XContentBuilderString("total_count");
|
||||||
static final XContentBuilderString MEAN = new XContentBuilderString("mean");
|
static final XContentBuilderString MEAN = new XContentBuilderString("mean");
|
||||||
|
static final XContentBuilderString MIN = new XContentBuilderString("min");
|
||||||
|
static final XContentBuilderString MAX = new XContentBuilderString("max");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
|
@ -219,6 +247,8 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet {
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
builder.field(Fields.KEY, entry.key());
|
builder.field(Fields.KEY, entry.key());
|
||||||
builder.field(Fields.COUNT, entry.count());
|
builder.field(Fields.COUNT, entry.count());
|
||||||
|
builder.field(Fields.MIN, entry.min());
|
||||||
|
builder.field(Fields.MAX, entry.max());
|
||||||
builder.field(Fields.TOTAL, entry.total());
|
builder.field(Fields.TOTAL, entry.total());
|
||||||
builder.field(Fields.TOTAL_COUNT, entry.totalCount());
|
builder.field(Fields.TOTAL_COUNT, entry.totalCount());
|
||||||
builder.field(Fields.MEAN, entry.mean());
|
builder.field(Fields.MEAN, entry.mean());
|
||||||
|
@ -242,7 +272,7 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet {
|
||||||
int size = in.readVInt();
|
int size = in.readVInt();
|
||||||
entries = new ArrayList<FullEntry>(size);
|
entries = new ArrayList<FullEntry>(size);
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
entries.add(new FullEntry(in.readLong(), in.readVLong(), in.readVLong(), in.readDouble()));
|
entries.add(new FullEntry(in.readLong(), in.readVLong(), in.readDouble(), in.readDouble(), in.readVLong(), in.readDouble()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,6 +284,8 @@ public class InternalFullHistogramFacet extends InternalHistogramFacet {
|
||||||
for (FullEntry entry : entries) {
|
for (FullEntry entry : entries) {
|
||||||
out.writeLong(entry.key);
|
out.writeLong(entry.key);
|
||||||
out.writeVLong(entry.count);
|
out.writeVLong(entry.count);
|
||||||
|
out.writeDouble(entry.min);
|
||||||
|
out.writeDouble(entry.max);
|
||||||
out.writeVLong(entry.totalCount);
|
out.writeVLong(entry.totalCount);
|
||||||
out.writeDouble(entry.total);
|
out.writeDouble(entry.total);
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,14 +119,20 @@ public class KeyValueHistogramFacetCollector extends AbstractFacetCollector {
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
if (valueFieldData.multiValued()) {
|
if (valueFieldData.multiValued()) {
|
||||||
double[] valuesValues = valueFieldData.doubleValues(docId);
|
double[] valuesValues = valueFieldData.doubleValues(docId);
|
||||||
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, valuesValues.length, 0);
|
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, Double.MAX_VALUE, Double.MIN_VALUE, valuesValues.length, 0);
|
||||||
for (double valueValue : valuesValues) {
|
for (double valueValue : valuesValues) {
|
||||||
entry.total += valueValue;
|
entry.total += valueValue;
|
||||||
|
if (valueValue < entry.min) {
|
||||||
|
entry.min = valueValue;
|
||||||
|
}
|
||||||
|
if (valueValue > entry.max) {
|
||||||
|
entry.max = valueValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
entries.put(bucket, entry);
|
entries.put(bucket, entry);
|
||||||
} else {
|
} else {
|
||||||
double valueValue = valueFieldData.doubleValue(docId);
|
double valueValue = valueFieldData.doubleValue(docId);
|
||||||
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, 1, valueValue);
|
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, valueValue, valueValue, 1, valueValue);
|
||||||
entries.put(bucket, entry);
|
entries.put(bucket, entry);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -136,11 +142,23 @@ public class KeyValueHistogramFacetCollector extends AbstractFacetCollector {
|
||||||
entry.totalCount += valuesValues.length;
|
entry.totalCount += valuesValues.length;
|
||||||
for (double valueValue : valuesValues) {
|
for (double valueValue : valuesValues) {
|
||||||
entry.total += valueValue;
|
entry.total += valueValue;
|
||||||
|
if (valueValue < entry.min) {
|
||||||
|
entry.min = valueValue;
|
||||||
|
}
|
||||||
|
if (valueValue > entry.max) {
|
||||||
|
entry.max = valueValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
entry.totalCount++;
|
entry.totalCount++;
|
||||||
double valueValue = valueFieldData.doubleValue(docId);
|
double valueValue = valueFieldData.doubleValue(docId);
|
||||||
entry.total += valueValue;
|
entry.total += valueValue;
|
||||||
|
if (valueValue < entry.min) {
|
||||||
|
entry.min = valueValue;
|
||||||
|
}
|
||||||
|
if (valueValue > entry.max) {
|
||||||
|
entry.max = valueValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,12 +126,18 @@ public class KeyValueScriptHistogramFacetCollector extends AbstractFacetCollecto
|
||||||
|
|
||||||
InternalFullHistogramFacet.FullEntry entry = entries.get(bucket);
|
InternalFullHistogramFacet.FullEntry entry = entries.get(bucket);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, 1, scriptValue);
|
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, scriptValue, scriptValue, 1, scriptValue);
|
||||||
entries.put(bucket, entry);
|
entries.put(bucket, entry);
|
||||||
} else {
|
} else {
|
||||||
entry.count++;
|
entry.count++;
|
||||||
entry.totalCount++;
|
entry.totalCount++;
|
||||||
entry.total += scriptValue;
|
entry.total += scriptValue;
|
||||||
|
if (scriptValue < entry.min) {
|
||||||
|
entry.min = scriptValue;
|
||||||
|
}
|
||||||
|
if (scriptValue > entry.max) {
|
||||||
|
entry.max = scriptValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,12 +67,18 @@ public class ScriptHistogramFacetCollector extends AbstractFacetCollector {
|
||||||
|
|
||||||
InternalFullHistogramFacet.FullEntry entry = entries.get(bucket);
|
InternalFullHistogramFacet.FullEntry entry = entries.get(bucket);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, 1, value);
|
entry = new InternalFullHistogramFacet.FullEntry(bucket, 1, value, value, 1, value);
|
||||||
entries.put(bucket, entry);
|
entries.put(bucket, entry);
|
||||||
} else {
|
} else {
|
||||||
entry.count++;
|
entry.count++;
|
||||||
entry.totalCount++;
|
entry.totalCount++;
|
||||||
entry.total += value;
|
entry.total += value;
|
||||||
|
if (value < entry.min) {
|
||||||
|
entry.min = value;
|
||||||
|
}
|
||||||
|
if (value > entry.max) {
|
||||||
|
entry.max = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -948,11 +948,15 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
||||||
assertThat(facet.entries().size(), equalTo(2));
|
assertThat(facet.entries().size(), equalTo(2));
|
||||||
assertThat(facet.entries().get(0).key(), equalTo(1000l));
|
assertThat(facet.entries().get(0).key(), equalTo(1000l));
|
||||||
assertThat(facet.entries().get(0).count(), equalTo(2l));
|
assertThat(facet.entries().get(0).count(), equalTo(2l));
|
||||||
|
assertThat(facet.entries().get(0).min(), closeTo(1055d, 0.000001));
|
||||||
|
assertThat(facet.entries().get(0).max(), closeTo(1065d, 0.000001));
|
||||||
assertThat(facet.entries().get(0).totalCount(), equalTo(2l));
|
assertThat(facet.entries().get(0).totalCount(), equalTo(2l));
|
||||||
assertThat(facet.entries().get(0).total(), equalTo(2120d));
|
assertThat(facet.entries().get(0).total(), equalTo(2120d));
|
||||||
assertThat(facet.entries().get(0).mean(), equalTo(1060d));
|
assertThat(facet.entries().get(0).mean(), equalTo(1060d));
|
||||||
assertThat(facet.entries().get(1).key(), equalTo(1100l));
|
assertThat(facet.entries().get(1).key(), equalTo(1100l));
|
||||||
assertThat(facet.entries().get(1).count(), equalTo(1l));
|
assertThat(facet.entries().get(1).count(), equalTo(1l));
|
||||||
|
assertThat(facet.entries().get(1).min(), closeTo(1175d, 0.000001));
|
||||||
|
assertThat(facet.entries().get(1).max(), closeTo(1175d, 0.000001));
|
||||||
assertThat(facet.entries().get(1).totalCount(), equalTo(1l));
|
assertThat(facet.entries().get(1).totalCount(), equalTo(1l));
|
||||||
assertThat(facet.entries().get(1).total(), equalTo(1175d));
|
assertThat(facet.entries().get(1).total(), equalTo(1175d));
|
||||||
assertThat(facet.entries().get(1).mean(), equalTo(1175d));
|
assertThat(facet.entries().get(1).mean(), equalTo(1175d));
|
||||||
|
|
Loading…
Reference in New Issue