Skip to content

Commit e59b099

Browse files
committed
For constant_keyword, make sure exists query handles missing values. (#55757)
It's possible for a constant_keyword to have a 'null' value before any documents are seen that contain a value for the field. In this case, no documents have a value for the field, and 'exists' queries should return no documents.
1 parent 96c1484 commit e59b099

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

docs/reference/mapping/types/constant-keyword.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ While this behavior can be convenient, note that it means that a single
6565
poisonous document can cause all other documents to be rejected if it had a
6666
wrong value.
6767

68+
Before a value has been provided (either through the mappings or from a
69+
document), queries on the field will not match any documents. This includes
70+
<<query-dsl-exists-query,`exists`>> queries.
71+
6872
The `value` of the field cannot be changed after it has been set.
6973

7074
[[constant-keyword-params]]

server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ public final boolean isAggregatable() {
5858
return true;
5959
}
6060

61-
@Override
62-
public final Query existsQuery(QueryShardContext context) {
63-
return new MatchAllDocsQuery();
64-
}
65-
6661
/**
6762
* Return whether the constant value of this field matches the provided {@code pattern}
6863
* as documented in {@link Regex#simpleMatch}.

server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.apache.lucene.index.IndexOptions;
2323
import org.apache.lucene.index.IndexableField;
24+
import org.apache.lucene.search.MatchAllDocsQuery;
25+
import org.apache.lucene.search.Query;
2426
import org.elasticsearch.common.lucene.Lucene;
2527
import org.elasticsearch.common.settings.Settings;
2628
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -106,6 +108,11 @@ protected boolean matches(String pattern, QueryShardContext context) {
106108
return context.indexMatches(pattern);
107109
}
108110

111+
@Override
112+
public Query existsQuery(QueryShardContext context) {
113+
return new MatchAllDocsQuery();
114+
}
115+
109116
@Override
110117
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
111118
return new ConstantIndexFieldData.Builder(mapperService -> fullyQualifiedIndexName);

x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ protected boolean matches(String pattern, QueryShardContext context) {
182182
return Regex.simpleMatch(pattern, value);
183183
}
184184

185+
@Override
186+
public Query existsQuery(QueryShardContext context) {
187+
return value != null ? new MatchAllDocsQuery() : new MatchNoDocsQuery();
188+
}
189+
185190
@Override
186191
public Query rangeQuery(
187192
Object lowerTerm, Object upperTerm,

x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldTypeTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public void testPrefixQuery() {
9696
assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("ba", null, null));
9797
}
9898

99+
public void testExistsQuery() {
100+
ConstantKeywordFieldType ft = new ConstantKeywordFieldType();
101+
assertEquals(new MatchNoDocsQuery(), ft.existsQuery(null));
102+
ft.setValue("foo");
103+
assertEquals(new MatchAllDocsQuery(), ft.existsQuery(null));
104+
}
105+
99106
public void testRangeQuery() {
100107
ConstantKeywordFieldType ft = new ConstantKeywordFieldType();
101108
assertEquals(new MatchNoDocsQuery(), ft.rangeQuery(null, null, randomBoolean(), randomBoolean(), null, null, null, null));

0 commit comments

Comments
 (0)