From 33986558cd912a54847293e28ff9520872c56df5 Mon Sep 17 00:00:00 2001 From: Harsha Vamsi Kalluri Date: Fri, 19 Jan 2024 13:52:16 -0800 Subject: [PATCH] Updating terms test Signed-off-by: Harsha Vamsi Kalluri --- .../index/mapper/BooleanFieldMapper.java | 13 ++++++--- .../index/mapper/BooleanFieldTypeTests.java | 27 ++++++++++--------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java index 7d4eb2db0a581..d55914bbbd9cc 100644 --- a/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java @@ -101,6 +101,11 @@ public static class Values { public static final BytesRef FALSE = new BytesRef("F"); } + public static class ExpandedValues { + public static final BytesRef TRUE = new BytesRef("true"); + public static final BytesRef FALSE = new BytesRef("false"); + } + private static BooleanFieldMapper toType(FieldMapper in) { return (BooleanFieldMapper) in; } @@ -291,13 +296,13 @@ public Query termQuery(Object value, QueryShardContext context) { public Query termsQuery(List values, QueryShardContext context) { failIfNotIndexedAndNoDocValues(); // if we do not get either True or False, we return no docs - if (!(values.contains(Values.TRUE)) || !(values.contains(Values.FALSE))) { + if (!(values.contains(ExpandedValues.TRUE)) && !(values.contains(ExpandedValues.FALSE))) { return new MatchNoDocsQuery("Values do not contain True or False"); } // if we have either True or False, we delegate to termQuery - if ((values.contains(Values.TRUE) && !(values.contains(Values.FALSE))) - || (values.contains(Values.FALSE) && !values.contains(Values.TRUE))) { - return termQuery(values.contains(Values.TRUE) ? Values.TRUE : Values.FALSE, context); + if ((values.contains(ExpandedValues.TRUE) && !(values.contains(ExpandedValues.FALSE))) + || (values.contains(ExpandedValues.FALSE) && !values.contains(ExpandedValues.TRUE))) { + return termQuery(values.contains(ExpandedValues.TRUE) ? ExpandedValues.TRUE : ExpandedValues.FALSE, context); } // if we have both True and False, we acknowledge that the field exists with a value return new FieldExistsQuery(name()); diff --git a/server/src/test/java/org/opensearch/index/mapper/BooleanFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/BooleanFieldTypeTests.java index 722997ce2dc74..d12c6c6779f14 100644 --- a/server/src/test/java/org/opensearch/index/mapper/BooleanFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/BooleanFieldTypeTests.java @@ -33,7 +33,9 @@ import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.index.Term; +import org.apache.lucene.search.FieldExistsQuery; import org.apache.lucene.search.IndexOrDocValuesQuery; +import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; @@ -84,22 +86,23 @@ public void testTermsQuery() { terms.add(new BytesRef("true")); terms.add(new BytesRef("false")); assertEquals( - new IndexOrDocValuesQuery( - new TermInSetQuery("field", terms.stream().map(booleanFieldType::indexedValueForSearch).toArray(BytesRef[]::new)), - new TermInSetQuery( - MultiTermQuery.DOC_VALUES_REWRITE, - "field", - terms.stream().map(booleanFieldType::indexedValueForSearch).toArray(BytesRef[]::new) - ) - ), + new FieldExistsQuery("field"), ft.termsQuery(terms, null) ); + + List newTerms = new ArrayList<>(); + newTerms.add(new BytesRef("true")); assertEquals( - new IndexOrDocValuesQuery( - new TermInSetQuery("field", terms), - new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, "field", terms) + new IndexOrDocValuesQuery( + new TermQuery(new Term("field", "T")), + SortedNumericDocValuesField.newSlowExactQuery("field", 1) ), - ft.termsQuery(terms, null) + ft.termsQuery(newTerms, null) + ); + + assertEquals( + new MatchNoDocsQuery("Values do not contain True or False"), + ft.termsQuery(new ArrayList<>(), null) ); MappedFieldType unsearchable = new BooleanFieldMapper.BooleanFieldType("field", false, false);