-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Make flattened synthetic source concatenate object keys on scalar/object mismatch #129600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make flattened synthetic source concatenate object keys on scalar/object mismatch #129600
Conversation
.../main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldSyntheticWriterHelper.java
Outdated
Show resolved
Hide resolved
.../main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldSyntheticWriterHelper.java
Outdated
Show resolved
Hide resolved
next = nextValue == null ? KeyValue.EMPTY : new KeyValue(nextValue); | ||
|
||
var startPrefix = curr.prefix.diff(openObjects); | ||
if (startPrefix.prefix.isEmpty() == false && startPrefix.prefix.getFirst().equals(lastScalarSingleLeaf)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the conflict doesn't happen on the first part of the prefix, e.g.
field {
path {
to: 10
to {
foo: bar
}
}
}
Would this be caught here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, so this will become:
field {
path {
to: 10
to.foo: bar
}
}
When it get to the first key/value field.path.to|10
it will take the else block and traverse down into the object, adding field
and path
to the openObject
context. When it reaches the key value field.path.to.foo|bar
that object will still be open, and seeing that lastScalarSingleLeaf
has a value of to
, and that to
is the first token in the startPrexix (to.foo
), it will make a concatenated path.
(Updated a test to this situation to verify it)
.../main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldSyntheticWriterHelper.java
Outdated
Show resolved
Hide resolved
@@ -103,7 +103,7 @@ public void testSingleObject() throws IOException { | |||
|
|||
// THEN | |||
assertEquals( | |||
"{\"a\":\"value_a\",\"a\":{\"b\":\"value_b\",\"b\":{\"c\":\"value_c\"},\"d\":\"value_d\"}}", | |||
"{\"a\":\"value_a\",\"a.b\":\"value_b\",\"a.b.c\":\"value_c\",\"a.d\":\"value_d\"}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, there was a test which had a scalar/object mismatch. But it produced duplicate keys. When the xcontent was converted to jsont these duplicate keys originally threw an error, but now just drop the duplicates. (Something must have changed in xcontent stuff since the issue was opened to cause this change from an error to deduplication)
Pinging @elastic/es-storage-engine (Team:StorageEngine) |
🔍 Preview links for changed docs: 🔔 The preview site may take up to 3 minutes to finish building. These links will become live once it completes. |
var syntheticSource = syntheticSource(mapper, b -> { | ||
b.startObject("field"); | ||
{ | ||
b.field("key1.key2", "foo"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add also: `b.field("key1", "baz");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't able to add a key1
field because there is a key1
object. I went ahead and added a few more keys with scalar values at the root level and in nested objects.
final FlattenedFieldSyntheticWriterHelper writer = new FlattenedFieldSyntheticWriterHelper(new SortedSetSortedKeyedValues(dv)); | ||
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
final XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), baos); | ||
final List<byte[]> bytes = List.of("a.b.c" + '\0' + "10", "a.b.c.d" + '\0' + "20") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add something for a
and a.b
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth leaving this one as is so that the concatenated keys occur in a nested object. But testSingleObject
has keys a
, a.b
, and a.b.c
, so it covers the scenario where keys in the root object are concatenated.
server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapperTests.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, some minor comments to address but otherwise it looks good.
💔 Backport failed
You can use sqren/backport to manually backport by running |
…ect mismatch (elastic#129600) There is an issue where for Flattened fields with synthetic source, if there is a key with a scalar value, and a duplicate key with an object value, one of the values will be left out of the produced synthetic source. This fixes the issue by replacing the object with paths to each of its keys. These paths consist of the concatenation of all keys going down to a given scalar, joined by a period. For example, they are of the form foo.bar.baz. This applies recursively, so that every value within the object, no matter how nested, will be accessible through a full specified path.
…ect mismatch (elastic#129600) There is an issue where for Flattened fields with synthetic source, if there is a key with a scalar value, and a duplicate key with an object value, one of the values will be left out of the produced synthetic source. This fixes the issue by replacing the object with paths to each of its keys. These paths consist of the concatenation of all keys going down to a given scalar, joined by a period. For example, they are of the form foo.bar.baz. This applies recursively, so that every value within the object, no matter how nested, will be accessible through a full specified path. (cherry picked from commit 245dc07) # Conflicts: # docs/reference/elasticsearch/mapping-reference/flattened.md
💚 All backports created successfully
Questions ?Please refer to the Backport tool documentation |
…ect mismatch (elastic#129600) There is an issue where for Flattened fields with synthetic source, if there is a key with a scalar value, and a duplicate key with an object value, one of the values will be left out of the produced synthetic source. This fixes the issue by replacing the object with paths to each of its keys. These paths consist of the concatenation of all keys going down to a given scalar, joined by a period. For example, they are of the form foo.bar.baz. This applies recursively, so that every value within the object, no matter how nested, will be accessible through a full specified path. (cherry picked from commit 245dc07)
…ect mismatch (#129600) (#129792) There is an issue where for Flattened fields with synthetic source, if there is a key with a scalar value, and a duplicate key with an object value, one of the values will be left out of the produced synthetic source. This fixes the issue by replacing the object with paths to each of its keys. These paths consist of the concatenation of all keys going down to a given scalar, joined by a period. For example, they are of the form foo.bar.baz. This applies recursively, so that every value within the object, no matter how nested, will be accessible through a full specified path.
…lar/object mismatch (#129600) (#129794) * Make flattened synthetic source concatenate object keys on scalar/object mismatch (#129600) There is an issue where for Flattened fields with synthetic source, if there is a key with a scalar value, and a duplicate key with an object value, one of the values will be left out of the produced synthetic source. This fixes the issue by replacing the object with paths to each of its keys. These paths consist of the concatenation of all keys going down to a given scalar, joined by a period. For example, they are of the form foo.bar.baz. This applies recursively, so that every value within the object, no matter how nested, will be accessible through a full specified path. (cherry picked from commit 245dc07) * remove methods not avaiable in java version * skip testing console-result in docs
…lar/object mismatch (#129600) (#129793) * Make flattened synthetic source concatenate object keys on scalar/object mismatch (#129600) There is an issue where for Flattened fields with synthetic source, if there is a key with a scalar value, and a duplicate key with an object value, one of the values will be left out of the produced synthetic source. This fixes the issue by replacing the object with paths to each of its keys. These paths consist of the concatenation of all keys going down to a given scalar, joined by a period. For example, they are of the form foo.bar.baz. This applies recursively, so that every value within the object, no matter how nested, will be accessible through a full specified path. (cherry picked from commit 245dc07) # Conflicts: # docs/reference/elasticsearch/mapping-reference/flattened.md * remove methods not avaiable in java version * skip testing console-result in docs
There is an issue where for Flattened fields with synthetic source, if there is a key with a scalar value, and a duplicate key with an object value, one of the values will be left out of the produced synthetic source.
This fixes the issue by replacing the problematic object with paths to each of its keys. These paths consist of the concatenation of all keys going down to a given scalar, joined by
.
. For example, they are of the formfoo.bar.baz
. This applies recursively, so that every value within the object, no matter how nested, will be accessible through a full specified path.For example if the following flattened field values is indexed:
The following synthetic source will be produced:
Fixes #122936