Nested queries fails to work in conjunction with doubly quoted string that includes reserved keywords

Nested queries fails to work in conjunction with doubly quoted string that includes reserved keywords, like "AND", "OR" "(" or ")".

  • Example one, query with doubly quoted string that includes reserved keywords "AND" or "OR" as below was transformed incorrectly:
"\"test AND test2 OR test3\" OR nested(data.comments, (\"test AND test2 OR test3\"))"

The expected transformed elastic search query should:

{
    "boost": 1.0,
    "should": [{
            "query_string": {
                "boost": 1.0,
                "allow_leading_wildcard": false,
                "auto_generate_synonyms_phrase_query": true,
                "default_operator": "or",
                "enable_position_increments": true,
                "escape": false,
                "fields": [],
                "fuzziness": "AUTO",
                "fuzzy_max_expansions": 50,
                "fuzzy_prefix_length": 0,
                "fuzzy_transpositions": true,
                "max_determinized_states": 10000,
                "phrase_slop": 0.0,
                "query": "\"test AND test2 OR test3\"",
                "type": "best_fields"
            }
        }, {
            "nested": {
                "boost": 1.0,
                "ignore_unmapped": true,
                "path": "data.comments",
                "query": {
                    "query_string": {
                        "boost": 1.0,
                        "allow_leading_wildcard": false,
                        "auto_generate_synonyms_phrase_query": true,
                        "default_operator": "or",
                        "enable_position_increments": true,
                        "escape": false,
                        "fields": [],
                        "fuzziness": "AUTO",
                        "fuzzy_max_expansions": 50,
                        "fuzzy_prefix_length": 0,
                        "fuzzy_transpositions": true,
                        "max_determinized_states": 10000,
                        "phrase_slop": 0.0,
                        "query": "(\"test AND test2 OR test3\")",
                        "type": "best_fields"
                    }
                },
                "score_mode": "avg"
            }
        }
    ]
}

But it was transformed to:

{
    "boost": 1.0,
    "must": [{
            "query_string": {
                "boost": 1.0,
                "allow_leading_wildcard": false,
                "auto_generate_synonyms_phrase_query": true,
                "default_operator": "or",
                "enable_position_increments": true,
                "escape": false,
                "fields": [],
                "fuzziness": "AUTO",
                "fuzzy_max_expansions": 50,
                "fuzzy_prefix_length": 0,
                "fuzzy_transpositions": true,
                "max_determinized_states": 10000,
                "phrase_slop": 0.0,
                "query": "\"test",
                "type": "best_fields"
            }
        }, {
            "query_string": {
                "boost": 1.0,
                "allow_leading_wildcard": false,
                "auto_generate_synonyms_phrase_query": true,
                "default_operator": "or",
                "enable_position_increments": true,
                "escape": false,
                "fields": [],
                "fuzziness": "AUTO",
                "fuzzy_max_expansions": 50,
                "fuzzy_prefix_length": 0,
                "fuzzy_transpositions": true,
                "max_determinized_states": 10000,
                "phrase_slop": 0.0,
                "query": "test2",
                "type": "best_fields"
            }
        }
    ],
    "should": [{
            "query_string": {
                "boost": 1.0,
                "allow_leading_wildcard": false,
                "auto_generate_synonyms_phrase_query": true,
                "default_operator": "or",
                "enable_position_increments": true,
                "escape": false,
                "fields": [],
                "fuzziness": "AUTO",
                "fuzzy_max_expansions": 50,
                "fuzzy_prefix_length": 0,
                "fuzzy_transpositions": true,
                "max_determinized_states": 10000,
                "phrase_slop": 0.0,
                "query": "test3\"",
                "type": "best_fields"
            }
        }, {
            "nested": {
                "boost": 1.0,
                "ignore_unmapped": true,
                "path": "data.comments",
                "query": {
                    "query_string": {
                        "boost": 1.0,
                        "allow_leading_wildcard": false,
                        "auto_generate_synonyms_phrase_query": true,
                        "default_operator": "or",
                        "enable_position_increments": true,
                        "escape": false,
                        "fields": [],
                        "fuzziness": "AUTO",
                        "fuzzy_max_expansions": 50,
                        "fuzzy_prefix_length": 0,
                        "fuzzy_transpositions": true,
                        "max_determinized_states": 10000,
                        "phrase_slop": 0.0,
                        "query": "(\"test AND test2 OR test3\")",
                        "type": "best_fields"
                    }
                },
                "score_mode": "avg"
            }
        }
    ]
}
  • Example two, query with doubly quoted string that includes reserved keywords "(" and ")" as below was transformed incorrectly:
