diff --git a/opensearch/src/main/java/org/opensearch/sql/opensearch/response/OpenSearchResponse.java b/opensearch/src/main/java/org/opensearch/sql/opensearch/response/OpenSearchResponse.java index e078d48896..85a6b503f6 100644 --- a/opensearch/src/main/java/org/opensearch/sql/opensearch/response/OpenSearchResponse.java +++ b/opensearch/src/main/java/org/opensearch/sql/opensearch/response/OpenSearchResponse.java @@ -128,31 +128,22 @@ public Iterator iterator() { ImmutableMap.Builder builder = new ImmutableMap.Builder<>(); builder.putAll(docData.tupleValue()); metaDataFieldSet.forEach(metaDataField -> { - switch (metaDataField) { - case METADATA_FIELD_INDEX: - builder.put(METADATA_FIELD_INDEX, new ExprStringValue(hit.getIndex())); - break; - case METADATA_FIELD_ID: - builder.put(METADATA_FIELD_ID, new ExprStringValue(hit.getId())); - break; - case METADATA_FIELD_SCORE: - if (!Float.isNaN(hit.getScore())) { - builder.put(METADATA_FIELD_SCORE, new ExprFloatValue(hit.getScore())); - } - break; - case METADATA_FIELD_MAXSCORE: - if (maxScore != null) { - builder.put(METADATA_FIELD_MAXSCORE, maxScore); - } - break; - case METADATA_FIELD_SORT: - builder.put(METADATA_FIELD_SORT, new ExprLongValue(hit.getSeqNo())); - break; - default: - // no-op - } + if (metaDataField.equals(METADATA_FIELD_INDEX)) { + builder.put(METADATA_FIELD_INDEX, new ExprStringValue(hit.getIndex())); + } else if (metaDataField.equals(METADATA_FIELD_ID)) { + builder.put(METADATA_FIELD_ID, new ExprStringValue(hit.getId())); + } else if (metaDataField.equals(METADATA_FIELD_SCORE)) { + if (!Float.isNaN(hit.getScore())) { + builder.put(METADATA_FIELD_SCORE, new ExprFloatValue(hit.getScore())); } - ); + } else if (metaDataField.equals(METADATA_FIELD_MAXSCORE)) { + if (maxScore != null) { + builder.put(METADATA_FIELD_MAXSCORE, maxScore); + } + } else { // if (metaDataField.equals(METADATA_FIELD_SORT)) { + builder.put(METADATA_FIELD_SORT, new ExprLongValue(hit.getSeqNo())); + } + }); if (!hit.getHighlightFields().isEmpty()) { var hlBuilder = ImmutableMap.builder(); diff --git a/opensearch/src/test/java/org/opensearch/sql/opensearch/response/OpenSearchResponseTest.java b/opensearch/src/test/java/org/opensearch/sql/opensearch/response/OpenSearchResponseTest.java index b8ba37eaef..92b47bc7da 100644 --- a/opensearch/src/test/java/org/opensearch/sql/opensearch/response/OpenSearchResponseTest.java +++ b/opensearch/src/test/java/org/opensearch/sql/opensearch/response/OpenSearchResponseTest.java @@ -148,9 +148,9 @@ void iterator_metafields() { "id1", new ExprIntegerValue(1), "_index", new ExprStringValue("testIndex"), "_id", new ExprStringValue("testId"), + "_sort", new ExprLongValue(123456L), "_score", new ExprFloatValue(3.75F), - "_maxscore", new ExprFloatValue(3.75F), - "_sort", new ExprLongValue(123456L) + "_maxscore", new ExprFloatValue(3.75F) )); List includes = List.of("id1", "_index", "_id", "_sort", "_score", "_maxscore"); int i = 0;