diff --git a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java index af25341034..1cd47ed51f 100644 --- a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java +++ b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/FilterQueryBuilderTest.java @@ -33,7 +33,6 @@ import java.util.stream.Stream; import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -370,41 +369,6 @@ void match_invalid_parameter() { assertEquals("Parameter invalid_parameter is invalid for match function.", msg); } - @Test - // `buildQuery` calls OpenSearch's (not plugin's) `toString()` function which prints - // - fields' weights inside quotes - // - `flags` as an integer, but not as an enum - // - parameters not in alphabetical order - @Disabled - void should_build_simple_query_string_query_with_default_parameters() { - assertJsonEquals( - "{\n" - + " \"simple_query_string\" : {\n" - + " \"query\" : \"search query\",\n" - + " \"fields\" : [\n" - + " \"field1^1.0\",\n" - + " \"field2^0.3\"\n" - + " ],\n" - + " \"default_operator\" : \"OR\",\n" - + " \"analyze_wildcard\" : false,\n" - + " \"auto_generate_synonyms_phrase_query\" : true,\n" - + " \"flags\" : \"ALL\",\n" - + " \"fuzzy_max_expansions\" : 50,\n" - + " \"fuzzy_prefix_length\" : 0,\n" - + " \"fuzzy_transpositions\" : true,\n" - + " \"lenient\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}", - buildQuery( - dsl.simple_query_string( - dsl.namedArgument("fields", DSL.literal(new ExprTupleValue( - new LinkedHashMap<>(ImmutableMap.of( - "field1", ExprValueUtils.floatValue(1.F), - "field2", ExprValueUtils.floatValue(.3F)))))), - dsl.namedArgument("query", literal("search query"))))); - } - @Test void should_build_match_phrase_query_with_default_parameters() { assertJsonEquals( @@ -425,34 +389,83 @@ void should_build_match_phrase_query_with_default_parameters() { } @Test - // Test is disabled because DSL because of: - // 1) DSL reverses order of the `fields` - // 2) `flags` are printed by OpenSearch (not by the plugin) as an integer - // 3) parameters are printed -//- not in lexicographical order - @Disabled - void should_build_simple_query_string_query_with_custom_parameters() { - assertJsonEquals( - "{\n" + // Notes for following three tests: + // 1) OpenSearch (not the plugin) might change order of fields + // 2) `flags` are printed by OpenSearch as an integer + // 3) `minimum_should_match` printed as a string + void should_build_simple_query_string_query_with_default_parameters_single_field() { + assertJsonEquals("{\n" + " \"simple_query_string\" : {\n" + " \"query\" : \"search query\",\n" + " \"fields\" : [\n" - + " \"field1^1.0\",\n" - + " \"field2^0.3\"\n" + + " \"field1^1.0\"\n" + " ],\n" + + " \"default_operator\" : \"or\",\n" + + " \"analyze_wildcard\" : false,\n" + + " \"auto_generate_synonyms_phrase_query\" : true,\n" + + " \"flags\" : -1,\n" + + " \"fuzzy_max_expansions\" : 50,\n" + + " \"fuzzy_prefix_length\" : 0,\n" + + " \"fuzzy_transpositions\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + "}", + buildQuery(dsl.simple_query_string( + dsl.namedArgument("fields", DSL.literal(new ExprTupleValue( + new LinkedHashMap<>(ImmutableMap.of( + "field1", ExprValueUtils.floatValue(1.F)))))), + dsl.namedArgument("query", literal("search query"))))); + } + + void should_build_simple_query_string_query_with_default_parameters_multiple_fields() { + var expected = "{\n" + + " \"simple_query_string\" : {\n" + + " \"query\" : \"search query\",\n" + + " \"fields\" : [%s],\n" + + " \"default_operator\" : \"or\",\n" + + " \"analyze_wildcard\" : false,\n" + + " \"auto_generate_synonyms_phrase_query\" : true,\n" + + " \"flags\" : -1,\n" + + " \"fuzzy_max_expansions\" : 50,\n" + + " \"fuzzy_prefix_length\" : 0,\n" + + " \"fuzzy_transpositions\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + "}"; + var actual = buildQuery(dsl.simple_query_string( + dsl.namedArgument("fields", DSL.literal(new ExprTupleValue( + new LinkedHashMap<>(ImmutableMap.of( + "field1", ExprValueUtils.floatValue(1.F), + "field2", ExprValueUtils.floatValue(.3F)))))), + dsl.namedArgument("query", literal("search query")))); + + var ex1 = String.format(expected, "\"field1^1.0\", \"field2^0.3\""); + var ex2 = String.format(expected, "\"field2^0.3\", \"field1^1.0\""); + assertTrue(new JSONObject(ex1).similar(new JSONObject(actual)) + || new JSONObject(ex2).similar(new JSONObject(actual)), + StringUtils.format("Actual %s doesn't match neither expected %s nor %s", actual, ex1, ex2)); + } + + @Test + void should_build_simple_query_string_query_with_custom_parameters() { + var expected = "{\n" + + " \"simple_query_string\" : {\n" + + " \"query\" : \"search query\",\n" + + " \"fields\" : [%s],\n" + " \"analyze_wildcard\" : true,\n" + " \"analyzer\" : \"keyword\",\n" + " \"auto_generate_synonyms_phrase_query\" : false,\n" - + " \"default_operator\" : \"AND\",\n" - + " \"flags\" : \"AND\",\n" + + " \"default_operator\" : \"and\",\n" + + " \"flags\" : 1,\n" + " \"fuzzy_max_expansions\" : 10,\n" + " \"fuzzy_prefix_length\" : 2,\n" + " \"fuzzy_transpositions\" : false,\n" + " \"lenient\" : false,\n" - + " \"minimum_should_match\" : 3,\n" + + " \"minimum_should_match\" : \"3\",\n" + " \"boost\" : 2.0\n" + " }\n" - + "}", - buildQuery( + + "}"; + var actual = buildQuery( dsl.simple_query_string( dsl.namedArgument("fields", DSL.literal( ExprValueUtils.tupleValue(ImmutableMap.of("field1", 1.F, "field2", .3F)))), @@ -467,7 +480,13 @@ void should_build_simple_query_string_query_with_custom_parameters() { dsl.namedArgument("fuzzy_transpositions", literal("false")), dsl.namedArgument("lenient", literal("false")), dsl.namedArgument("minimum_should_match", literal("3")), - dsl.namedArgument("boost", literal("2.0"))))); + dsl.namedArgument("boost", literal("2.0")))); + + var ex1 = String.format(expected, "\"field1^1.0\", \"field2^0.3\""); + var ex2 = String.format(expected, "\"field2^0.3\", \"field1^1.0\""); + assertTrue(new JSONObject(ex1).similar(new JSONObject(actual)) + || new JSONObject(ex2).similar(new JSONObject(actual)), + StringUtils.format("Actual %s doesn't match neither expected %s nor %s", actual, ex1, ex2)); } @Test