Skip to content

Commit 17b5565

Browse files
committed
Fix exists queries on nested flat_object fields throw exception
Signed-off-by: kkewwei <kewei.11@bytedance.com> Signed-off-by: kkewwei <kkewwei@163.com>
1 parent 8d5e1a3 commit 17b5565

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
9797
- Fix Shallow copy snapshot failures on closed index ([#16868](https://github.com/opensearch-project/OpenSearch/pull/16868))
9898
- Fix multi-value sort for unsigned long ([#16732](https://github.com/opensearch-project/OpenSearch/pull/16732))
9999
- The `phone-search` analyzer no longer emits the tel/sip prefix, international calling code, extension numbers and unformatted input as a token ([#16993](https://github.com/opensearch-project/OpenSearch/pull/16993))
100+
- Fix exists queries on nested flat_object fields throws exception ([#16803](https://github.com/opensearch-project/OpenSearch/pull/16803))
100101

101102
### Security
102103

rest-api-spec/src/main/resources/rest-api-spec/test/index/90_flat_object.yml

+20
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ teardown:
7373
- do:
7474
indices.delete:
7575
index: test
76+
77+
---
78+
"Exist query in root field":
79+
- skip:
80+
version: "- 2.99.99"
81+
reason: "the query would throw exception prior to 2.99.99"
82+
83+
- do:
84+
search:
85+
body: {
86+
_source: true,
87+
size: 10,
88+
query: {
89+
exists: {
90+
field: "catalog"
91+
}
92+
}
93+
}
94+
- length: { hits.hits: 2 }
95+
7696
---
7797
"Invalid docs":
7898
- skip:

server/src/main/java/org/opensearch/index/mapper/FlatObjectFieldMapper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.opensearch.common.unit.Fuzziness;
2828
import org.opensearch.common.xcontent.JsonToStringXContentParser;
2929
import org.opensearch.core.common.ParsingException;
30+
import org.opensearch.core.common.Strings;
3031
import org.opensearch.core.common.io.stream.StreamOutput;
3132
import org.opensearch.core.xcontent.DeprecationHandler;
3233
import org.opensearch.core.xcontent.NamedXContentRegistry;
@@ -87,7 +88,7 @@ public static class Defaults {
8788
@Override
8889
public MappedFieldType keyedFieldType(String key) {
8990
return new FlatObjectFieldType(
90-
this.name() + DOT_SYMBOL + key,
91+
Strings.isNullOrEmpty(key) ? this.name() : (this.name() + DOT_SYMBOL + key),
9192
this.name(),
9293
(KeywordFieldType) valueFieldMapper.fieldType(),
9394
(KeywordFieldType) valueAndPathFieldMapper.fieldType()

server/src/test/java/org/opensearch/index/mapper/FlatObjectFieldMapperTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.apache.lucene.search.Query;
1717
import org.apache.lucene.search.TermQuery;
1818
import org.apache.lucene.util.BytesRef;
19+
import org.opensearch.common.util.set.Sets;
1920
import org.opensearch.common.xcontent.XContentFactory;
2021
import org.opensearch.common.xcontent.json.JsonXContent;
2122
import org.opensearch.core.xcontent.ToXContent;
@@ -24,6 +25,7 @@
2425
import org.opensearch.search.DocValueFormat;
2526

2627
import java.io.IOException;
28+
import java.util.Set;
2729

2830
import static org.opensearch.common.xcontent.JsonToStringXContentParser.VALUE_AND_PATH_SUFFIX;
2931
import static org.opensearch.common.xcontent.JsonToStringXContentParser.VALUE_SUFFIX;
@@ -416,7 +418,14 @@ public void testFetchDocValues() throws IOException {
416418
Throwable throwable = assertThrows(IllegalArgumentException.class, () -> ft.docValueFormat(null, null));
417419
assertEquals("Field [field] of type [flat_object] does not support doc_value in root field", throwable.getMessage());
418420
}
421+
}
419422

423+
public void testPatternMatch() throws IOException {
424+
MapperService mapperService = createMapperService(fieldMapping(this::minimalMapping));
425+
QueryShardContext queryShardContext = createQueryShardContext(mapperService);
426+
Set<String> fields = queryShardContext.simpleMatchToIndexNames("field.*");
427+
assertEquals(2, fields.size());
428+
assertEquals(Sets.newHashSet("field._value", "field._valueAndPath"), fields);
420429
}
421430

422431
@Override

0 commit comments

Comments
 (0)