Skip to content

Commit bb04fbc

Browse files
authored
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 cc81fe3 commit bb04fbc

File tree

6 files changed

+28
-5
lines changed

6 files changed

+28
-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;
@@ -108,6 +110,11 @@ protected boolean matches(String pattern, QueryShardContext context) {
108110
return context.indexMatches(pattern);
109111
}
110112

113+
@Override
114+
public Query existsQuery(QueryShardContext context) {
115+
return new MatchAllDocsQuery();
116+
}
117+
111118
@Override
112119
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
113120
return new ConstantIndexFieldData.Builder(mapperService -> fullyQualifiedIndexName);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public String typeName() {
107107
return CONTENT_TYPE;
108108
}
109109

110+
@Override
111+
public Query existsQuery(QueryShardContext context) {
112+
return new MatchAllDocsQuery();
113+
}
114+
110115
@Override
111116
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
112117
Function<MapperService, String> typeFunction = mapperService -> mapperService.documentMapper().type();

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
@@ -184,6 +184,11 @@ protected boolean matches(String pattern, QueryShardContext context) {
184184
return Regex.simpleMatch(pattern, value);
185185
}
186186

187+
@Override
188+
public Query existsQuery(QueryShardContext context) {
189+
return value != null ? new MatchAllDocsQuery() : new MatchNoDocsQuery();
190+
}
191+
187192
@Override
188193
public Query rangeQuery(
189194
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)