-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically flatten objects when subobjects:false (#97972)
While ingesting documents that contain nested objects and the mapping property subobjects is set to false instead of throwing a mapping exception and dropping the document(s), we map only leaf field(s) with their full path as their name separated by dots.
- Loading branch information
Showing
7 changed files
with
755 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pr: 97972 | ||
summary: Automatically flatten objects when subobjects:false | ||
area: Mapping | ||
type: enhancement | ||
issues: | ||
- 88934 |
47 changes: 47 additions & 0 deletions
47
libs/x-content/src/main/java/org/elasticsearch/xcontent/FlatteningXContentParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.xcontent; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* A subclass of XContentSubParser that provides the functionality to flatten | ||
* the field names by prefixing them with the provided parent name. | ||
*/ | ||
public class FlatteningXContentParser extends XContentSubParser { | ||
private final String parentName; | ||
private static final char DELIMITER = '.'; | ||
|
||
/** | ||
* Constructs a FlatteningXContentParser with the given parent name and wraps an existing XContentParser. | ||
* | ||
* @param parser The XContentParser to be wrapped and extended with flattening functionality. | ||
* @param parentName The parent name to be used as a prefix for immediate children. | ||
*/ | ||
public FlatteningXContentParser(XContentParser parser, String parentName) { | ||
super(parser); | ||
this.parentName = parentName; | ||
} | ||
|
||
/** | ||
* Retrieves the name of the current field being parsed. If the current parsing level is 1, | ||
* the returned field name will be constructed by prepending the parent name to the | ||
* delegate's currentFieldName, otherwise just delegate. | ||
* | ||
* @return The current field name, potentially modified by prepending the parent name as a prefix. | ||
* @throws IOException If an I/O error occurs during parsing. | ||
*/ | ||
@Override | ||
public String currentName() throws IOException { | ||
if (level() == 1) { | ||
return new StringBuilder(parentName).append(DELIMITER).append(delegate().currentName()).toString(); | ||
} | ||
return delegate().currentName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,4 +77,8 @@ public void close() throws IOException { | |
} | ||
} | ||
} | ||
|
||
int level() { | ||
return level; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.