|
44 | 44 | import org.opensearch.index.mapper.MappedFieldType; |
45 | 45 | import org.opensearch.index.mapper.NumberFieldMapper; |
46 | 46 | import org.opensearch.plugins.SearchPlugin; |
| 47 | +import org.opensearch.search.MultiValueMode; |
47 | 48 | import org.opensearch.search.aggregations.AggregatorTestCase; |
48 | 49 | import org.opensearch.search.aggregations.matrix.MatrixAggregationModulePlugin; |
49 | 50 |
|
@@ -126,6 +127,39 @@ public void testTwoFields() throws Exception { |
126 | 127 | } |
127 | 128 | } |
128 | 129 |
|
| 130 | + public void testMultiValueModeAffectsResult() throws Exception { |
| 131 | + String field = "grades"; |
| 132 | + MappedFieldType ft = new NumberFieldMapper.NumberFieldType(field, NumberFieldMapper.NumberType.DOUBLE); |
| 133 | + |
| 134 | + try (Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { |
| 135 | + Document doc = new Document(); |
| 136 | + doc.add(new SortedNumericDocValuesField(field, NumericUtils.doubleToSortableLong(1.0))); |
| 137 | + doc.add(new SortedNumericDocValuesField(field, NumericUtils.doubleToSortableLong(3.0))); |
| 138 | + doc.add(new SortedNumericDocValuesField(field, NumericUtils.doubleToSortableLong(5.0))); |
| 139 | + indexWriter.addDocument(doc); |
| 140 | + |
| 141 | + try (IndexReader reader = indexWriter.getReader()) { |
| 142 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 143 | + |
| 144 | + MatrixStatsAggregationBuilder avgAgg = new MatrixStatsAggregationBuilder("avg_agg") |
| 145 | + .fields(Collections.singletonList(field)) |
| 146 | + .multiValueMode(MultiValueMode.AVG); |
| 147 | + |
| 148 | + MatrixStatsAggregationBuilder minAgg = new MatrixStatsAggregationBuilder("min_agg") |
| 149 | + .fields(Collections.singletonList(field)) |
| 150 | + .multiValueMode(MultiValueMode.MIN); |
| 151 | + |
| 152 | + InternalMatrixStats avgStats = searchAndReduce(searcher, new MatchAllDocsQuery(), avgAgg, ft); |
| 153 | + InternalMatrixStats minStats = searchAndReduce(searcher, new MatchAllDocsQuery(), minAgg, ft); |
| 154 | + |
| 155 | + double avg = avgStats.getMean(field); |
| 156 | + double min = minStats.getMean(field); |
| 157 | + |
| 158 | + assertNotEquals("AVG and MIN mode should yield different means", avg, min, 0.0001); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
129 | 163 | @Override |
130 | 164 | protected List<SearchPlugin> getSearchPlugins() { |
131 | 165 | return Collections.singletonList(new MatrixAggregationModulePlugin()); |
|
0 commit comments