Skip to content

Commit 2f715d1

Browse files
[ML] Handle nested arrays in source fields (#48885)
1 parent 72e2d16 commit 2f715d1

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ private void addAirlineData() throws IOException {
185185
client().performRequest(createAirlineDataNested);
186186

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

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

294-
public void testLookbackOnlyWithNestedFields() throws Exception {
294+
public void testLookbackonlyWithNestedFields() throws Exception {
295295
String jobId = "test-lookback-only-with-nested-fields";
296296
Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId);
297297
createJobRequest.setJsonEntity("{\n"
@@ -301,7 +301,8 @@ public void testLookbackOnlyWithNestedFields() throws Exception {
301301
+ " \"detectors\": [\n"
302302
+ " {\n"
303303
+ " \"function\": \"mean\",\n"
304-
+ " \"field_name\": \"responsetime.millis\"\n"
304+
+ " \"field_name\": \"responsetime.millis\",\n"
305+
+ " \"by_field_name\": \"airline.name\"\n"
305306
+ " }\n"
306307
+ " ]\n"
307308
+ " },"

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/SourceField.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ private static Map<String, Object> getNextLevel(Map<String, Object> source, Stri
5454
if (nextLevel instanceof Map<?, ?>) {
5555
return (Map<String, Object>) source.get(key);
5656
}
57+
if (nextLevel instanceof List<?>) {
58+
List<?> asList = (List<?>) nextLevel;
59+
if (asList.isEmpty() == false) {
60+
Object firstElement = asList.get(0);
61+
if (firstElement instanceof Map<?, ?>) {
62+
return (Map<String, Object>) firstElement;
63+
}
64+
}
65+
}
5766
return null;
5867
}
5968

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/SourceFieldTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ public void testValueGivenNested() {
6767

6868
assertThat(nested.value(hit), equalTo(new String[] { "bar" }));
6969
}
70+
71+
public void testValueGivenNestedArray() {
72+
SearchHit hit = new SearchHitBuilder(42).setSource("{\"level_1\":{\"level_2\":[{\"foo\":\"bar\"}]}}").build();
73+
74+
ExtractedField nested = new SourceField("level_1.level_2.foo", Collections.singleton("text"));
75+
76+
assertThat(nested.value(hit), equalTo(new String[] { "bar" }));
77+
}
7078
}

0 commit comments

Comments
 (0)