Skip to content

Commit 72a3371

Browse files
authored
Adapt nested fields extraction from "fields" API output to the new un-flattened structure (#68745)
1 parent 2da9b1e commit 72a3371

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/AbstractFieldHitExtractor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.time.ZoneId;
1919
import java.util.List;
20+
import java.util.Map;
2021
import java.util.Objects;
2122

2223
/**
@@ -78,7 +79,13 @@ public void writeTo(StreamOutput out) throws IOException {
7879
@Override
7980
public Object extract(SearchHit hit) {
8081
Object value = null;
81-
DocumentField field = hit.field(fieldName);
82+
DocumentField field = null;
83+
if (hitName != null) {
84+
// a nested field value is grouped under the nested parent name (ie dep.dep_name lives under "dep":[{dep_name:value}])
85+
field = hit.field(hitName);
86+
} else {
87+
field = hit.field(fieldName);
88+
}
8289
if (field != null) {
8390
value = unwrapFieldsMultiValue(field.getValues());
8491
}
@@ -89,6 +96,10 @@ protected Object unwrapFieldsMultiValue(Object values) {
8996
if (values == null) {
9097
return null;
9198
}
99+
if (values instanceof Map && hitName != null) {
100+
// extract the sub-field from a nested field (dep.dep_name -> dep_name)
101+
return unwrapFieldsMultiValue(((Map<?,?>) values).get(fieldName.substring(hitName.length() + 1)));
102+
}
92103
if (values instanceof List) {
93104
List<?> list = (List<?>) values;
94105
if (list.isEmpty()) {

0 commit comments

Comments
 (0)