Skip to content

Commit

Permalink
Converting booleans to use IndexOrDocValues
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
  • Loading branch information
harshavamsi committed Dec 19, 2023
1 parent 37dae82 commit e950ee1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
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;
import org.opensearch.common.Booleans;
Expand Down Expand Up @@ -175,6 +181,10 @@ public BooleanFieldType(String name, boolean searchable) {
this(name, searchable, false, true, false, Collections.emptyMap());
}

public BooleanFieldType(String name, boolean searchable, boolean hasDocValues) {
this(name, searchable, false, hasDocValues, false, Collections.emptyMap());
}

@Override
public String typeName() {
return CONTENT_TYPE;
Expand Down Expand Up @@ -257,15 +267,77 @@ public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
return DocValueFormat.BOOLEAN;
}

@Override
public Query termQuery(Object value, QueryShardContext context){
failIfNotIndexedAndNoDocValues();
Query query = new TermQuery(new Term(name(), indexedValueForSearch(value)));
if (boost() != 1f) {
query = new BoostQuery(query, boost());
}
if(isSearchable() && hasDocValues()){
return new IndexOrDocValuesQuery(query,
SortedNumericDocValuesField.newSlowExactQuery(name(),
Values.TRUE.bytesEquals(indexedValueForSearch(value))?1:0));
}
if(hasDocValues()){
return SortedNumericDocValuesField.newSlowExactQuery(name(),
Values.TRUE.bytesEquals(indexedValueForSearch(value))?1:0);
}
return query;
}

@Override
public Query termsQuery(List<?> values, QueryShardContext context){
failIfNotIndexedAndNoDocValues();
if(isSearchable() && hasDocValues()){
Query query = new TermInSetQuery(name(), values.stream().map(this::indexedValueForSearch).toArray(BytesRef[]::new));
Query dvQuery = new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, name(), values.stream().map(this::indexedValueForSearch).toArray(BytesRef[]::new));
return new IndexOrDocValuesQuery(query,
dvQuery);
}
if(hasDocValues()){
return new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, name(), values.stream().map(this::indexedValueForSearch).toArray(BytesRef[]::new));
}
return new TermInSetQuery(name(), values.stream().map(this::indexedValueForSearch).toArray(BytesRef[]::new));
}

@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
failIfNotIndexed();
failIfNotIndexedAndNoDocValues();
if(isSearchable() && hasDocValues()){
Query query = new TermRangeQuery(
name(),
lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
upperTerm == null ? null : indexedValueForSearch(upperTerm),
includeLower,
includeUpper
);
Query dvQuery = new TermRangeQuery(
name(),
lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
upperTerm == null ? null : indexedValueForSearch(upperTerm),
includeLower,
includeUpper,
MultiTermQuery.DOC_VALUES_REWRITE
);
return new IndexOrDocValuesQuery(query, dvQuery);
}
if(hasDocValues()){
return new TermRangeQuery(
name(),
lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
upperTerm == null ? null : indexedValueForSearch(upperTerm),
includeLower,
includeUpper,
MultiTermQuery.DOC_VALUES_REWRITE
);
}
return new TermRangeQuery(
name(),
lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
upperTerm == null ? null : indexedValueForSearch(upperTerm),
includeLower,
includeUpper
name(),
lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
upperTerm == null ? null : indexedValueForSearch(upperTerm),
includeLower,
includeUpper
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@

package org.opensearch.index.mapper;

import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MultiTermQuery;
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;

public class BooleanFieldTypeTests extends FieldTypeTestCase {

Expand All @@ -56,12 +63,28 @@ public void testValueForSearch() {

public void testTermQuery() {
MappedFieldType ft = new BooleanFieldMapper.BooleanFieldType("field");
assertEquals(new TermQuery(new Term("field", "T")), ft.termQuery("true", null));
assertEquals(new TermQuery(new Term("field", "F")), ft.termQuery("false", null));
assertEquals(new IndexOrDocValuesQuery(new TermQuery(new Term("field", "T")), SortedNumericDocValuesField.newSlowExactQuery("field", 1)), ft.termQuery("true", null));
assertEquals(new IndexOrDocValuesQuery(new TermQuery(new Term("field", "F")), SortedNumericDocValuesField.newSlowExactQuery("field", 0)), ft.termQuery("false", null));

MappedFieldType unsearchable = new BooleanFieldMapper.BooleanFieldType("field", false);
MappedFieldType unsearchable = new BooleanFieldMapper.BooleanFieldType("field", false, false);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termQuery("true", null));
assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
assertEquals("Cannot search on field [field] since it is both not indexed, and does not have doc_values enabled.", e.getMessage());
}

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 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))
), ft.termsQuery(terms, null));

MappedFieldType unsearchable = new BooleanFieldMapper.BooleanFieldType("field", false, false);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termsQuery(terms, null));
assertEquals("Cannot search on field [field] since it is both not indexed, and does not have doc_values enabled.", e.getMessage());
}

public void testFetchSourceValue() throws IOException {
Expand Down

0 comments on commit e950ee1

Please sign in to comment.