Skip to content

Commit

Permalink
Update RelevanceFieldList to store Map<String, Float> and simplif…
Browse files Browse the repository at this point in the history
…y processing.

Signed-off-by: Yury Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Jun 4, 2022
1 parent 0817d9e commit 015aa06
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@AllArgsConstructor
public class RelevanceFieldList extends UnresolvedExpression {
@Getter
private java.util.Map<UnresolvedExpression, UnresolvedExpression> fieldList;
private java.util.Map<String, Float> fieldList;

@Override
public List<UnresolvedExpression> getChild() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))));
}

Expand All @@ -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"))));
}
Expand All @@ -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"))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,8 @@ private List<UnresolvedExpression> 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,8 @@ private List<UnresolvedExpression> 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))),
Expand Down

0 comments on commit 015aa06

Please sign in to comment.