Skip to content

Commit 6582d5e

Browse files
committed
Refactored NumberFieldTypeTests to properly compare term queries
Signed-off-by: Sawan Srivastava <[email protected]>
1 parent ce1843d commit 6582d5e

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

server/src/test/java/org/opensearch/index/mapper/NumberFieldTypeTests.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,19 @@ private static MappedFieldType unsearchable() {
181181
public void testTermQuery() {
182182
MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG);
183183
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery("field", 42);
184-
Query query = new IndexOrDocValuesQuery(LongPoint.newExactQuery("field", 42), dvQuery);
185-
assertEquals(query, ft.termQuery("42", null));
184+
Query pointQuery = LongPoint.newExactQuery("field", 42);
185+
Query indexOrDocValuesQuery = new IndexOrDocValuesQuery(pointQuery, dvQuery);
186+
Query approximateQuery = new ApproximateScoreQuery(
187+
indexOrDocValuesQuery,
188+
new ApproximatePointRangeQuery(
189+
"field",
190+
LongPoint.pack(new long[] { 42 }).bytes,
191+
LongPoint.pack(new long[] { 42 }).bytes,
192+
1,
193+
ApproximatePointRangeQuery.LONG_FORMAT
194+
)
195+
);
196+
assertEquals(approximateQuery, ft.termQuery("42", null));
186197

187198
MappedFieldType unsearchable = unsearchable();
188199
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termQuery("42", null));
@@ -621,10 +632,26 @@ public void testNegativeZero() {
621632
NumberType.HALF_FLOAT.rangeQuery("field", null, +0f, true, false, false, true, MOCK_QSC)
622633
);
623634

624-
assertFalse(NumberType.DOUBLE.termQuery("field", -0d, true, true).equals(NumberType.DOUBLE.termQuery("field", +0d, true, true)));
625-
assertFalse(NumberType.FLOAT.termQuery("field", -0f, true, true).equals(NumberType.FLOAT.termQuery("field", +0f, true, true)));
635+
// For term queries, we need to extract the original query from the ApproximateScoreQuery
636+
Query negativeZeroDouble = NumberType.DOUBLE.termQuery("field", -0d, true, true);
637+
Query positiveZeroDouble = NumberType.DOUBLE.termQuery("field", +0d, true, true);
638+
assertFalse(
639+
((ApproximateScoreQuery) negativeZeroDouble).getOriginalQuery()
640+
.equals(((ApproximateScoreQuery) positiveZeroDouble).getOriginalQuery())
641+
);
642+
643+
Query negativeZeroFloat = NumberType.FLOAT.termQuery("field", -0f, true, true);
644+
Query positiveZeroFloat = NumberType.FLOAT.termQuery("field", +0f, true, true);
645+
assertFalse(
646+
((ApproximateScoreQuery) negativeZeroFloat).getOriginalQuery()
647+
.equals(((ApproximateScoreQuery) positiveZeroFloat).getOriginalQuery())
648+
);
649+
650+
Query negativeZeroHalfFloat = NumberType.HALF_FLOAT.termQuery("field", -0f, true, true);
651+
Query positiveZeroHalfFloat = NumberType.HALF_FLOAT.termQuery("field", +0f, true, true);
626652
assertFalse(
627-
NumberType.HALF_FLOAT.termQuery("field", -0f, true, true).equals(NumberType.HALF_FLOAT.termQuery("field", +0f, true, true))
653+
((ApproximateScoreQuery) negativeZeroHalfFloat).getOriginalQuery()
654+
.equals(((ApproximateScoreQuery) positiveZeroHalfFloat).getOriginalQuery())
628655
);
629656
}
630657

0 commit comments

Comments
 (0)