Skip to content

Commit

Permalink
Adding square parenthesis support for fields parameter.
Browse files Browse the repository at this point in the history
Signed-off-by: forestmvey <[email protected]>
  • Loading branch information
forestmvey committed Nov 16, 2022
1 parent 94a700a commit 2f8e6ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 8 additions & 2 deletions sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand Down Expand Up @@ -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
;
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -516,32 +516,17 @@ private List<UnresolvedExpression> alternateMultiMatchArguments(
// all the arguments are defaulted to string values
// to skip environment resolving and function signature resolving
ImmutableList.Builder<UnresolvedExpression> 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)
Expand Down

0 comments on commit 2f8e6ed

Please sign in to comment.