# Query\_string breaks search term on space even when keyword tokenizer is used

**URL:** <https://discuss.elastic.co/t/query-string-breaks-search-term-on-space-even-when-keyword-tokenizer-is-used/152535>\
**Category:** Elasticsearch\
**Created:** [October 15, 2018, 4:52pm UTC](https://discuss.elastic.co/t/query-string-breaks-search-term-on-space-even-when-keyword-tokenizer-is-used/152535 "2018-10-15T16:52:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomekit](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@tomekit](https://discuss.elastic.co/u/tomekit)\
**Post date:** [October 15, 2018, 4:52pm UTC](https://discuss.elastic.co/t/query-string-breaks-search-term-on-space-even-when-keyword-tokenizer-is-used/152535/1 "2018-10-15T16:52:26Z")

</div>

I am using `keyword` tokenizer for one of my `name` fields.

```
 "analyzer": {            
            "lowercase_keyword_analyzer": {
              "filter": [
                "lowercase"
              ],
              "tokenizer": "keyword"
            }
          }       

... 
    "name": {
            "type": "text",
            "analyzer":"lowercase_keyword_analyzer"        
          }
...

```

Unfortunately when I do the `query_string` search , my search phrase gets broken into multiple terms even though I've explicitly specified my `lowercase_keyword_analyzer` analyzer.

Query:

> {"query": {"query\_string" : {"default\_field": "name", "query": "some-long (specific),phrase with spaces and wild\*", "analyzer": "lowercase\_keyword\_analyzer", "quote\_analyzer": "lowercase\_keyword\_analyzer"}}}

Explain:

> "explanation":"+(name:some-long name:specific name:,phrase name:with name:spaces name:and name:wild\*) #(#\_type:substance)"}

Debug analyze:

```
{
  "analyzer": "lowercase_keyword_analyzer",
  "text": "some-long (specific),phrase with spaces and wild*"
}

```

> {"token":"some-long (specific),phrase with spaces and wild\*","start\_offset":0,"end\_offset":49,"type":"word","position":0}

Why `query_string` ignores the analyzer?

I've found this old topic from 2011 where someone asks about similar problem, with no real answer: [Query string not working with keyword tokenizer](https://discuss.elastic.co/t/query-string-not-working-with-keyword-tokenizer/3778)

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 18, 2018, 2:26pm UTC](https://discuss.elastic.co/t/query-string-breaks-search-term-on-space-even-when-keyword-tokenizer-is-used/152535/2 "2018-10-18T14:26:45Z")

</div>

Regardless of whether you use a `keyword` tokenizer, the `query_string` query breaks up a query string whenever it encounters an operator.

In your case, this happens for the parentheses `(` and `)` [that are reserved characters](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters). You need to escape these.

Furthermore, the same is true for the `*` wildcard. `*` is considered as an operator so the query parser will break on the first space before it.

As a result, your query becomes a bool query for:

- `name:some-long`
- `name:specific`
- `name:,phrase with spaces and`
- `name:wild*`

You can see this by using the Validate API. It can show you the Lucene query that your query gets written into:

```auto
GET test/_validate/query?rewrite=true
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "some-long (specific),phrase with spaces and wild*",
      "analyzer": "lowercase_keyword_analyzer",
      "quote_analyzer": "lowercase_keyword_analyzer"
    }
  }
}

```

Now, how to resolve this? You can escape the operators and whitespaces, to prevent the query parser from breaking up the query string on those:

```auto
GET test/_search
{
  "query": {
    "query_string": {
      "default_field": "name",
      "query": "some-long\\ \\(specific\\),phrase\\ with\\ spaces\\ and\\ wild*",
      "analyzer": "lowercase_keyword_analyzer",
      "quote_analyzer": "lowercase_keyword_analyzer"
    }
  }
}

```

But a better approach may be to not use the `query_string` query in combination with an analyzed field and wildcards. Things can get pretty complicated really fast. Maybe it is easier to use a [`wildcard` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html) on a `keyword` field on which you can apply a [normalizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/normalizer.html) to lowercase the token?

```auto
PUT test
{
  "settings": {
    "analysis": {
      "normalizer": {
        "lowercase_keyword_normalizer": {
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "keyword",
          "normalizer": "lowercase_keyword_normalizer"
        }
      }
    }
  }
}

PUT test/_doc/1
{
  "name": "some-long (specific),phrase with spaces and wildcard"
}

GET test/_search
{
  "query": {
    "wildcard": {
      "name": "some-long (specific),phrase with spaces and wild*"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 15, 2018, 2:26pm UTC](https://discuss.elastic.co/t/query-string-breaks-search-term-on-space-even-when-keyword-tokenizer-is-used/152535/3 "2018-11-15T14:26:47Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
