Skip to content

Add support for exists queries for rank_features #110876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/110876.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 110876
summary: Add support for exists queries for `rank_features`
area: Search
type: enhancement
issues:
- 98096
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.fielddata.FieldDataContext;
import org.elasticsearch.index.fielddata.IndexFieldData;
Expand Down Expand Up @@ -91,7 +92,10 @@ public String typeName() {

@Override
public Query existsQuery(SearchExecutionContext context) {
throw new IllegalArgumentException("[rank_features] fields do not support [exists] queries");
if (context.getIndexSettings().getIndexVersionCreated().before(IndexVersions.RANK_FEATURES_IN_FIELD_NAMES_SUPPORT)) {
throw new IllegalArgumentException("[rank_features] fields do not support [exists] queries");
}
return super.existsQuery(context);
}

@Override
Expand Down Expand Up @@ -196,6 +200,9 @@ public void parse(DocumentParserContext context) throws IOException {
);
}
}
if (context.indexSettings().getIndexVersionCreated().onOrAfter(IndexVersions.RANK_FEATURES_IN_FIELD_NAMES_SUPPORT)) {
context.addToFieldNames(fieldType().name());
}
} finally {
context.path().setWithinLeafObject(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.elasticsearch.index.mapper.DocumentParsingException;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MapperTestCase;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.plugins.Plugin;
Expand Down Expand Up @@ -43,12 +42,6 @@ protected Object getSampleObjectForDocument() {
return getSampleValueForDocument();
}

@Override
protected void assertExistsQuery(MapperService mapperService) {
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> super.assertExistsQuery(mapperService));
assertEquals("[rank_features] fields do not support [exists] queries", iae.getMessage());
}

@Override
protected Collection<? extends Plugin> getPlugins() {
return List.of(new MapperExtrasPlugin());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ setup:
1star: 1
2star: 10

- do:
index:
index: test
id: "3"
body:
text: "doc with no rank_features fields available"

- do:
indices.refresh: {}

Expand Down Expand Up @@ -181,3 +188,19 @@ setup:
negative_reviews:
1star: 1
2star: 10

---
"Exists queries":

- requires:
cluster_features: [ "gte_v8.16.0" ]
reason: "Support for exists queries was introduced in 8.16.0"
- do:
search:
rest_total_hits_as_int: true
index: test
body:
query:
exists:
field: tags
- match: { hits.total: 2 }
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public void testExists() throws Exception {
.startObject("vec")
.field("type", "sparse_vector")
.endObject()
.startObject("rank_feats")
.field("type", "rank_features")
.endObject()
.endObject()
.endObject()
.endObject();
Expand All @@ -91,6 +94,10 @@ public void testExists() throws Exception {
singletonMap("vec", emptyMap()),
// sparse_vector field non-empty
singletonMap("vec", singletonMap("1", 100)),
// rank features field empty
singletonMap("rank_feats", emptyMap()),
// rank_features field non-empty
singletonMap("rank_feats", singletonMap("foo", 100)),
// empty doc
emptyMap() };
List<IndexRequestBuilder> reqs = new ArrayList<>();
Expand All @@ -112,6 +119,7 @@ public void testExists() throws Exception {
expected.put("bar.bar.bar", 1);
expected.put("foobar", 0);
expected.put("vec", 2);
expected.put("rank_feats", 2);

final long numDocs = sources.length;
assertNoFailuresAndResponse(prepareSearch("idx").setSize(sources.length), allDocs -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private static IndexVersion def(int id, Version luceneVersion) {
public static final IndexVersion ADD_SECURITY_MIGRATION = def(8_510_00_0, Version.LUCENE_9_11_0);
public static final IndexVersion UPGRADE_TO_LUCENE_9_11_1 = def(8_511_00_0, Version.LUCENE_9_11_1);
public static final IndexVersion INDEX_SORTING_ON_NESTED = def(8_512_00_0, Version.LUCENE_9_11_1);
public static final IndexVersion RANK_FEATURES_IN_FIELD_NAMES_SUPPORT = def(8_513_00_0, Version.LUCENE_9_11_1);
/*
* STOP! READ THIS FIRST! No, really,
* ____ _____ ___ ____ _ ____ _____ _ ____ _____ _ _ ___ ____ _____ ___ ____ ____ _____ _
Expand Down