diff --git a/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/LuceneQuery.java b/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/LuceneQuery.java index d97a2517e2..b50c741318 100644 --- a/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/LuceneQuery.java +++ b/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/LuceneQuery.java @@ -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; @@ -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; + } + } diff --git a/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/TermQuery.java b/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/TermQuery.java index 01e3c4d9f0..db07116a25 100644 --- a/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/TermQuery.java +++ b/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/TermQuery.java @@ -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; @@ -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()); } diff --git a/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/WildcardQuery.java b/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/WildcardQuery.java index 1f20663a9d..7b5c109924 100644 --- a/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/WildcardQuery.java +++ b/elasticsearch/src/main/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/lucene/WildcardQuery.java @@ -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); } diff --git a/elasticsearch/src/test/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/FilterQueryBuilderTest.java b/elasticsearch/src/test/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/FilterQueryBuilderTest.java index 5a7d93ec6b..aabbd857dc 100644 --- a/elasticsearch/src/test/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/FilterQueryBuilderTest.java +++ b/elasticsearch/src/test/java/com/amazon/opendistroforelasticsearch/sql/elasticsearch/storage/script/filter/FilterQueryBuilderTest.java @@ -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), @@ -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" @@ -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(); } diff --git a/integ-test/src/test/resources/correctness/bugfixes/368.txt b/integ-test/src/test/resources/correctness/bugfixes/368.txt index a9faaca95c..13796605cb 100644 --- a/integ-test/src/test/resources/correctness/bugfixes/368.txt +++ b/integ-test/src/test/resources/correctness/bugfixes/368.txt @@ -1 +1 @@ -SELECT Origin FROM kibana_sample_data_flights WHERE Origin LIKE 'London%' \ No newline at end of file +SELECT Origin FROM kibana_sample_data_flights WHERE Origin LIKE 'London Hea%' \ No newline at end of file