Skip to content

Commit

Permalink
Replace usages of deprecated specialized field exists queries (#88312)
Browse files Browse the repository at this point in the history
DocValueFieldExistsQuery, NormsFieldExistsQuery as well as KnnVectorFieldExistsQuery are deprecated in Lucene in favour of FieldExistsQuery which combines the three into a single query.

This commit updates Elasticsearch to no longer rely on such deprecated queries.

see https://issues.apache.org/jira/browse/LUCENE-10436
  • Loading branch information
javanna authored Jul 8, 2022
1 parent f65ffc6 commit d3b1a61
Show file tree
Hide file tree
Showing 41 changed files with 244 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.NormsFieldExistsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermQuery;
Expand Down Expand Up @@ -593,9 +593,9 @@ public void testNestedExistsQuery() throws IOException, ParseException {
Query q = parser.parse("foo:*");
assertEquals(
new ConstantScoreQuery(
new BooleanQuery.Builder().add(new NormsFieldExistsQuery("foo.bar"), BooleanClause.Occur.SHOULD)
.add(new NormsFieldExistsQuery("foo.bar._3gram"), BooleanClause.Occur.SHOULD)
.add(new NormsFieldExistsQuery("foo.bar._2gram"), BooleanClause.Occur.SHOULD)
new BooleanQuery.Builder().add(new FieldExistsQuery("foo.bar"), BooleanClause.Occur.SHOULD)
.add(new FieldExistsQuery("foo.bar._3gram"), BooleanClause.Occur.SHOULD)
.add(new FieldExistsQuery("foo.bar._2gram"), BooleanClause.Occur.SHOULD)
.add(new TermQuery(new Term("_field_names", "foo.bar._index_prefix")), BooleanClause.Occur.SHOULD)
.build()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
Expand Down Expand Up @@ -56,7 +56,7 @@ public static Query newNestedFilter() {
* Creates a new non-nested docs query
*/
public static Query newNonNestedFilter() {
return new DocValuesFieldExistsQuery(SeqNoFieldMapper.PRIMARY_TERM_NAME);
return new FieldExistsQuery(SeqNoFieldMapper.PRIMARY_TERM_NAME);
}

public static BooleanQuery filtered(@Nullable Query query, @Nullable Query filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.NormsFieldExistsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
Expand Down Expand Up @@ -318,10 +317,8 @@ public Query regexpQuery(
}

public Query existsQuery(SearchExecutionContext context) {
if (hasDocValues()) {
return new DocValuesFieldExistsQuery(name());
} else if (getTextSearchInfo().hasNorms()) {
return new NormsFieldExistsQuery(name());
if (hasDocValues() || getTextSearchInfo().hasNorms()) {
return new FieldExistsQuery(name());
} else {
return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import org.apache.lucene.document.Field;
import org.apache.lucene.document.KnnVectorField;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.KnnVectorFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.KnnVectorQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
Expand Down Expand Up @@ -286,12 +285,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S

@Override
public Query existsQuery(SearchExecutionContext context) {
if (indexed) {
return new KnnVectorFieldExistsQuery(name());
} else {
assert hasDocValues();
return new DocValuesFieldExistsQuery(name());
}
return new FieldExistsQuery(name());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.PointRangeQuery;
Expand Down Expand Up @@ -226,7 +226,7 @@ private static boolean checkMatchAllOrRangeQuery(Query query, String fieldName)
return true;
} else if (query instanceof PointRangeQuery pointQuery) {
return fieldName.equals(pointQuery.getField());
} else if (query instanceof DocValuesFieldExistsQuery existsQuery) {
} else if (query instanceof FieldExistsQuery existsQuery) {
return fieldName.equals(existsQuery.getField());
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MultiCollector;
import org.apache.lucene.search.Query;
Expand Down Expand Up @@ -401,8 +401,8 @@ static int shortcutTotalHitCount(IndexReader reader, Query query) throws IOExcep
count += context.reader().docFreq(term);
}
return count;
} else if (query.getClass() == DocValuesFieldExistsQuery.class && reader.hasDeletions() == false) {
final String field = ((DocValuesFieldExistsQuery) query).getField();
} else if (query.getClass() == FieldExistsQuery.class && reader.hasDeletions() == false) {
final String field = ((FieldExistsQuery) query).getField();
int count = 0;
for (LeafReaderContext context : reader.leaves()) {
FieldInfos fieldInfos = context.reader().getFieldInfos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
Expand All @@ -23,7 +23,7 @@ public void testNonNestedQuery() {
// This is a custom query that extends AutomatonQuery and want to make sure the equals method works
assertEquals(Queries.newNonNestedFilter(), Queries.newNonNestedFilter());
assertEquals(Queries.newNonNestedFilter().hashCode(), Queries.newNonNestedFilter().hashCode());
assertEquals(Queries.newNonNestedFilter(), new DocValuesFieldExistsQuery(SeqNoFieldMapper.PRIMARY_TERM_NAME));
assertEquals(Queries.newNonNestedFilter(), new FieldExistsQuery(SeqNoFieldMapper.PRIMARY_TERM_NAME));
}

public void testIsNegativeQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.index.Term;
import org.apache.lucene.sandbox.search.DocValuesTermsQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.NormsFieldExistsQuery;
import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
Expand Down Expand Up @@ -123,17 +122,17 @@ public void testTermsQuery() {
public void testExistsQuery() {
{
KeywordFieldType ft = new KeywordFieldType("field");
assertEquals(new DocValuesFieldExistsQuery("field"), ft.existsQuery(MOCK_CONTEXT));
assertEquals(new FieldExistsQuery("field"), ft.existsQuery(MOCK_CONTEXT));
}
{
KeywordFieldType ft = new KeywordFieldType("field", false, true, Map.of());
assertEquals(new DocValuesFieldExistsQuery("field"), ft.existsQuery(MOCK_CONTEXT));
assertEquals(new FieldExistsQuery("field"), ft.existsQuery(MOCK_CONTEXT));
}
{
FieldType fieldType = new FieldType();
fieldType.setOmitNorms(false);
KeywordFieldType ft = new KeywordFieldType("field", fieldType);
assertEquals(new NormsFieldExistsQuery("field"), ft.existsQuery(MOCK_CONTEXT));
assertEquals(new FieldExistsQuery("field"), ft.existsQuery(MOCK_CONTEXT));
}
{
KeywordFieldType ft = new KeywordFieldType("field", true, false, Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.NormsFieldExistsQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
Expand Down Expand Up @@ -842,7 +842,7 @@ public void testObjectExistsQuery() throws IOException, ParseException {
SearchExecutionContext context = createSearchExecutionContext(ms);
QueryStringQueryParser parser = new QueryStringQueryParser(context, "f");
Query q = parser.parse("foo:*");
assertEquals(new ConstantScoreQuery(new NormsFieldExistsQuery("foo.bar")), q);
assertEquals(new ConstantScoreQuery(new FieldExistsQuery("foo.bar")), q);
}

private static void assertAnalyzesTo(Analyzer analyzer, String field, String input, String[] output) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package org.elasticsearch.index.mapper.flattened;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RegexpQuery;
Expand Down Expand Up @@ -62,7 +62,7 @@ public void testExistsQuery() {
assertEquals(new TermQuery(new Term(FieldNamesFieldMapper.NAME, new BytesRef("field"))), ft.existsQuery(null));

RootFlattenedFieldType withDv = new RootFlattenedFieldType("field", true, true, Collections.emptyMap(), false, false);
assertEquals(new DocValuesFieldExistsQuery("field"), withDv.existsQuery(null));
assertEquals(new FieldExistsQuery("field"), withDv.existsQuery(null));
}

public void testFuzzyQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import org.apache.lucene.document.BinaryDocValuesField;
import org.apache.lucene.document.KnnVectorField;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.KnnVectorFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version;
Expand Down Expand Up @@ -122,16 +121,9 @@ protected void assertSearchable(MappedFieldType fieldType) {
}

protected void assertExistsQuery(MappedFieldType fieldType, Query query, LuceneDocument fields) {
if (indexed) {
assertThat(query, instanceOf(KnnVectorFieldExistsQuery.class));
KnnVectorFieldExistsQuery existsQuery = (KnnVectorFieldExistsQuery) query;
assertEquals("field", existsQuery.getField());
} else {
assertThat(query, instanceOf(DocValuesFieldExistsQuery.class));
DocValuesFieldExistsQuery existsQuery = (DocValuesFieldExistsQuery) query;
assertEquals("field", existsQuery.getField());
assertDocValuesField(fields, "field");
}
assertThat(query, instanceOf(FieldExistsQuery.class));
FieldExistsQuery existsQuery = (FieldExistsQuery) query;
assertEquals("field", existsQuery.getField());
assertNoFieldNamesField(fields);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.NormsFieldExistsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.common.regex.Regex;
Expand Down Expand Up @@ -69,14 +68,10 @@ protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query,
for (BooleanClause booleanClause : booleanQuery) {
assertThat(booleanClause.getOccur(), equalTo(BooleanClause.Occur.SHOULD));
}
} else if (context.getFieldType(field).hasDocValues()) {
assertThat(constantScoreQuery.getQuery(), instanceOf(DocValuesFieldExistsQuery.class));
DocValuesFieldExistsQuery dvExistsQuery = (DocValuesFieldExistsQuery) constantScoreQuery.getQuery();
assertEquals(field, dvExistsQuery.getField());
} else if (context.getFieldType(field).getTextSearchInfo().hasNorms()) {
assertThat(constantScoreQuery.getQuery(), instanceOf(NormsFieldExistsQuery.class));
NormsFieldExistsQuery normsExistsQuery = (NormsFieldExistsQuery) constantScoreQuery.getQuery();
assertEquals(field, normsExistsQuery.getField());
} else if (context.getFieldType(field).hasDocValues() || context.getFieldType(field).getTextSearchInfo().hasNorms()) {
assertThat(constantScoreQuery.getQuery(), instanceOf(FieldExistsQuery.class));
FieldExistsQuery existsQuery = (FieldExistsQuery) constantScoreQuery.getQuery();
assertEquals(field, existsQuery.getField());
} else {
assertThat(constantScoreQuery.getQuery(), instanceOf(TermQuery.class));
TermQuery termQuery = (TermQuery) constantScoreQuery.getQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.FuzzyQuery;
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.NormsFieldExistsQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
Expand Down Expand Up @@ -1041,7 +1041,7 @@ public void testExistsFieldQuery() throws Exception {
QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(TEXT_FIELD_NAME + ":*");
Query query = queryBuilder.toQuery(context);
if (context.getFieldType(TEXT_FIELD_NAME).getTextSearchInfo().hasNorms()) {
assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(TEXT_FIELD_NAME))));
assertThat(query, equalTo(new ConstantScoreQuery(new FieldExistsQuery(TEXT_FIELD_NAME))));
} else {
assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", TEXT_FIELD_NAME)))));
}
Expand All @@ -1051,7 +1051,7 @@ public void testExistsFieldQuery() throws Exception {
queryBuilder = new QueryStringQueryBuilder("_exists_:" + value);
query = queryBuilder.toQuery(context);
if (context.getFieldType(TEXT_FIELD_NAME).getTextSearchInfo().hasNorms()) {
assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(TEXT_FIELD_NAME))));
assertThat(query, equalTo(new ConstantScoreQuery(new FieldExistsQuery(TEXT_FIELD_NAME))));
} else {
assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", TEXT_FIELD_NAME)))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DocValuesFieldExistsQuery;
import org.apache.lucene.search.FieldExistsQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.NormsFieldExistsQuery;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
Expand Down Expand Up @@ -151,10 +150,8 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query,
if (queryBuilder.from() == null && queryBuilder.to() == null) {
final Query expectedQuery;
final MappedFieldType resolvedFieldType = context.getFieldType(queryBuilder.fieldName());
if (resolvedFieldType.hasDocValues()) {
expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(expectedFieldName));
} else if (context.getFieldType(resolvedFieldType.name()).getTextSearchInfo().hasNorms()) {
expectedQuery = new ConstantScoreQuery(new NormsFieldExistsQuery(expectedFieldName));
if (resolvedFieldType.hasDocValues() || context.getFieldType(resolvedFieldType.name()).getTextSearchInfo().hasNorms()) {
expectedQuery = new ConstantScoreQuery(new FieldExistsQuery(expectedFieldName));
} else {
expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, expectedFieldName)));
}
Expand Down Expand Up @@ -452,7 +449,7 @@ protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteC
Query luceneQuery = rewrittenRange.toQuery(searchExecutionContext);
final Query expectedQuery;
if (searchExecutionContext.getFieldType(query.fieldName()).hasDocValues()) {
expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(query.fieldName()));
expectedQuery = new ConstantScoreQuery(new FieldExistsQuery(query.fieldName()));
} else {
expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, query.fieldName())));
}
Expand Down
Loading

0 comments on commit d3b1a61

Please sign in to comment.