Skip to content

Commit

Permalink
Fix flat_object parsing error when an object field name is too long (#…
Browse files Browse the repository at this point in the history
…13259)


---------

Signed-off-by: naomichi-y <[email protected]>
  • Loading branch information
naomichi-y authored Apr 23, 2024
1 parent 7c6127a commit 9e62ccf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix snapshot _status API to return correct status for partial snapshots ([#12812](https://github.com/opensearch-project/OpenSearch/pull/12812))
- Improve the error messages for _stats with closed indices ([#13012](https://github.com/opensearch-project/OpenSearch/pull/13012))
- Ignore BaseRestHandler unconsumed content check as it's always consumed. ([#13290](https://github.com/opensearch-project/OpenSearch/pull/13290))
- Fix mapper_parsing_exception when using flat_object fields with names longer than 11 characters ([#13259](https://github.com/opensearch-project/OpenSearch/pull/13259))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ private void parseToken(StringBuilder path, String currentFieldName) throws IOEx
// skip
} else if (this.parser.currentToken() == Token.START_OBJECT) {
parseToken(path, currentFieldName);
int dotIndex = path.lastIndexOf(DOT_SYMBOL);
if (dotIndex != -1) {
int dotIndex = path.lastIndexOf(DOT_SYMBOL, path.length());

if (dotIndex != -1 && path.length() > currentFieldName.length()) {
path.setLength(path.length() - currentFieldName.length() - 1);
}
} else {
Expand All @@ -117,8 +118,8 @@ private void parseToken(StringBuilder path, String currentFieldName) throws IOEx
parseValue(parsedFields);
this.valueList.add(parsedFields.toString());
this.valueAndPathList.add(path + EQUAL_SYMBOL + parsedFields);
int dotIndex = path.lastIndexOf(DOT_SYMBOL);
if (dotIndex != -1) {
int dotIndex = path.lastIndexOf(DOT_SYMBOL, path.length());
if (dotIndex != -1 && path.length() > currentFieldName.length()) {
path.setLength(path.length() - currentFieldName.length() - 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,49 @@ public void testDocValue() throws Exception {
assertEquals(1, valueReaders.size());
}

public void testLongFieldNameWithHashArray() throws Exception {
String mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("test")
.startObject("properties")
.startObject("field")
.field("type", FIELD_TYPE)
.endObject()
.endObject()
.endObject()
.endObject()
.toString();
final DocumentMapper mapper = mapperService.documentMapperParser().parse("test", new CompressedXContent(mapping));

XContentBuilder json = XContentFactory.jsonBuilder()
.startObject()
.startObject("field")
.startObject("detail")
.startArray("fooooooooooo")
.startObject()
.field("name", "baz")
.endObject()
.startObject()
.field("name", "baz")
.endObject()
.endArray()
.endObject()
.endObject()
.endObject();

ParsedDocument d = mapper.parse(new SourceToParse("test", "1", BytesReference.bytes(json), MediaTypeRegistry.JSON));
writer.addDocument(d.rootDoc());
writer.commit();

IndexFieldData<?> fieldData = getForField("field");
List<LeafReaderContext> readers = refreshReader();
assertEquals(1, readers.size());

IndexFieldData<?> valueFieldData = getForField("field._value");
List<LeafReaderContext> valueReaders = refreshReader();
assertEquals(1, valueReaders.size());
}

@Override
protected String getFieldDataType() {
return FIELD_TYPE;
Expand Down

0 comments on commit 9e62ccf

Please sign in to comment.