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

Commit

Permalink
Handle multi field in wildcard query too
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-chen committed Aug 17, 2020
1 parent 81822ca commit 0b2c435
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

import static com.amazon.opendistroforelasticsearch.sql.elasticsearch.data.type.ElasticsearchDataType.ES_TEXT_KEYWORD;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.expression.FunctionExpression;
Expand Down Expand Up @@ -69,4 +71,11 @@ protected QueryBuilder doBuild(String fieldName, ExprType fieldType, ExprValue l
"Subclass doesn't implement this and build method either");
}

protected String convertTextToKeyword(String fieldName, ExprType fieldType) {
if (fieldType == ES_TEXT_KEYWORD) { // Assume inner field name is always "keyword"
return fieldName + ".keyword";
}
return fieldName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import static com.amazon.opendistroforelasticsearch.sql.elasticsearch.data.type.ElasticsearchDataType.ES_TEXT_KEYWORD;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import org.elasticsearch.index.query.QueryBuilder;
Expand All @@ -30,9 +28,7 @@ public class TermQuery extends LuceneQuery {

@Override
protected QueryBuilder doBuild(String fieldName, ExprType fieldType, ExprValue literal) {
if (fieldType == ES_TEXT_KEYWORD) { // Assume inner field name is always "keyword"
fieldName += ".keyword";
}
fieldName = convertTextToKeyword(fieldName, fieldType);
return QueryBuilders.termQuery(fieldName, literal.value());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class WildcardQuery extends LuceneQuery {

@Override
protected QueryBuilder doBuild(String fieldName, ExprType fieldType, ExprValue literal) {
fieldName = convertTextToKeyword(fieldName, fieldType);
String matchText = convertSqlWildcardToLucene(literal.stringValue());
return QueryBuilders.wildcardQuery(fieldName, matchText);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void should_build_script_query_for_comparison_between_fields() {
@Test
void should_build_bool_query_for_and_or_expression() {
String[] names = { "filter", "should" };
FunctionExpression expr1 = dsl.equal(ref("name", ES_TEXT_KEYWORD), literal("John"));
FunctionExpression expr1 = dsl.equal(ref("name", STRING), literal("John"));
FunctionExpression expr2 = dsl.equal(ref("age", INTEGER), literal(30));
Expression[] exprs = {
dsl.and(expr1, expr2),
Expand All @@ -185,7 +185,7 @@ void should_build_bool_query_for_and_or_expression() {
+ " \"" + names[i] + "\" : [\n"
+ " {\n"
+ " \"term\" : {\n"
+ " \"name.keyword\" : {\n"
+ " \"name\" : {\n"
+ " \"value\" : \"John\",\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
Expand Down Expand Up @@ -233,6 +233,38 @@ void should_build_bool_query_for_not_expression() {
ref("age", INTEGER), literal(30)))));
}

@Test
void should_use_keyword_for_multi_field_in_equality_expression() {
assertEquals(
"{\n"
+ " \"term\" : {\n"
+ " \"name.keyword\" : {\n"
+ " \"value\" : \"John\",\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ " }\n"
+ "}",
buildQuery(
dsl.equal(
ref("name", ES_TEXT_KEYWORD), literal("John"))));
}

@Test
void should_use_keyword_for_multi_field_in_like_expression() {
assertEquals(
"{\n"
+ " \"wildcard\" : {\n"
+ " \"name.keyword\" : {\n"
+ " \"wildcard\" : \"John*\",\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ " }\n"
+ "}",
buildQuery(
dsl.like(
ref("name", ES_TEXT_KEYWORD), literal("John%"))));
}

private String buildQuery(Expression expr) {
return filterQueryBuilder.build(expr).toString();
}
Expand Down
2 changes: 1 addition & 1 deletion integ-test/src/test/resources/correctness/bugfixes/368.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SELECT Origin FROM kibana_sample_data_flights WHERE Origin LIKE 'London%'
SELECT Origin FROM kibana_sample_data_flights WHERE Origin LIKE 'London Hea%'

0 comments on commit 0b2c435

Please sign in to comment.