-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
[ML] properly nesting objects in document source #41901
Merged
benwtrent
merged 3 commits into
elastic:master
from
benwtrent:feature/ml-df-better-handling-of-object-fields
May 10, 2019
+112
−8
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -77,12 +77,12 @@ public static Stream<Map<String, Object>> extractCompositeAggregationResults(Com | |
// gather the `value` type, otherwise utilize `getValueAsString` so we don't lose formatted outputs. | ||
if (isNumericType(fieldType) || | ||
(aggResultSingleValue.getValueAsString().equals(String.valueOf(aggResultSingleValue.value())))) { | ||
document.put(aggName, aggResultSingleValue.value()); | ||
updateDocument(document, aggName, aggResultSingleValue.value()); | ||
} else { | ||
document.put(aggName, aggResultSingleValue.getValueAsString()); | ||
updateDocument(document, aggName, aggResultSingleValue.getValueAsString()); | ||
} | ||
} else if (aggResult instanceof ScriptedMetric) { | ||
document.put(aggName, ((ScriptedMetric) aggResult).aggregation()); | ||
updateDocument(document, aggName, ((ScriptedMetric) aggResult).aggregation()); | ||
} else { | ||
// Execution should never reach this point! | ||
// Creating transforms with unsupported aggregations shall not be possible | ||
|
@@ -97,4 +97,35 @@ public static Stream<Map<String, Object>> extractCompositeAggregationResults(Com | |
}); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static void updateDocument(Map<String, Object> document, String fieldName, Object value) { | ||
String[] fieldTokens = fieldName.split("\\."); | ||
Map<String, Object> internalMap = document; | ||
for (int i = 0; i < fieldTokens.length; i++) { | ||
String token = fieldTokens[i]; | ||
if (i == fieldTokens.length - 1) { | ||
if (internalMap.containsKey(token)) { | ||
logger.error("duplicate key value pairs key {} old value {} duplicate value {}", | ||
benwtrent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
token, | ||
internalMap.get(token), | ||
value); | ||
assert false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the visible effect from the user perspective when this assertion fires? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is none unless they are running with the |
||
} | ||
internalMap.put(token, value); | ||
} else { | ||
if (internalMap.containsKey(token)) { | ||
if (internalMap.get(token) instanceof Map) { | ||
internalMap = (Map<String, Object>)internalMap.get(token); | ||
} else { | ||
logger.error("mixed object types of nested {} and non-nested fields {}", fieldName, token); | ||
benwtrent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert false; | ||
} | ||
} else { | ||
Map<String, Object> newMap = new HashMap<>(); | ||
internalMap.put(token, newMap); | ||
internalMap = newMap; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
in 99% of cases
fieldName
is flat, so I wonder if we should have a shortcut optimization like:and then briefly explain as a code comment what the special handling does.
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.
That sounds good to me. It may provide a logic flow optimization, but I doubt there will be a performance one as
updateDocument
will just add the field and return.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.
Looking at how
String#split
is written, it does a handful of constant time calculations, and since we are splitting on a single char.
,String#split
when the char does not exist in the string is essentially the same as doingString.contains
once.I will add a clause that checks
if (fieldTokens.length == 1)
as an early shortcut, but I don't think it will cause any performance increase.