diff --git a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java index edb95c1c77..91b964d074 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java @@ -13,6 +13,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import lombok.Getter; @@ -165,14 +166,8 @@ public Expression visitAggregateFunction(AggregateFunction node, AnalysisContext @Override public Expression visitRelevanceFieldList(RelevanceFieldList node, AnalysisContext context) { - return new LiteralExpression(new ExprTupleValue(new LinkedHashMap<>(node - .getFieldList() - .entrySet() - .stream() - .collect(ImmutableMap.toImmutableMap( - n -> n.getKey().toString(), - n -> ExprValueUtils.floatValue((Float) ((Literal) n.getValue()).getValue()) - ))))); + return new LiteralExpression(ExprValueUtils.tupleValue( + ImmutableMap.copyOf(node.getFieldList()))); } @Override diff --git a/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java b/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java index 0a789fea26..3166fe45c3 100644 --- a/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java +++ b/core/src/main/java/org/opensearch/sql/ast/expression/RelevanceFieldList.java @@ -20,7 +20,7 @@ @AllArgsConstructor public class RelevanceFieldList extends UnresolvedExpression { @Getter - private java.util.Map fieldList; + private java.util.Map fieldList; @Override public List getChild() { diff --git a/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java index a42a916bd7..ede3bf2dbe 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java @@ -375,7 +375,7 @@ void simple_query_string_expression() { dsl.namedArgument("query", DSL.literal("sample query"))), AstDSL.function("simple_query_string", AstDSL.unresolvedArg("fields", new RelevanceFieldList(Map.of( - stringLiteral("field"), floatLiteral(1.F)))), + "field", 1.F))), AstDSL.unresolvedArg("query", stringLiteral("sample query")))); } @@ -390,7 +390,7 @@ void simple_query_string_expression_with_params() { dsl.namedArgument("analyzer", DSL.literal("keyword"))), AstDSL.function("simple_query_string", AstDSL.unresolvedArg("fields", new RelevanceFieldList(Map.of( - stringLiteral("field"), floatLiteral(1.F)))), + "field", 1.F))), AstDSL.unresolvedArg("query", stringLiteral("sample query")), AstDSL.unresolvedArg("analyzer", stringLiteral("keyword")))); } @@ -406,8 +406,7 @@ void simple_query_string_expression_two_fields() { dsl.namedArgument("query", DSL.literal("sample query"))), AstDSL.function("simple_query_string", AstDSL.unresolvedArg("fields", new RelevanceFieldList(ImmutableMap.of( - stringLiteral("field1"), floatLiteral(1.F), - stringLiteral("field2"), floatLiteral(.3F)))), + "field1", 1.F, "field2", .3F))), AstDSL.unresolvedArg("query", stringLiteral("sample query")))); } diff --git a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java index bf8148153a..6e5893d6a3 100644 --- a/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java +++ b/ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java @@ -364,10 +364,8 @@ private List multiFieldRelevanceArguments( .getRuleContexts(OpenSearchPPLParser.RelevanceFieldAndWeightContext.class) .stream() .collect(Collectors.toMap( - f -> new Literal(StringUtils.unquoteText(f.field.getText()), DataType.STRING), - f -> (f.weight == null) - ? new Literal(1F, DataType.FLOAT) - : new Literal(Float.parseFloat(f.weight.getText()), DataType.FLOAT)))); + f -> StringUtils.unquoteText(f.field.getText()), + f -> (f.weight == null) ? 1F : Float.parseFloat(f.weight.getText())))); builder.add(new UnresolvedArgument("fields", fields)); builder.add(new UnresolvedArgument("query", new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING))); diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java index d1b74bd82e..b621fe2495 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java @@ -669,8 +669,7 @@ public void canBuildSimple_query_stringRelevanceFunctionWithArguments() { function( "simple_query_string", unresolvedArg("fields", new RelevanceFieldList(ImmutableMap.of( - stringLiteral("field1"), floatLiteral(1.F), - stringLiteral("field2"), floatLiteral(3.2F)))), + "field1", 1.F, "field2", 3.2F))), unresolvedArg("query", stringLiteral("test query")), unresolvedArg("analyzer", stringLiteral("keyword")) ) diff --git a/sql/src/main/java/org/opensearch/sql/sql/parser/AstExpressionBuilder.java b/sql/src/main/java/org/opensearch/sql/sql/parser/AstExpressionBuilder.java index 767475e624..3c686b5e8e 100644 --- a/sql/src/main/java/org/opensearch/sql/sql/parser/AstExpressionBuilder.java +++ b/sql/src/main/java/org/opensearch/sql/sql/parser/AstExpressionBuilder.java @@ -424,10 +424,8 @@ private List multiFieldRelevanceArguments( .getRuleContexts(OpenSearchSQLParser.RelevanceFieldAndWeightContext.class) .stream() .collect(Collectors.toMap( - f -> new Literal(StringUtils.unquoteText(f.field.getText()), DataType.STRING), - f -> (f.weight == null) - ? new Literal(1F, DataType.FLOAT) - : new Literal(Float.parseFloat(f.weight.getText()), DataType.FLOAT)))); + f -> StringUtils.unquoteText(f.field.getText()), + f -> (f.weight == null) ? 1F : Float.parseFloat(f.weight.getText())))); builder.add(new UnresolvedArgument("fields", fields)); builder.add(new UnresolvedArgument("query", new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING))); diff --git a/sql/src/test/java/org/opensearch/sql/sql/parser/AstExpressionBuilderTest.java b/sql/src/test/java/org/opensearch/sql/sql/parser/AstExpressionBuilderTest.java index ef7d779fd9..9b6085ff49 100644 --- a/sql/src/test/java/org/opensearch/sql/sql/parser/AstExpressionBuilderTest.java +++ b/sql/src/test/java/org/opensearch/sql/sql/parser/AstExpressionBuilderTest.java @@ -451,16 +451,14 @@ public void relevanceMatch() { public void relevanceSimple_query_string() { assertEquals(AstDSL.function("simple_query_string", unresolvedArg("fields", new RelevanceFieldList(ImmutableMap.of( - stringLiteral("field2"), floatLiteral(3.2F), - stringLiteral("field1"), floatLiteral(1.F)))), + "field2", 3.2F, "field1", 1.F))), unresolvedArg("query", stringLiteral("search query"))), buildExprAst("simple_query_string(['field1', 'field2' ^ 3.2], 'search query')") ); assertEquals(AstDSL.function("simple_query_string", unresolvedArg("fields", new RelevanceFieldList(ImmutableMap.of( - stringLiteral("field2"), floatLiteral(3.2F), - stringLiteral("field1"), floatLiteral(1.F)))), + "field2", 3.2F, "field1", 1.F))), unresolvedArg("query", stringLiteral("search query")), unresolvedArg("analyzer", stringLiteral("keyword")), unresolvedArg("operator", stringLiteral("AND"))),