Skip to content

Commit

Permalink
Bugfix source parsing. Fix recursion add integer case.
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Carroll <carrofin@amazon.com>
  • Loading branch information
finnegancarroll committed Nov 25, 2024
1 parent 1937f5f commit 5a60d4e
Showing 1 changed file with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ private static List<Hit> getHits(org.opensearch.action.search.SearchResponse res

//[optional] Contains field values for the documents.
//optional .google.protobuf.Struct fields = 6;
Value docFieldsVal = docFieldsToStruct(hit.getFields());
Value docFieldsVal = sourceMapToProto(hit.getFields());
if (!docFieldsVal.hasStructValue()) {
throw new IllegalArgumentException("Document fields do not parse to struct");
}
Expand Down Expand Up @@ -609,7 +609,7 @@ private static List<Hit> getHits(org.opensearch.action.search.SearchResponse res

//[optional] Source document.
//optional .google.protobuf.Struct source = 16;
Value sourceVal = docFieldsToStruct(hit.getSourceAsMap());
Value sourceVal = sourceMapToProto(hit.getSourceAsMap());
if (!sourceVal.hasStructValue()) {
throw new IllegalArgumentException("Document fields do not parse to struct");
}
Expand All @@ -636,32 +636,38 @@ private static List<Hit> getHits(org.opensearch.action.search.SearchResponse res
return hits;
}

private static com.google.protobuf.Value docFieldsToStruct(Object docFieldRoot) {
private static com.google.protobuf.Value sourceMapToProto(Object sourceRoot) {
Value.Builder val = Value.newBuilder();
sourceMapToProto(val, sourceRoot);
return val.build();
}

if (docFieldRoot instanceof Double) {
val.setNumberValue((double) docFieldRoot);
} else if (docFieldRoot instanceof String) {
val.setStringValue((String) docFieldRoot);
} else if (docFieldRoot instanceof Boolean) {
val.setBoolValue((Boolean) docFieldRoot);
} else if (docFieldRoot instanceof List) {
private static void sourceMapToProto(Value.Builder result, Object sourceRoot) {
if (sourceRoot instanceof Integer) {
// TODO: Value proto definition does not include Integer field
result.setNumberValue((int) sourceRoot);
} else if (sourceRoot instanceof Double) {
result.setNumberValue((double) sourceRoot);
} else if (sourceRoot instanceof String) {
result.setStringValue((String) sourceRoot);
} else if (sourceRoot instanceof Boolean) {
result.setBoolValue((Boolean) sourceRoot);
} else if (sourceRoot instanceof List) {
ListValue.Builder listBuilder = ListValue.newBuilder();
for (Object listEntry : (List) docFieldRoot) {
listBuilder.addValues(docFieldsToStruct(listEntry));
for (Object listEntry : (List) sourceRoot) {
listBuilder.addValues(sourceMapToProto(listEntry));
}
} else if (docFieldRoot instanceof Map) {
result.setListValue(listBuilder);
} else if (sourceRoot instanceof Map) {
Struct.Builder structBuilder = Struct.newBuilder();
Map<String, Object> fieldMap = (Map<String, Object>) docFieldRoot;
Map<String, Object> fieldMap = (Map<String, Object>) sourceRoot;
for (Map.Entry<String, Object> entry : fieldMap.entrySet()) {
structBuilder.putFields(entry.getKey(), docFieldsToStruct(entry.getValue()));
structBuilder.putFields(entry.getKey(), sourceMapToProto(entry.getValue()));
}
val.setStructValue(structBuilder);
result.setStructValue(structBuilder);
} else {
throw new UnsupportedOperationException("Failed to parse document field [" + docFieldRoot.toString() + "]");
throw new UnsupportedOperationException("Failed to parse document field [" + sourceRoot.toString() + "]");
}

return val.build();
}

// TODO: Refactor RestSearchAction::checkRestTotalHits to this
Expand Down

0 comments on commit 5a60d4e

Please sign in to comment.