|
|
@@ -5,9 +5,16 @@
|
|
|
*/
|
|
|
package org.elasticsearch.xpack.analytics.aggregations.metrics;
|
|
|
|
|
|
-import com.tdunning.math.stats.Centroid;
|
|
|
-import com.tdunning.math.stats.TDigest;
|
|
|
-import org.apache.lucene.document.BinaryDocValuesField;
|
|
|
+import static java.util.Collections.singleton;
|
|
|
+import static org.elasticsearch.search.aggregations.AggregationBuilders.max;
|
|
|
+import static org.elasticsearch.xpack.analytics.AnalyticsTestsUtils.histogramFieldDocValues;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.Consumer;
|
|
|
+
|
|
|
import org.apache.lucene.document.Field;
|
|
|
import org.apache.lucene.document.StringField;
|
|
|
import org.apache.lucene.index.RandomIndexWriter;
|
|
|
@@ -16,14 +23,12 @@ import org.apache.lucene.search.MatchAllDocsQuery;
|
|
|
import org.apache.lucene.search.Query;
|
|
|
import org.apache.lucene.search.TermQuery;
|
|
|
import org.elasticsearch.common.CheckedConsumer;
|
|
|
-import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
import org.elasticsearch.index.mapper.MappedFieldType;
|
|
|
import org.elasticsearch.plugins.SearchPlugin;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AggregatorTestCase;
|
|
|
import org.elasticsearch.search.aggregations.metrics.InternalMax;
|
|
|
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
|
|
|
-import org.elasticsearch.search.aggregations.metrics.TDigestState;
|
|
|
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
|
|
|
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
|
|
|
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
|
|
|
@@ -31,17 +36,6 @@ import org.elasticsearch.xpack.analytics.AnalyticsPlugin;
|
|
|
import org.elasticsearch.xpack.analytics.aggregations.support.AnalyticsValuesSourceType;
|
|
|
import org.elasticsearch.xpack.analytics.mapper.HistogramFieldMapper;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
-import java.util.function.Consumer;
|
|
|
-
|
|
|
-import static java.util.Collections.singleton;
|
|
|
-import static org.elasticsearch.search.aggregations.AggregationBuilders.max;
|
|
|
-
|
|
|
public class HistoBackedMaxAggregatorTests extends AggregatorTestCase {
|
|
|
|
|
|
private static final String FIELD_NAME = "field";
|
|
|
@@ -57,8 +51,8 @@ public class HistoBackedMaxAggregatorTests extends AggregatorTestCase {
|
|
|
|
|
|
public void testNoMatchingField() throws IOException {
|
|
|
testCase(new MatchAllDocsQuery(), iw -> {
|
|
|
- iw.addDocument(singleton(getDocValue("wrong_field", new double[] {3, 1.2, 10})));
|
|
|
- iw.addDocument(singleton(getDocValue("wrong_field", new double[] {5.3, 6, 20})));
|
|
|
+ iw.addDocument(singleton(histogramFieldDocValues("wrong_field", new double[] {3, 1.2, 10})));
|
|
|
+ iw.addDocument(singleton(histogramFieldDocValues("wrong_field", new double[] {5.3, 6, 20})));
|
|
|
}, max -> {
|
|
|
assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), 0d);
|
|
|
assertFalse(AggregationInspectionHelper.hasValue(max));
|
|
|
@@ -67,9 +61,9 @@ public class HistoBackedMaxAggregatorTests extends AggregatorTestCase {
|
|
|
|
|
|
public void testSimpleHistogram() throws IOException {
|
|
|
testCase(new MatchAllDocsQuery(), iw -> {
|
|
|
- iw.addDocument(singleton(getDocValue(FIELD_NAME, new double[] {3, 1.2, 10})));
|
|
|
- iw.addDocument(singleton(getDocValue(FIELD_NAME, new double[] {5.3, 6, 6, 20})));
|
|
|
- iw.addDocument(singleton(getDocValue(FIELD_NAME, new double[] {-10, 0.01, 1, 90})));
|
|
|
+ iw.addDocument(singleton(histogramFieldDocValues(FIELD_NAME, new double[] {3, 1.2, 10})));
|
|
|
+ iw.addDocument(singleton(histogramFieldDocValues(FIELD_NAME, new double[] {5.3, 6, 6, 20})));
|
|
|
+ iw.addDocument(singleton(histogramFieldDocValues(FIELD_NAME, new double[] {-10, 0.01, 1, 90})));
|
|
|
}, max -> {
|
|
|
assertEquals(90d, max.getValue(), 0.01d);
|
|
|
assertTrue(AggregationInspectionHelper.hasValue(max));
|
|
|
@@ -80,23 +74,23 @@ public class HistoBackedMaxAggregatorTests extends AggregatorTestCase {
|
|
|
testCase(new TermQuery(new Term("match", "yes")), iw -> {
|
|
|
iw.addDocument(Arrays.asList(
|
|
|
new StringField("match", "yes", Field.Store.NO),
|
|
|
- getDocValue(FIELD_NAME, new double[] {3, 1.2, 10}))
|
|
|
+ histogramFieldDocValues(FIELD_NAME, new double[] {3, 1.2, 10}))
|
|
|
);
|
|
|
iw.addDocument(Arrays.asList(
|
|
|
new StringField("match", "yes", Field.Store.NO),
|
|
|
- getDocValue(FIELD_NAME, new double[] {5.3, 6, 20}))
|
|
|
+ histogramFieldDocValues(FIELD_NAME, new double[] {5.3, 6, 20}))
|
|
|
);
|
|
|
iw.addDocument(Arrays.asList(
|
|
|
new StringField("match", "no", Field.Store.NO),
|
|
|
- getDocValue(FIELD_NAME, new double[] {-34, 1.2, 10}))
|
|
|
+ histogramFieldDocValues(FIELD_NAME, new double[] {-34, 1.2, 10}))
|
|
|
);
|
|
|
iw.addDocument(Arrays.asList(
|
|
|
new StringField("match", "no", Field.Store.NO),
|
|
|
- getDocValue(FIELD_NAME, new double[] {3, 1.2, 100}))
|
|
|
+ histogramFieldDocValues(FIELD_NAME, new double[] {3, 1.2, 100}))
|
|
|
);
|
|
|
iw.addDocument(Arrays.asList(
|
|
|
new StringField("match", "yes", Field.Store.NO),
|
|
|
- getDocValue(FIELD_NAME, new double[] {-10, 0.01, 1, 90}))
|
|
|
+ histogramFieldDocValues(FIELD_NAME, new double[] {-10, 0.01, 1, 90}))
|
|
|
);
|
|
|
}, min -> {
|
|
|
assertEquals(90d, min.getValue(), 0.01d);
|
|
|
@@ -110,23 +104,6 @@ public class HistoBackedMaxAggregatorTests extends AggregatorTestCase {
|
|
|
testCase(max("_name").field(FIELD_NAME), query, indexer, verify, defaultFieldType());
|
|
|
}
|
|
|
|
|
|
- private BinaryDocValuesField getDocValue(String fieldName, double[] values) throws IOException {
|
|
|
- TDigest histogram = new TDigestState(100.0); //default
|
|
|
- for (double value : values) {
|
|
|
- histogram.add(value);
|
|
|
- }
|
|
|
- BytesStreamOutput streamOutput = new BytesStreamOutput();
|
|
|
- histogram.compress();
|
|
|
- Collection<Centroid> centroids = histogram.centroids();
|
|
|
- Iterator<Centroid> iterator = centroids.iterator();
|
|
|
- while ( iterator.hasNext()) {
|
|
|
- Centroid centroid = iterator.next();
|
|
|
- streamOutput.writeVInt(centroid.count());
|
|
|
- streamOutput.writeDouble(centroid.mean());
|
|
|
- }
|
|
|
- return new BinaryDocValuesField(fieldName, streamOutput.bytes().toBytesRef());
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
protected List<SearchPlugin> getSearchPlugins() {
|
|
|
return List.of(new AnalyticsPlugin());
|