Skip to content

Commit 5bde254

Browse files
committed
Add Open Parameters to Flat_object Field Type
Signed-off-by: kkewwei <[email protected]>
1 parent 1c0a274 commit 5bde254

File tree

7 files changed

+625
-53
lines changed

7 files changed

+625
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3232
- Add ability for Boolean and date field queries to run when only doc_values are enabled ([#11650](https://github.com/opensearch-project/OpenSearch/pull/11650))
3333
- Refactor implementations of query phase searcher, allow QueryCollectorContext to have zero collectors ([#13481](https://github.com/opensearch-project/OpenSearch/pull/13481))
3434
- Adds support to inject telemetry instances to plugins ([#13636](https://github.com/opensearch-project/OpenSearch/pull/13636))
35+
- Add Open Parameters to Flat_object Field Type ([#13853](https://github.com/opensearch-project/OpenSearch/pull/13853))
3536

3637
### Deprecated
3738

server/src/main/java/org/opensearch/common/xcontent/JsonToStringXContentParser.java

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.opensearch.core.xcontent.XContentBuilder;
1919
import org.opensearch.core.xcontent.XContentLocation;
2020
import org.opensearch.core.xcontent.XContentParser;
21+
import org.opensearch.index.mapper.MapperParsingException;
2122

2223
import java.io.IOException;
2324
import java.math.BigInteger;
@@ -44,6 +45,10 @@ public class JsonToStringXContentParser extends AbstractXContentParser {
4445

4546
private DeprecationHandler deprecationHandler;
4647

48+
private int depthLimit;
49+
private String nullValue;
50+
private int ignoreAbove;
51+
4752
private static final String VALUE_AND_PATH_SUFFIX = "._valueAndPath";
4853
private static final String VALUE_SUFFIX = "._value";
4954
private static final String DOT_SYMBOL = ".";
@@ -53,19 +58,25 @@ public JsonToStringXContentParser(
5358
NamedXContentRegistry xContentRegistry,
5459
DeprecationHandler deprecationHandler,
5560
XContentParser parser,
56-
String fieldTypeName
61+
String fieldTypeName,
62+
int depthLimit,
63+
String nullValue,
64+
int ignoreAbove
5765
) throws IOException {
5866
super(xContentRegistry, deprecationHandler);
5967
this.deprecationHandler = deprecationHandler;
6068
this.xContentRegistry = xContentRegistry;
6169
this.parser = parser;
6270
this.fieldTypeName = fieldTypeName;
71+
this.depthLimit = depthLimit;
72+
this.nullValue = nullValue;
73+
this.ignoreAbove = ignoreAbove;
6374
}
6475

6576
public XContentParser parseObject() throws IOException {
6677
builder.startObject();
6778
StringBuilder path = new StringBuilder(fieldTypeName);
68-
parseToken(path, null);
79+
parseToken(path, null, 1);
6980
builder.field(this.fieldTypeName, keyList);
7081
builder.field(this.fieldTypeName + VALUE_SUFFIX, valueList);
7182
builder.field(this.fieldTypeName + VALUE_AND_PATH_SUFFIX, valueAndPathList);
@@ -74,7 +85,15 @@ public XContentParser parseObject() throws IOException {
7485
return JsonXContent.jsonXContent.createParser(this.xContentRegistry, this.deprecationHandler, String.valueOf(jString));
7586
}
7687

77-
private void parseToken(StringBuilder path, String currentFieldName) throws IOException {
88+
private void parseToken(StringBuilder path, String currentFieldName, int depth) throws IOException {
89+
if (depth >= depthLimit) {
90+
throw new MapperParsingException(
91+
"the depth of flat_object field path [" + path + "] is bigger than maximum" + " depth [" + depthLimit + "]"
92+
);
93+
}
94+
if (depth == 1 && processNoNestedValue()) {
95+
return;
96+
}
7897

7998
while (this.parser.nextToken() != Token.END_OBJECT) {
8099
if (this.parser.currentName() != null) {
@@ -100,12 +119,12 @@ private void parseToken(StringBuilder path, String currentFieldName) throws IOEx
100119
this.keyList.add(fieldNameSuffix);
101120
}
102121
} else if (this.parser.currentToken() == Token.START_ARRAY) {
103-
parseToken(path, currentFieldName);
122+
parseToken(path, currentFieldName, depth);
104123
break;
105124
} else if (this.parser.currentToken() == Token.END_ARRAY) {
106125
// skip
107126
} else if (this.parser.currentToken() == Token.START_OBJECT) {
108-
parseToken(path, currentFieldName);
127+
parseToken(path, currentFieldName, depth + 1);
109128
int dotIndex = path.lastIndexOf(DOT_SYMBOL, path.length());
110129

111130
if (dotIndex != -1 && path.length() > currentFieldName.length()) {
@@ -115,9 +134,10 @@ private void parseToken(StringBuilder path, String currentFieldName) throws IOEx
115134
if (!path.toString().contains(currentFieldName)) {
116135
path.append(DOT_SYMBOL).append(currentFieldName);
117136
}
118-
parseValue(parsedFields);
119-
this.valueList.add(parsedFields.toString());
120-
this.valueAndPathList.add(path + EQUAL_SYMBOL + parsedFields);
137+
if (parseValue(parsedFields)) {
138+
this.valueList.add(parsedFields.toString());
139+
this.valueAndPathList.add(path + EQUAL_SYMBOL + parsedFields);
140+
}
121141
int dotIndex = path.lastIndexOf(DOT_SYMBOL, path.length());
122142
if (dotIndex != -1 && path.length() > currentFieldName.length()) {
123143
path.setLength(path.length() - currentFieldName.length() - 1);
@@ -127,13 +147,35 @@ private void parseToken(StringBuilder path, String currentFieldName) throws IOEx
127147
}
128148
}
129149

130-
private void parseValue(StringBuilder parsedFields) throws IOException {
150+
private boolean processNoNestedValue() throws IOException {
151+
if (parser.currentToken() == Token.VALUE_NULL) {
152+
if (nullValue != null) {
153+
this.valueList.add(nullValue);
154+
}
155+
return true;
156+
} else if (this.parser.currentToken() == Token.VALUE_STRING
157+
|| this.parser.currentToken() == Token.VALUE_NUMBER
158+
|| this.parser.currentToken() == Token.VALUE_BOOLEAN) {
159+
String value = this.parser.textOrNull();
160+
if (value != null && value.length() <= ignoreAbove) {
161+
this.valueList.add(value);
162+
}
163+
return true;
164+
}
165+
return false;
166+
}
167+
168+
private boolean parseValue(StringBuilder parsedFields) throws IOException {
131169
switch (this.parser.currentToken()) {
132170
case VALUE_BOOLEAN:
133171
case VALUE_NUMBER:
134172
case VALUE_STRING:
135173
case VALUE_NULL:
136-
parsedFields.append(this.parser.textOrNull());
174+
String value = this.parser.textOrNull();
175+
if (value != null && value.length() <= ignoreAbove) {
176+
parsedFields.append(value);
177+
return true;
178+
}
137179
break;
138180
// Handle other token types as needed
139181
case FIELD_NAME:
@@ -144,6 +186,7 @@ private void parseValue(StringBuilder parsedFields) throws IOException {
144186
default:
145187
throw new IOException("Unsupported token type [" + parser.currentToken() + "]");
146188
}
189+
return false;
147190
}
148191

149192
@Override

0 commit comments

Comments
 (0)