Skip to content

Commit

Permalink
Fixing boolean field tests
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
  • Loading branch information
harshavamsi committed Apr 29, 2024
1 parent 735cad6 commit 93c15fe
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
terms:
boolean: [true, false]

- match: { hits.total: 3 }

- do:
search:
rest_total_hits_as_int: true
Expand Down Expand Up @@ -665,13 +676,13 @@
- match: {hits.total: 1}

- do:
search:
rest_total_hits_as_int: true
index: test-index
body:
query:
term:
boolean: true
search:
rest_total_hits_as_int: true
index: test-index
body:
query:
term:
boolean: true

- match: { hits.total: 2 }

Expand Down Expand Up @@ -775,6 +786,17 @@

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-index
body:
query:
terms:
boolean: [true, false]

- match: { hits.total: 3 }

- do:
search:
rest_total_hits_as_int: true
Expand Down Expand Up @@ -1235,6 +1257,17 @@

- match: { hits.total: 2 }

- do:
search:
rest_total_hits_as_int: true
index: test-doc-values
body:
query:
terms:
boolean: [true, false]

- match: { hits.total: 3 }

- do:
search:
rest_total_hits_as_int: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.util.BytesRef;
Expand Down Expand Up @@ -284,27 +283,18 @@ public Query termQuery(Object value, QueryShardContext context) {
@Override
public Query termsQuery(List<?> values, QueryShardContext context) {
failIfNotIndexedAndNoDocValues();
boolean seenTrue = false;
boolean seenFalse = false;
for (Object value : values) {
if (Values.TRUE.equals(indexedValueForSearch(value))) {
seenTrue = true;
} else if (Values.FALSE.equals(indexedValueForSearch(value))) {
seenFalse = true;
} else {
return new MatchNoDocsQuery("Values did not contain True or False");
}
}
if (seenTrue) {
if (seenFalse) {
return new MatchAllDocsQuery();
if (!isSearchable()) {
long[] v = new long[values.size()];
for (int i = 0; i < v.length; i++) {
v[i] = Values.TRUE.bytesEquals(indexedValueForSearch(values.get(i))) ? 1 : 0;
}
return termQuery("true", context);
return SortedNumericDocValuesField.newSlowSetQuery(name(), v);
}
if (seenFalse) {
return termQuery("false", context);
BytesRef[] bytesRefs = new BytesRef[values.size()];
for (int i = 0; i < bytesRefs.length; i++) {
bytesRefs[i] = indexedValueForSearch(values.get(i));
}
return new MatchNoDocsQuery("Values did not contain True or False");
return new TermInSetQuery(name(), List.of(bytesRefs));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@

package org.opensearch.index.mapper;

import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;
import org.opensearch.common.xcontent.XContentFactory;
Expand Down Expand Up @@ -208,13 +206,7 @@ public void testBoosts() throws Exception {
}));

MappedFieldType ft = mapperService.fieldType("field");
assertEquals(
new IndexOrDocValuesQuery(
new BoostQuery(new TermQuery(new Term("field", "T")), 2.0f),
SortedNumericDocValuesField.newSlowExactQuery("field", 1)
),
ft.termQuery("true", null)
);
assertEquals(new BoostQuery(new TermQuery(new Term("field", "T")), 2.0f), ft.termQuery("true", null));
assertParseMaximalWarnings();
}

Expand All @@ -235,16 +227,6 @@ public void testIndexedValueForSearch() throws Exception {
BooleanFieldMapper.Values.FALSE
);

assertEquals(
new BooleanFieldMapper.BooleanFieldType("bool").indexedValueForSearch(new BytesRef("T")),
BooleanFieldMapper.Values.TRUE
);

assertEquals(
new BooleanFieldMapper.BooleanFieldType("bool").indexedValueForSearch(new BytesRef("F")),
BooleanFieldMapper.Values.FALSE
);

IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> new BooleanFieldMapper.BooleanFieldType("bool").indexedValueForSearch(new BytesRef("random"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

public class BooleanFieldTypeTests extends FieldTypeTestCase {

Expand Down Expand Up @@ -82,17 +82,21 @@ public void testTermQuery() {

public void testTermsQuery() {
MappedFieldType ft = new BooleanFieldMapper.BooleanFieldType("field");
BooleanFieldMapper.BooleanFieldType booleanFieldType = new BooleanFieldMapper.BooleanFieldType("field");
List<BytesRef> terms = new ArrayList<>();
terms.add(new BytesRef("true"));
terms.add(new BytesRef("false"));
assertEquals(new MatchAllDocsQuery(), ft.termsQuery(terms, null));
assertEquals(new TermInSetQuery("field", List.of(new BytesRef("T"), newBytesRef("F"))), ft.termsQuery(terms, null));

List<BytesRef> newTerms = new ArrayList<>();
newTerms.add(new BytesRef("true"));
assertEquals(new TermQuery(new Term("field", "T")), ft.termsQuery(newTerms, null));
assertEquals(new TermInSetQuery("field", List.of(new BytesRef("T"))), ft.termsQuery(newTerms, null));

assertEquals(new MatchNoDocsQuery("Values do not contain True or False"), ft.termsQuery(new ArrayList<>(), null));
MappedFieldType doc_only_ft = new BooleanFieldMapper.BooleanFieldType("field", false, true);

assertEquals(
SortedNumericDocValuesField.newSlowSetQuery("field", Stream.of(1).mapToLong(l -> l).toArray()),
doc_only_ft.termsQuery(newTerms, null)
);

MappedFieldType unsearchable = new BooleanFieldMapper.BooleanFieldType("field", false, false);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termsQuery(terms, null));
Expand Down

0 comments on commit 93c15fe

Please sign in to comment.