Home > database >  What is escape property in query_string in elasticsearch
What is escape property in query_string in elasticsearch

Time:09-17

I have a problem using wildcard in query_string in elasticsearch.I can not find documentation for the "escape" property in query_string when the value is "true" I got some results but when it is "false" the result is zero.

{
                    "query_string": {
                        "query": "*Serial\\ *",
                        "fields": [
                            "info.name^1.0"
                        ],
                        "type": "best_fields",
                        "allow_leading_wildcard": true,
                        "analyze_wildcard": true,
                        "escape": true,
                        "auto_generate_synonyms_phrase_query": true,
                        "fuzzy_transpositions": true,
                        "boost": 1.0
                    }
                }

can anybody explain the "escape" property?

CodePudding user response:

Weird that its not documented, but looking at the source code we can see:

Es query string source code

If "escape" is "true", the code calls org.apache.lucene.queryparser.classic.QueryParser.escape

On the query string, the function itself is documented as

Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.

So looks like this flag is just used to automatically escape characters for you, so you don't have to manually escape them

CodePudding user response:

Looking at the source code of QueryStringQueryBuilder.java (Github), we find

public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQueryBuilder> {

    // ...

    public static final boolean DEFAULT_ESCAPE = false;

    // ...

    private boolean escape = DEFAULT_ESCAPE;

    // ...

    public QueryStringQueryBuilder(StreamInput in) throws IOException {
        // ...
        escape = in.readBoolean();
        // ...
    }

    // ...

    protected Query doToQuery(SearchExecutionContext context) throws IOException {
        String rewrittenQueryString = escape ? org.apache.lucene.queryparser.classic.QueryParser.escape(this.queryString) : queryString;
        // ...
    }
}

So it governs whether the query string is passed through the Lucene classic QueryParser's .escape() method, or not.

This parameter appears to be relevant if you want to use Lucene's query language instead of KQL. It might be a helper parameter for Kibana's web-based query interface, where you can switch between the two dialects.

Overall - if it's not documented, it's probably not meant for public use.

  • Related