Skip to content
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

[7.5][ML] Handle nested arrays in source fields (#48885) #48890

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ private void addAirlineData() throws IOException {
client().performRequest(createAirlineDataNested);

bulk.append("{\"index\": {\"_index\": \"nested-data\", \"_id\": 1}}\n");
bulk.append("{\"time\":\"2016-06-01T00:00:00Z\", \"responsetime\":{\"millis\":135.22}}\n");
bulk.append("{\"time\":\"2016-06-01T00:00:00Z\", \"responsetime\":{\"millis\":135.22}, \"airline\":[{\"name\": \"foo\"}]}\n");
bulk.append("{\"index\": {\"_index\": \"nested-data\", \"_id\": 2}}\n");
bulk.append("{\"time\":\"2016-06-01T01:59:00Z\",\"responsetime\":{\"millis\":222.0}}\n");
bulk.append("{\"time\":\"2016-06-01T01:59:00Z\", \"responsetime\":{\"millis\":222.00}, \"airline\":[{\"name\": \"bar\"}]}\n");

// Create index with multiple docs per time interval for aggregation testing
Request createAirlineDataAggs = new Request("PUT", "/airline-data-aggs");
Expand Down Expand Up @@ -291,7 +291,7 @@ public void testLookbackOnlyWithScriptFields() throws Exception {
.execute();
}

public void testLookbackOnlyWithNestedFields() throws Exception {
public void testLookbackonlyWithNestedFields() throws Exception {
String jobId = "test-lookback-only-with-nested-fields";
Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId);
createJobRequest.setJsonEntity("{\n"
Expand All @@ -301,7 +301,8 @@ public void testLookbackOnlyWithNestedFields() throws Exception {
+ " \"detectors\": [\n"
+ " {\n"
+ " \"function\": \"mean\",\n"
+ " \"field_name\": \"responsetime.millis\"\n"
+ " \"field_name\": \"responsetime.millis\",\n"
+ " \"by_field_name\": \"airline.name\"\n"
+ " }\n"
+ " ]\n"
+ " },"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ private static Map<String, Object> getNextLevel(Map<String, Object> source, Stri
if (nextLevel instanceof Map<?, ?>) {
return (Map<String, Object>) source.get(key);
}
if (nextLevel instanceof List<?>) {
List<?> asList = (List<?>) nextLevel;
if (asList.isEmpty() == false) {
Object firstElement = asList.get(0);
if (firstElement instanceof Map<?, ?>) {
return (Map<String, Object>) firstElement;
}
}
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ public void testValueGivenNested() {

assertThat(nested.value(hit), equalTo(new String[] { "bar" }));
}

public void testValueGivenNestedArray() {
SearchHit hit = new SearchHitBuilder(42).setSource("{\"level_1\":{\"level_2\":[{\"foo\":\"bar\"}]}}").build();

ExtractedField nested = new SourceField("level_1.level_2.foo", Collections.singleton("text"));

assertThat(nested.value(hit), equalTo(new String[] { "bar" }));
}
}