Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Add wildcard query impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-chen committed Aug 11, 2020
1 parent e950412 commit a2dcde4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.RangeQuery;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.RangeQuery.Comparison;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.TermQuery;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.WildcardQuery;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.serialization.ExpressionSerializer;
import com.amazon.opendistroforelasticsearch.sql.expression.Expression;
import com.amazon.opendistroforelasticsearch.sql.expression.ExpressionNodeVisitor;
Expand Down Expand Up @@ -58,6 +59,7 @@ public class FilterQueryBuilder extends ExpressionNodeVisitor<QueryBuilder, Obje
.put(BuiltinFunctionName.GREATER.getName(), new RangeQuery(Comparison.GT))
.put(BuiltinFunctionName.LTE.getName(), new RangeQuery(Comparison.LTE))
.put(BuiltinFunctionName.GTE.getName(), new RangeQuery(Comparison.GTE))
.put(BuiltinFunctionName.LIKE.getName(), new WildcardQuery())
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/

package com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

/**
* Lucene wildcard query that builds DSL query.
*/
public class WildcardQuery extends LuceneQuery {

@Override
protected QueryBuilder doBuild(String fieldName, ExprType fieldType, ExprValue literal) {
String matchText = literal.stringValue()
.replace('%', '*')
.replace('_', '?');
return QueryBuilders.wildcardQuery(fieldName, matchText);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void should_return_null_if_exception() {
}

@Test
void can_build_query_for_equality_expression() {
void should_build_term_query_for_equality_expression() {
assertEquals(
"{\n"
+ " \"term\" : {\n"
Expand All @@ -85,7 +85,7 @@ void can_build_query_for_equality_expression() {
}

@Test
void can_build_query_for_comparison_expression() {
void should_build_range_query_for_comparison_expression() {
Expression[] params = {ref("age", INTEGER), literal(30)};
Map<Expression, Object[]> ranges = ImmutableMap.of(
dsl.less(params), new Object[]{null, 30, true, false},
Expand All @@ -110,7 +110,24 @@ void can_build_query_for_comparison_expression() {
}

@Test
void can_build_query_for_function_expression() {
void should_build_wildcard_query_for_like_expression() {
assertEquals(
"{\n"
+ " \"wildcard\" : {\n"
+ " \"name\" : {\n"
+ " \"wildcard\" : \"*John?\",\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ " }\n"
+ "}",
buildQuery(
dsl.like(
ref("name", STRING), literal("%John_")))
);
}

@Test
void should_build_script_query_for_function_expression() {
doAnswer(invocation -> {
Expression expr = invocation.getArgument(0);
return expr.toString();
Expand All @@ -132,7 +149,29 @@ void can_build_query_for_function_expression() {
}

@Test
void can_build_query_for_and_or_expression() {
void should_build_script_query_for_comparison_between_fields() {
doAnswer(invocation -> {
Expression expr = invocation.getArgument(0);
return expr.toString();
}).when(serializer).serialize(any());

assertEquals(
"{\n"
+ " \"script\" : {\n"
+ " \"script\" : {\n"
+ " \"source\" : \"age1 = age2\",\n"
+ " \"lang\" : \"opendistro_expression\"\n"
+ " },\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}",
buildQuery(
dsl.equal(
ref("age1", INTEGER), ref("age2", INTEGER))));
}

@Test
void can_build_bool_query_for_and_or_expression() {
String[] names = { "must", "should" };
FunctionExpression expr1 = dsl.equal(ref("name", ES_TEXT_KEYWORD), literal("John"));
FunctionExpression expr2 = dsl.equal(ref("age", INTEGER), literal(30));
Expand Down Expand Up @@ -172,7 +211,7 @@ void can_build_query_for_and_or_expression() {
}

@Test
void can_build_query_for_not_expression() {
void can_build_bool_query_for_not_expression() {
assertEquals(
"{\n"
+ " \"bool\" : {\n"
Expand Down

0 comments on commit a2dcde4

Please sign in to comment.