Skip to content

Commit

Permalink
Updating terms test
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
  • Loading branch information
harshavamsi committed Jan 19, 2024
1 parent a263767 commit 3398655
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BytesRef> 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);
Expand Down

0 comments on commit 3398655

Please sign in to comment.