"\"test(v1)\" OR nested(data.comments, (\"test(v1)\"))"

The expected transformed elastic search query should:

{
    "boost": 1.0,
    "should": [{
            "query_string": {
                "boost": 1.0,
                "allow_leading_wildcard": false,
                "auto_generate_synonyms_phrase_query": true,
                "default_operator": "or",
                "enable_position_increments": true,
                "escape": false,
                "fields": [],
                "fuzziness": "AUTO",
                "fuzzy_max_expansions": 50,
                "fuzzy_prefix_length": 0,
                "fuzzy_transpositions": true,
                "max_determinized_states": 10000,
                "phrase_slop": 0.0,
                "query": "\"test(v1)\"",
                "type": "best_fields"
            }
        }, {
            "nested": {
                "boost": 1.0,
                "ignore_unmapped": true,
                "path": "data.comments",
                "query": {
                    "query_string": {
                        "boost": 1.0,
                        "allow_leading_wildcard": false,
                        "auto_generate_synonyms_phrase_query": true,
                        "default_operator": "or",
                        "enable_position_increments": true,
                        "escape": false,
                        "fields": [],
                        "fuzziness": "AUTO",
                        "fuzzy_max_expansions": 50,
                        "fuzzy_prefix_length": 0,
                        "fuzzy_transpositions": true,
                        "max_determinized_states": 10000,
                        "phrase_slop": 0.0,
                        "query": "(\"test(v1)\")",
                        "type": "best_fields"
                    }
                },
                "score_mode": "avg"
            }
        }
    ]
}

But it was transformed to:

{
    "boost": 1.0,
    "must": [{
            "query_string": {
                "boost": 1.0,
                "allow_leading_wildcard": false,
                "auto_generate_synonyms_phrase_query": true,
                "default_operator": "or",
                "enable_position_increments": true,
                "escape": false,
                "fields": [],
                "fuzziness": "AUTO",
                "fuzzy_max_expansions": 50,
                "fuzzy_prefix_length": 0,
                "fuzzy_transpositions": true,
                "max_determinized_states": 10000,
                "phrase_slop": 0.0,
                "query": "\"test(v1)",
                "type": "best_fields"
            }
        }, {
            "query_string": {
                "boost": 1.0,
                "allow_leading_wildcard": false,
                "auto_generate_synonyms_phrase_query": true,
                "default_operator": "or",
                "enable_position_increments": true,
                "escape": false,
                "fields": [],
                "fuzziness": "AUTO",
                "fuzzy_max_expansions": 50,
                "fuzzy_prefix_length": 0,
                "fuzzy_transpositions": true,
                "max_determinized_states": 10000,
                "phrase_slop": 0.0,
                "query": "\"",
                "type": "best_fields"
            }
        }
    ],
    "should": [{
            "nested": {
                "boost": 1.0,
                "ignore_unmapped": true,
                "path": "data.comments",
                "query": {
                    "query_string": {
                        "boost": 1.0,
                        "allow_leading_wildcard": false,
                        "auto_generate_synonyms_phrase_query": true,
                        "default_operator": "or",
                        "enable_position_increments": true,
                        "escape": false,
                        "fields": [],
                        "fuzziness": "AUTO",
                        "fuzzy_max_expansions": 50,
                        "fuzzy_prefix_length": 0,
                        "fuzzy_transpositions": true,
                        "max_determinized_states": 10000,
                        "phrase_slop": 0.0,
                        "query": "(\"test(v1)\")",
                        "type": "best_fields"
                    }
                },
                "score_mode": "avg"
            }
        }
    ]
}