Skip to content

Commit 391c80c

Browse files
committed
test: add unit test to verify multiValueMode affects matrix_stats result
- Verifies that different multiValueMode settings (e.g., AVG vs MIN) produce different results - Prevents regression of incorrect aggregator reuse due to missing mode in equals/hashCode - Ensures matrix_stats behavior reflects requested aggregation mode Signed-off-by: Jinwoo Lee <jinlee1703@gmail.com>
1 parent 386ab58 commit 391c80c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

modules/aggs-matrix-stats/src/test/java/org/opensearch/search/aggregations/matrix/stats/MatrixStatsAggregatorTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.opensearch.index.mapper.MappedFieldType;
4545
import org.opensearch.index.mapper.NumberFieldMapper;
4646
import org.opensearch.plugins.SearchPlugin;
47+
import org.opensearch.search.MultiValueMode;
4748
import org.opensearch.search.aggregations.AggregatorTestCase;
4849
import org.opensearch.search.aggregations.matrix.MatrixAggregationModulePlugin;
4950

@@ -126,6 +127,39 @@ public void testTwoFields() throws Exception {
126127
}
127128
}
128129

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+
129163
@Override
130164
protected List<SearchPlugin> getSearchPlugins() {
131165
return Collections.singletonList(new MatrixAggregationModulePlugin());

0 commit comments

Comments
 (0)