|
7 | 7 | package org.elasticsearch.xpack.dataframe.transforms.pivot;
|
8 | 8 |
|
9 | 9 | import org.elasticsearch.common.ParseField;
|
| 10 | +import org.elasticsearch.common.geo.GeoPoint; |
10 | 11 | import org.elasticsearch.common.xcontent.ContextParser;
|
11 | 12 | import org.elasticsearch.common.xcontent.DeprecationHandler;
|
12 | 13 | import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
31 | 32 | import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
|
32 | 33 | import org.elasticsearch.search.aggregations.metrics.CardinalityAggregationBuilder;
|
33 | 34 | import org.elasticsearch.search.aggregations.metrics.ExtendedStatsAggregationBuilder;
|
| 35 | +import org.elasticsearch.search.aggregations.metrics.GeoBounds; |
| 36 | +import org.elasticsearch.search.aggregations.metrics.GeoCentroid; |
34 | 37 | import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
|
35 | 38 | import org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder;
|
| 39 | +import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; |
36 | 40 | import org.elasticsearch.search.aggregations.metrics.ParsedAvg;
|
37 | 41 | import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
|
38 | 42 | import org.elasticsearch.search.aggregations.metrics.ParsedExtendedStats;
|
|
42 | 46 | import org.elasticsearch.search.aggregations.metrics.ParsedStats;
|
43 | 47 | import org.elasticsearch.search.aggregations.metrics.ParsedSum;
|
44 | 48 | import org.elasticsearch.search.aggregations.metrics.ParsedValueCount;
|
| 49 | +import org.elasticsearch.search.aggregations.metrics.ScriptedMetric; |
45 | 50 | import org.elasticsearch.search.aggregations.metrics.ScriptedMetricAggregationBuilder;
|
46 | 51 | import org.elasticsearch.search.aggregations.metrics.StatsAggregationBuilder;
|
47 | 52 | import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
|
|
56 | 61 | import org.elasticsearch.xpack.core.dataframe.transforms.pivot.GroupConfig;
|
57 | 62 |
|
58 | 63 | import java.io.IOException;
|
| 64 | +import java.util.Arrays; |
59 | 65 | import java.util.Collection;
|
60 | 66 | import java.util.Collections;
|
61 | 67 | import java.util.HashMap;
|
|
67 | 73 |
|
68 | 74 | import static java.util.Arrays.asList;
|
69 | 75 | import static org.hamcrest.CoreMatchers.equalTo;
|
| 76 | +import static org.hamcrest.CoreMatchers.hasItem; |
| 77 | +import static org.hamcrest.CoreMatchers.is; |
| 78 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 79 | +import static org.mockito.Mockito.mock; |
| 80 | +import static org.mockito.Mockito.when; |
70 | 81 |
|
71 | 82 | public class AggregationResultUtilsTests extends ESTestCase {
|
72 | 83 |
|
@@ -781,6 +792,151 @@ public void testUpdateDocumentWithObjectAndNotObject() {
|
781 | 792 | equalTo("mixed object types of nested and non-nested fields [foo.bar]"));
|
782 | 793 | }
|
783 | 794 |
|
| 795 | + private NumericMetricsAggregation.SingleValue createSingleMetricAgg(Double value, String valueAsString) { |
| 796 | + NumericMetricsAggregation.SingleValue agg = mock(NumericMetricsAggregation.SingleValue.class); |
| 797 | + when(agg.value()).thenReturn(value); |
| 798 | + when(agg.getValueAsString()).thenReturn(valueAsString); |
| 799 | + return agg; |
| 800 | + } |
| 801 | + |
| 802 | + public void testSingleValueAggExtractor() { |
| 803 | + Aggregation agg = createSingleMetricAgg(Double.NaN, "NaN"); |
| 804 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "double"), is(nullValue())); |
| 805 | + |
| 806 | + agg = createSingleMetricAgg(Double.POSITIVE_INFINITY, "NaN"); |
| 807 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "double"), is(nullValue())); |
| 808 | + |
| 809 | + agg = createSingleMetricAgg(100.0, "100.0"); |
| 810 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "double"), equalTo(100.0)); |
| 811 | + |
| 812 | + agg = createSingleMetricAgg(100.0, "one_hundred"); |
| 813 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "double"), equalTo(100.0)); |
| 814 | + |
| 815 | + agg = createSingleMetricAgg(100.0, "one_hundred"); |
| 816 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "string"), equalTo("one_hundred")); |
| 817 | + } |
| 818 | + |
| 819 | + private ScriptedMetric createScriptedMetric(Object returnValue) { |
| 820 | + ScriptedMetric agg = mock(ScriptedMetric.class); |
| 821 | + when(agg.aggregation()).thenReturn(returnValue); |
| 822 | + return agg; |
| 823 | + } |
| 824 | + |
| 825 | + @SuppressWarnings("unchecked") |
| 826 | + public void testScriptedMetricAggExtractor() { |
| 827 | + Aggregation agg = createScriptedMetric(null); |
| 828 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "object"), is(nullValue())); |
| 829 | + |
| 830 | + agg = createScriptedMetric(Collections.singletonList("values")); |
| 831 | + Object val = AggregationResultUtils.getExtractor(agg).value(agg, "object"); |
| 832 | + assertThat((List<String>)val, hasItem("values")); |
| 833 | + |
| 834 | + agg = createScriptedMetric(Collections.singletonMap("key", 100)); |
| 835 | + val = AggregationResultUtils.getExtractor(agg).value(agg, "object"); |
| 836 | + assertThat(((Map<String, Object>)val).get("key"), equalTo(100)); |
| 837 | + } |
| 838 | + |
| 839 | + private GeoCentroid createGeoCentroid(GeoPoint point, long count) { |
| 840 | + GeoCentroid agg = mock(GeoCentroid.class); |
| 841 | + when(agg.centroid()).thenReturn(point); |
| 842 | + when(agg.count()).thenReturn(count); |
| 843 | + return agg; |
| 844 | + } |
| 845 | + |
| 846 | + public void testGeoCentroidAggExtractor() { |
| 847 | + Aggregation agg = createGeoCentroid(null, 0); |
| 848 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "geo_point"), is(nullValue())); |
| 849 | + |
| 850 | + agg = createGeoCentroid(new GeoPoint(100.0, 101.0), 0); |
| 851 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "geo_point"), is(nullValue())); |
| 852 | + |
| 853 | + agg = createGeoCentroid(new GeoPoint(100.0, 101.0), randomIntBetween(1, 100)); |
| 854 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "geo_point"), equalTo("100.0, 101.0")); |
| 855 | + } |
| 856 | + |
| 857 | + private GeoBounds createGeoBounds(GeoPoint tl, GeoPoint br) { |
| 858 | + GeoBounds agg = mock(GeoBounds.class); |
| 859 | + when(agg.bottomRight()).thenReturn(br); |
| 860 | + when(agg.topLeft()).thenReturn(tl); |
| 861 | + return agg; |
| 862 | + } |
| 863 | + |
| 864 | + @SuppressWarnings("unchecked") |
| 865 | + public void testGeoBoundsAggExtractor() { |
| 866 | + final int numberOfRuns = 25; |
| 867 | + Aggregation agg = createGeoBounds(null, new GeoPoint(100.0, 101.0)); |
| 868 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "geo_shape"), is(nullValue())); |
| 869 | + |
| 870 | + agg = createGeoBounds(new GeoPoint(100.0, 101.0), null); |
| 871 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "geo_shape"), is(nullValue())); |
| 872 | + |
| 873 | + String type = "point"; |
| 874 | + for (int i = 0; i < numberOfRuns; i++) { |
| 875 | + Map<String, Object> expectedObject = new HashMap<>(); |
| 876 | + expectedObject.put("type", type); |
| 877 | + double lat = randomDoubleBetween(-90.0, 90.0, false); |
| 878 | + double lon = randomDoubleBetween(-180.0, 180.0, false); |
| 879 | + expectedObject.put("coordinates", Arrays.asList(lon, lat)); |
| 880 | + agg = createGeoBounds(new GeoPoint(lat, lon), new GeoPoint(lat, lon)); |
| 881 | + assertThat(AggregationResultUtils.getExtractor(agg).value(agg, "geo_shape"), equalTo(expectedObject)); |
| 882 | + } |
| 883 | + |
| 884 | + type = "linestring"; |
| 885 | + for (int i = 0; i < numberOfRuns; i++) { |
| 886 | + double lat = randomDoubleBetween(-90.0, 90.0, false); |
| 887 | + double lon = randomDoubleBetween(-180.0, 180.0, false); |
| 888 | + double lat2 = lat; |
| 889 | + double lon2 = lon; |
| 890 | + if (randomBoolean()) { |
| 891 | + lat2 = randomDoubleBetween(-90.0, 90.0, false); |
| 892 | + } else { |
| 893 | + lon2 = randomDoubleBetween(-180.0, 180.0, false); |
| 894 | + } |
| 895 | + agg = createGeoBounds(new GeoPoint(lat, lon), new GeoPoint(lat2, lon2)); |
| 896 | + Object val = AggregationResultUtils.getExtractor(agg).value(agg, "geo_shape"); |
| 897 | + Map<String, Object> geoJson = (Map<String, Object>)val; |
| 898 | + assertThat(geoJson.get("type"), equalTo(type)); |
| 899 | + List<Double[]> coordinates = (List<Double[]>)geoJson.get("coordinates"); |
| 900 | + for(Double[] coor : coordinates) { |
| 901 | + assertThat(coor.length, equalTo(2)); |
| 902 | + } |
| 903 | + assertThat(coordinates.get(0)[0], equalTo(lon)); |
| 904 | + assertThat(coordinates.get(0)[1], equalTo(lat)); |
| 905 | + assertThat(coordinates.get(1)[0], equalTo(lon2)); |
| 906 | + assertThat(coordinates.get(1)[1], equalTo(lat2)); |
| 907 | + } |
| 908 | + |
| 909 | + type = "polygon"; |
| 910 | + for (int i = 0; i < numberOfRuns; i++) { |
| 911 | + double lat = randomDoubleBetween(-90.0, 90.0, false); |
| 912 | + double lon = randomDoubleBetween(-180.0, 180.0, false); |
| 913 | + double lat2 = randomDoubleBetween(-90.0, 90.0, false); |
| 914 | + double lon2 = randomDoubleBetween(-180.0, 180.0, false); |
| 915 | + while (Double.compare(lat, lat2) == 0 || Double.compare(lon, lon2) == 0) { |
| 916 | + lat2 = randomDoubleBetween(-90.0, 90.0, false); |
| 917 | + lon2 = randomDoubleBetween(-180.0, 180.0, false); |
| 918 | + } |
| 919 | + agg = createGeoBounds(new GeoPoint(lat, lon), new GeoPoint(lat2, lon2)); |
| 920 | + Object val = AggregationResultUtils.getExtractor(agg).value(agg, "geo_shape"); |
| 921 | + Map<String, Object> geoJson = (Map<String, Object>)val; |
| 922 | + assertThat(geoJson.get("type"), equalTo(type)); |
| 923 | + List<List<Double[]>> coordinates = (List<List<Double[]>>)geoJson.get("coordinates"); |
| 924 | + assertThat(coordinates.size(), equalTo(1)); |
| 925 | + assertThat(coordinates.get(0).size(), equalTo(5)); |
| 926 | + List<List<Double>> expected = Arrays.asList( |
| 927 | + Arrays.asList(lon, lat), |
| 928 | + Arrays.asList(lon2, lat), |
| 929 | + Arrays.asList(lon2, lat2), |
| 930 | + Arrays.asList(lon, lat2), |
| 931 | + Arrays.asList(lon, lat)); |
| 932 | + for(int j = 0; j < 5; j++) { |
| 933 | + Double[] coordinate = coordinates.get(0).get(j); |
| 934 | + assertThat(coordinate.length, equalTo(2)); |
| 935 | + assertThat(coordinate[0], equalTo(expected.get(j).get(0))); |
| 936 | + assertThat(coordinate[1], equalTo(expected.get(j).get(1))); |
| 937 | + } |
| 938 | + } |
| 939 | + } |
784 | 940 |
|
785 | 941 | private void executeTest(GroupConfig groups,
|
786 | 942 | Collection<AggregationBuilder> aggregationBuilders,
|
|
0 commit comments