Skip to content

Commit c6dfc65

Browse files
authored
Fix exists queries on nested flat_object fields throw exception (opensearch-project#16803)
Signed-off-by: kkewwei <kewei.11@bytedance.com> Signed-off-by: kkewwei <kkewwei@163.com>
1 parent e15f712 commit c6dfc65

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
109109
- Fix multi-value sort for unsigned long ([#16732](https://github.com/opensearch-project/OpenSearch/pull/16732))
110110
- 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))
111111
- Fix GRPC AUX_TRANSPORT_PORT and SETTING_GRPC_PORT settings and remove lingering HTTP terminology ([#17037](https://github.com/opensearch-project/OpenSearch/pull/17037))
112+
- Fix exists queries on nested flat_object fields throws exception ([#16803](https://github.com/opensearch-project/OpenSearch/pull/16803))
112113

113114
### Security
114115

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

+45-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ setup:
1616
type : "flat_object"
1717
required_matches:
1818
type : "long"
19-
19+
infos:
20+
properties:
21+
info:
22+
type: "flat_object"
2023
- do:
2124
index:
2225
index: test
@@ -60,7 +63,12 @@ setup:
6063
"review": [["bad",30.41],["ok",80.0]],
6164
"publishDate": "2016-01-01"
6265
},
63-
"required_matches": 1
66+
"required_matches": 1,
67+
"infos": {
68+
"info":{
69+
"name": "name1"
70+
}
71+
}
6472
}
6573
# Do index refresh
6674
- do:
@@ -73,6 +81,40 @@ teardown:
7381
- do:
7482
indices.delete:
7583
index: test
84+
85+
---
86+
"Exist query in root field":
87+
- skip:
88+
version: "- 2.99.99"
89+
reason: "the query would throw exception prior to 2.99.99"
90+
91+
- do:
92+
search:
93+
body: {
94+
_source: true,
95+
size: 10,
96+
query: {
97+
exists: {
98+
field: "catalog"
99+
}
100+
}
101+
}
102+
- length: { hits.hits: 2 }
103+
104+
- do:
105+
search:
106+
body: {
107+
_source: true,
108+
size: 10,
109+
query: {
110+
exists: {
111+
field: "infos"
112+
}
113+
}
114+
}
115+
- length: { hits.hits: 1 }
116+
- match: { hits.hits.0._source.infos.info.name: "name1" }
117+
76118
---
77119
"Invalid docs":
78120
- skip:
@@ -135,7 +177,7 @@ teardown:
135177
- match: { test.mappings.properties.catalog.type: flat_object }
136178
- match: { test.mappings.properties.required_matches.type: long }
137179
# https://github.com/opensearch-project/OpenSearch/tree/main/rest-api-spec/src/main/resources/rest-api-spec/test#length
138-
- length: { test.mappings.properties: 3 }
180+
- length: { test.mappings.properties: 4 }
139181
- length: { test.mappings.properties.catalog: 1 }
140182

141183
---

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ public static class Defaults {
9393

9494
@Override
9595
public MappedFieldType keyedFieldType(String key) {
96-
return new FlatObjectFieldType(this.name() + DOT_SYMBOL + key, this.name(), valueFieldType, valueAndPathFieldType);
96+
return new FlatObjectFieldType(
97+
Strings.isNullOrEmpty(key) ? this.name() : (this.name() + DOT_SYMBOL + key),
98+
this.name(),
99+
valueFieldType,
100+
valueAndPathFieldType
101+
);
97102
}
98103

99104
/**
@@ -177,7 +182,7 @@ public FlatObjectFieldType(
177182
new TextSearchInfo(Defaults.FIELD_TYPE, null, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER),
178183
Collections.emptyMap()
179184
);
180-
assert rootFieldName == null || (name.length() > rootFieldName.length() && name.startsWith(rootFieldName));
185+
assert rootFieldName == null || (name.length() >= rootFieldName.length() && name.startsWith(rootFieldName));
181186
this.ignoreAbove = Integer.MAX_VALUE;
182187
this.nullValue = null;
183188
this.rootFieldName = rootFieldName;

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

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.apache.lucene.search.TermQuery;
1818
import org.apache.lucene.util.BytesRef;
1919
import org.opensearch.common.TriFunction;
20+
import org.opensearch.common.util.set.Sets;
2021
import org.opensearch.common.xcontent.XContentFactory;
2122
import org.opensearch.common.xcontent.json.JsonXContent;
2223
import org.opensearch.core.xcontent.ToXContent;
@@ -27,6 +28,7 @@
2728

2829
import java.io.IOException;
2930
import java.util.List;
31+
import java.util.Set;
3032

3133
import static org.opensearch.index.mapper.FlatObjectFieldMapper.CONTENT_TYPE;
3234
import static org.opensearch.index.mapper.FlatObjectFieldMapper.VALUE_AND_PATH_SUFFIX;
@@ -397,7 +399,17 @@ public void testFetchDocValues() throws IOException {
397399
Throwable throwable = assertThrows(IllegalArgumentException.class, () -> ft.docValueFormat(null, null));
398400
assertEquals("Field [field] of type [flat_object] does not support doc_value in root field", throwable.getMessage());
399401
}
402+
}
403+
404+
public void testPatternMatch() throws IOException {
405+
MapperService mapperService = createMapperService(
406+
fieldMapping(b -> b.startObject("properties").startObject("foo").field("type", "flat_object").endObject().endObject())
407+
);
400408

409+
QueryShardContext queryShardContext = createQueryShardContext(mapperService);
410+
Set<String> fields = queryShardContext.simpleMatchToIndexNames("field.*");
411+
assertEquals(1, fields.size());
412+
assertEquals(Sets.newHashSet("field.foo"), fields);
401413
}
402414

403415
@Override

0 commit comments

Comments
 (0)