# Query String Query escaping spaces in V2.3

**URL:** <https://discuss.elastic.co/t/query-string-query-escaping-spaces-in-v2-3/81254>\
**Category:** Elasticsearch\
**Created:** [April 5, 2017, 5:57am UTC](https://discuss.elastic.co/t/query-string-query-escaping-spaces-in-v2-3/81254 "2017-04-05T05:57:35Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [April 13, 2017, 8:06am UTC](https://discuss.elastic.co/t/query-string-query-escaping-spaces-in-v2-3/81254/2 "2017-04-13T08:06:24Z")

</div>

Hi @jasw,

I'm following up here on ticket [#24082](https://github.com/elastic/elasticsearch/issues/24082) that you have created on Github. Let's start with a minimal self-contained example (tested on Elasticsearch 2.4.4):

```auto
PUT /my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "city": {
          "type": "string",
          "fields": {
            "raw": { 
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

PUT /my_index/my_type/1
{
    "city": "A B C Cleaning"
}

PUT /my_index/my_type/2
{
    "city": "Cleaning"
}

```

Now you run this query:

```auto
GET /my_index/_search
{
    "query": {
        "query_string": {
           "fields": ["city", "city.raw"],
           "query": "*A\\ B\\ C*"
        }
    }
}

```

and you would expect 1 result: "A B C Cleaning" but Elasticsearch returns no result at all. Let me quote the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-query-string-query.html#_wildcards):

> Wildcarded terms are not analyzed by default — they are lower-cased (lowercase\_expanded\_terms defaults to true) but no further analysis is done, mainly because it is impossible to accurately analyze a word that is missing some of its letters. However, by setting analyze\_wildcard to true, an attempt will be made to analyze wildcarded words before searching the term list for matching terms.

So let's try to set `analyze_wildcard` to `true`:

```auto
GET /my_index/_search
{
    "query": {
        "query_string": {
           "fields": ["city", "city.raw"],
           "query": "*A\\ B\\ C*",
           "analyze_wildcard": true
        }
    }
}

```

Now, both documents are returned, but why? Let's use the `explain` parameter:

```auto
GET /my_index/_search?explain
{
    "query": {
        "query_string": {
           "fields": ["city", "city.raw"],
           "query": "*A\\ B\\ C*",
           "analyze_wildcard": true
        }
    }
}

```

Which will reveal `"description": "city:*a*, product of:"`. So both "A B C Cleaning" and "Cleaning" contain an "a" and that's why they both match. But what we _actually_ want is something different: We want to match all documents that contain "A B C". This is called a phrase query. The [syntax of a phrase query](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-query-string-query.html#query-string-syntax) is just the search terms enclosed in double quotes. We need to escape the double-quotes because they are contained in a JSON string. Let's try this:

```auto
GET /my_index/_search
{
    "query": {
        "query_string": {
           "fields": ["city"],
           "query": "\"A B C\""
        }
    }
}

```

This returns only "A B C Cleaning" as we've expected.

A couple of thoughts:

- Wild cards, and especially leading wild cards, are really bad from a performance perspective and you should try to avoid them whenever you can. They are also not very intuitive as we've seen and there are often better alternatives.
- You don't need to include the not-analyzed sub-field "city.raw" because you'd only benefit from it for exact matches (i.e. when you want to search _exactly_ for the term "A B C Cleaning").

I hope that clarifies the confusion and helps you to proceed.

Daniel

---

_[View the full topic](https://discuss.elastic.co/t/query-string-query-escaping-spaces-in-v2-3/81254)._
