Skip to content

Commit 5763a44

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 98dbc4a commit 5763a44

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6464
- [Tiered Caching] Fix bug in cache stats API ([#16560](https://github.com/opensearch-project/OpenSearch/pull/16560))
6565
- Bound the size of cache in deprecation logger ([16702](https://github.com/opensearch-project/OpenSearch/issues/16702))
6666
- Ensure consistency of system flag on IndexMetadata after diff is applied ([#16644](https://github.com/opensearch-project/OpenSearch/pull/16644))
67+
- Fix exists queries on nested flat_object fields throws exception ([#16803](https://github.com/opensearch-project/OpenSearch/pull/16803))
6768

6869
### Security
6970

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
@@ -28,6 +28,7 @@
2828
import org.opensearch.common.unit.Fuzziness;
2929
import org.opensearch.common.xcontent.JsonToStringXContentParser;
3030
import org.opensearch.core.common.ParsingException;
31+
import org.opensearch.core.common.Strings;
3132
import org.opensearch.core.xcontent.DeprecationHandler;
3233
import org.opensearch.core.xcontent.NamedXContentRegistry;
3334
import org.opensearch.core.xcontent.XContentParser;
@@ -84,7 +85,7 @@ public static class Defaults {
8485
@Override
8586
public MappedFieldType keyedFieldType(String key) {
8687
return new FlatObjectFieldType(
87-
this.name() + DOT_SYMBOL + key,
88+
Strings.isNullOrEmpty(key) ? this.name() : (this.name() + DOT_SYMBOL + key),
8889
this.name(),
8990
(KeywordFieldType) valueFieldMapper.fieldType(),
9091
(KeywordFieldType) valueAndPathFieldMapper.fieldType()

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

+11
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
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;
2223
import org.opensearch.core.xcontent.XContentBuilder;
2324
import org.opensearch.index.query.QueryShardContext;
2425

2526
import java.io.IOException;
27+
import java.util.Set;
2628

2729
import static org.opensearch.common.xcontent.JsonToStringXContentParser.VALUE_AND_PATH_SUFFIX;
2830
import static org.opensearch.common.xcontent.JsonToStringXContentParser.VALUE_SUFFIX;
@@ -397,6 +399,15 @@ public void testDeduplicationValue() throws IOException {
397399
assertEquals(new BytesRef("field.labels=3"), fieldValueAndPaths[4].binaryValue());
398400
}
399401

402+
public void testPatternMatch() throws IOException {
403+
MapperService mapperService = createMapperService(fieldMapping(this::minimalMapping));
404+
QueryShardContext queryShardContext = createQueryShardContext(mapperService);
405+
Set<String> fields = queryShardContext.simpleMatchToIndexNames("field.*");
406+
assertEquals(2, fields.size());
407+
assertEquals(Sets.newHashSet("field._value", "field._valueAndPath"), fields);
408+
System.out.println(fields);
409+
}
410+
400411
@Override
401412
protected void registerParameters(ParameterChecker checker) throws IOException {
402413
// In the future we will want to make sure parameter updates are covered.

0 commit comments

Comments
 (0)