diff --git a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MultiMatchTest.java b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MultiMatchTest.java index 6023ff0f9e..fedc77432a 100644 --- a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MultiMatchTest.java +++ b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MultiMatchTest.java @@ -40,10 +40,10 @@ class MultiMatchTest { .dsl(new ExpressionConfig().functionRepository()); private final MultiMatchQuery multiMatchQuery = new MultiMatchQuery(); private final FunctionName multiMatchName = FunctionName.of("multimatch"); - private final FunctionName multiMatchWithUnderscoreName = FunctionName.of("multi_match"); - private final FunctionName multiMatchQueryName = FunctionName.of("multi_match"); + private final FunctionName multi_matchName = FunctionName.of("multi_match"); + private final FunctionName multiMatchQueryName = FunctionName.of("multimatchquery"); private final FunctionName[] functionNames = - {multiMatchName, multiMatchWithUnderscoreName, multiMatchQueryName}; + {multiMatchName, multi_matchName, multiMatchQueryName}; private static final LiteralExpression fields_value = DSL.literal( new ExprTupleValue(new LinkedHashMap<>(ImmutableMap.of( "title", ExprValueUtils.floatValue(1.F), diff --git a/sql/src/main/antlr/OpenSearchSQLParser.g4 b/sql/src/main/antlr/OpenSearchSQLParser.g4 index 910158566c..2761344474 100644 --- a/sql/src/main/antlr/OpenSearchSQLParser.g4 +++ b/sql/src/main/antlr/OpenSearchSQLParser.g4 @@ -348,7 +348,7 @@ multiFieldRelevanceFunction LT_SQR_PRTHS field=relevanceFieldAndWeight (COMMA field=relevanceFieldAndWeight)* RT_SQR_PRTHS COMMA query=relevanceQuery (COMMA relevanceArg)* RR_BRACKET | multiFieldRelevanceFunctionName LR_BRACKET - alternateMultiMatchQueryField COMMA alternateMultiMatchQueryField + alternateMultiMatchQuery COMMA alternateMultiMatchField (COMMA alternateMultiMatchOptionalArg)* RR_BRACKET ; @@ -519,6 +519,12 @@ alternateMultiMatchOptionalArg | argName=stringLiteral EQUAL_SYMBOL argVal=relevanceArgValue ; -alternateMultiMatchQueryField +alternateMultiMatchQuery : argName=alternateMultiMatchArgName EQUAL_SYMBOL argVal=alternateMultiMatchArgVal ; + +alternateMultiMatchField + : argName=alternateMultiMatchArgName EQUAL_SYMBOL argVal=alternateMultiMatchArgVal + | argName=alternateMultiMatchArgName EQUAL_SYMBOL + LT_SQR_PRTHS argVal=alternateMultiMatchArgVal RT_SQR_PRTHS + ; \ No newline at end of file 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 d6b7cdd398..c71c19e9de 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 @@ -409,7 +409,7 @@ public UnresolvedExpression visitMultiFieldRelevanceFunction( .equals(BuiltinFunctionName.MULTIMATCH.toString()) || StringUtils.unquoteText(ctx.multiFieldRelevanceFunctionName().getText().toUpperCase()) .equals(BuiltinFunctionName.MULTIMATCHQUERY.toString())) - && ! ctx.getRuleContexts(OpenSearchSQLParser.AlternateMultiMatchQueryFieldContext.class) + && ! ctx.getRuleContexts(OpenSearchSQLParser.AlternateMultiMatchQueryContext.class) .isEmpty()) { return new Function( ctx.multiFieldRelevanceFunctionName().getText().toLowerCase(), @@ -516,32 +516,17 @@ private List alternateMultiMatchArguments( // all the arguments are defaulted to string values // to skip environment resolving and function signature resolving ImmutableList.Builder builder = ImmutableList.builder(); - String fields = ""; - String query = ""; - for (var arg : ctx.getRuleContexts( - OpenSearchSQLParser.AlternateMultiMatchQueryFieldContext.class)) { - switch (StringUtils.unquoteText(arg.argName.getText())) { - case "query": - query = StringUtils.unquoteText(arg.argVal.getText()); - break; - - case "fields": - fields = StringUtils.unquoteText(arg.argVal.getText()); - break; - - default: - throw new SemanticCheckException( - String.format("can't resolve argument %s for %s", - StringUtils.unquoteText(arg.argName.getText()), - StringUtils.unquoteText(ctx.multiFieldRelevanceFunctionName().getText())) - ); - } - } + ctx.getRuleContexts(OpenSearchSQLParser.AlternateMultiMatchFieldContext.class).stream().findFirst().ifPresent( + arg -> + builder.add(new UnresolvedArgument("fields", + new RelevanceFieldList(ImmutableMap.of(StringUtils.unquoteText(arg.argVal.getText()), 1F)))) + ); - builder.add(new UnresolvedArgument("fields", - new RelevanceFieldList(ImmutableMap.of(fields, 1F)))); - builder.add(new UnresolvedArgument("query", - new Literal(query, DataType.STRING))); + ctx.getRuleContexts(OpenSearchSQLParser.AlternateMultiMatchQueryContext.class).stream().findFirst().ifPresent( + arg -> + builder.add(new UnresolvedArgument("query", + new Literal(StringUtils.unquoteText(arg.argVal.getText()), DataType.STRING))) + ); // To support old syntax we must support argument keys as quoted strings. ctx.getRuleContexts(OpenSearchSQLParser.AlternateMultiMatchOptionalArgContext.class)