Skip to content

Commit 2e901a3

Browse files
committed
fix the response format
Signed-off-by: Jialiang Liang <jiallian@amazon.com>
1 parent 38ad5e8 commit 2e901a3

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

protocol/src/main/java/org/opensearch/sql/protocol/response/QueryResult.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66
package org.opensearch.sql.protocol.response;
77

8+
import static org.opensearch.sql.expression.HighlightExpression.HIGHLIGHT_FIELD;
9+
810
import java.util.Collection;
911
import java.util.Iterator;
1012
import java.util.LinkedHashMap;
13+
import java.util.List;
1114
import java.util.Locale;
1215
import java.util.Map;
16+
import java.util.stream.Collectors;
1317
import lombok.Getter;
1418
import org.opensearch.sql.data.model.ExprValue;
1519
import org.opensearch.sql.data.model.ExprValueUtils;
@@ -82,19 +86,46 @@ public Map<String, String> columnNameTypes() {
8286

8387
@Override
8488
public Iterator<Object[]> iterator() {
85-
// Any chance to avoid copy for json response generation?
8689
return exprValues.stream()
8790
.map(ExprValueUtils::getTupleValue)
88-
.map(Map::values)
89-
.map(this::convertExprValuesToValues)
91+
.map(
92+
tuple ->
93+
tuple.entrySet().stream()
94+
.filter(e -> !HIGHLIGHT_FIELD.equals(e.getKey()))
95+
.map(e -> e.getValue().value())
96+
.toArray(Object[]::new))
9097
.iterator();
9198
}
9299

93-
private String getColumnName(Column column) {
94-
return (column.getAlias() != null) ? column.getAlias() : column.getName();
100+
/**
101+
* Extract highlight data from each result row. Each row may contain a {@code _highlight} field
102+
* added by {@code OpenSearchResponse.addHighlightsToBuilder()} and preserved through projection.
103+
* Returns a list parallel to datarows where each entry is either a map of field name to highlight
104+
* fragments, or null if no highlight data exists for that row.
105+
*/
106+
public List<Map<String, Object>> highlights() {
107+
return exprValues.stream()
108+
.map(ExprValueUtils::getTupleValue)
109+
.map(
110+
tuple -> {
111+
ExprValue hl = tuple.get(HIGHLIGHT_FIELD);
112+
if (hl == null || hl.isMissing() || hl.isNull()) {
113+
return null;
114+
}
115+
Map<String, Object> hlMap = new LinkedHashMap<>();
116+
for (Map.Entry<String, ExprValue> entry : hl.tupleValue().entrySet()) {
117+
hlMap.put(
118+
entry.getKey(),
119+
entry.getValue().collectionValue().stream()
120+
.map(ExprValue::stringValue)
121+
.collect(Collectors.toList()));
122+
}
123+
return (Map<String, Object>) hlMap;
124+
})
125+
.collect(Collectors.toList());
95126
}
96127

97-
private Object[] convertExprValuesToValues(Collection<ExprValue> exprValues) {
98-
return exprValues.stream().map(ExprValue::value).toArray(Object[]::new);
128+
private String getColumnName(Column column) {
129+
return (column.getAlias() != null) ? column.getAlias() : column.getName();
99130
}
100131
}

protocol/src/main/java/org/opensearch/sql/protocol/response/format/SimpleJsonResponseFormatter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package org.opensearch.sql.protocol.response.format;
77

88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Objects;
911
import lombok.Builder;
1012
import lombok.Getter;
1113
import lombok.RequiredArgsConstructor;
@@ -54,6 +56,12 @@ public Object buildJsonObject(QueryResult response) {
5456
response.columnNameTypes().forEach((name, type) -> json.column(new Column(name, type)));
5557

5658
json.datarows(fetchDataRows(response));
59+
60+
List<Map<String, Object>> highlights = response.highlights();
61+
if (highlights.stream().anyMatch(Objects::nonNull)) {
62+
json.highlights(highlights);
63+
}
64+
5765
formatMetric.set(System.nanoTime() - formatTime);
5866

5967
json.profile(QueryProfiling.current().finish());
@@ -79,6 +87,7 @@ public static class JsonResponse {
7987
private final List<Column> schema;
8088

8189
private final Object[][] datarows;
90+
private final List<Map<String, Object>> highlights;
8291

8392
private long total;
8493
private long size;

0 commit comments

Comments
 (0)