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

Commit

Permalink
Add exists query impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-chen committed Aug 11, 2020
1 parent a2dcde4 commit 40e659b
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.util.Collections.emptyMap;
import static org.elasticsearch.script.Script.DEFAULT_SCRIPT_TYPE;

import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.ExistsQuery;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.LuceneQuery;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.RangeQuery;
import com.amazon.opendistroforelasticsearch.sql.elasticsearch.storage.script.filter.lucene.RangeQuery.Comparison;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class FilterQueryBuilder extends ExpressionNodeVisitor<QueryBuilder, Obje
.put(BuiltinFunctionName.LTE.getName(), new RangeQuery(Comparison.LTE))
.put(BuiltinFunctionName.GTE.getName(), new RangeQuery(Comparison.GTE))
.put(BuiltinFunctionName.LIKE.getName(), new WildcardQuery())
.put(BuiltinFunctionName.IS_NULL.getName(), new ExistsQuery())
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.expression.FunctionExpression;
import com.amazon.opendistroforelasticsearch.sql.expression.ReferenceExpression;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

/**
* Lucene query that builds exists query by Elasticsearch DSL.
*/
public class ExistsQuery extends LuceneQuery {

@Override
public boolean canSupport(FunctionExpression func) {
return (func.getArguments().size() == 1)
&& (func.getArguments().get(0) instanceof ReferenceExpression);
}

@Override
public QueryBuilder build(FunctionExpression func) {
ReferenceExpression ref = (ReferenceExpression) func.getArguments().get(0);
String fieldName = ref.getAttr();
return QueryBuilders.existsQuery(fieldName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public QueryBuilder build(FunctionExpression func) {
* @param literal expr literal
* @return query
*/
protected abstract QueryBuilder doBuild(String fieldName, ExprType fieldType, ExprValue literal);
protected QueryBuilder doBuild(String fieldName, ExprType fieldType, ExprValue literal) {
throw new UnsupportedOperationException(
"Subclass doesn't implement this and build method either");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.elasticsearch.index.query.RangeQueryBuilder;

/**
* Lucene range query that builds range query for non-quality comparison.
* Lucene query that builds range query for non-quality comparison.
*/
@RequiredArgsConstructor
public class RangeQuery extends LuceneQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.elasticsearch.index.query.QueryBuilders;

/**
* Lucene term query that build term query for equality comparison for different data types.
* Lucene query that build term query for equality comparison.
*/
public class TermQuery extends LuceneQuery {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
import org.elasticsearch.index.query.QueryBuilders;

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

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

private String convertSqlWildcardToLucene(String text) {
return text.replace('%', '*')
.replace('_', '?');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ void should_build_term_query_for_equality_expression() {
+ "}",
buildQuery(
dsl.equal(
ref("name", STRING), literal("John")))
);
ref("name", STRING), literal("John"))));
}

@Test
Expand Down Expand Up @@ -122,8 +121,21 @@ void should_build_wildcard_query_for_like_expression() {
+ "}",
buildQuery(
dsl.like(
ref("name", STRING), literal("%John_")))
);
ref("name", STRING), literal("%John_"))));
}

@Test
void should_build_exists_query_for_is_null_expression() {
assertEquals(
"{\n"
+ " \"exists\" : {\n"
+ " \"field\" : \"name\",\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}",
buildQuery(
dsl.isnull(
ref("name", STRING))));
}

@Test
Expand Down

0 comments on commit 40e659b

Please sign in to comment.