# Searching on keywords that have spaces fail using simple\_query\_string

**URL:** https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577
**Category:** Elasticsearch
**Created:** [March 2, 2022, 3:30am UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577 "2022-03-02T03:30:09Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![JJK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jjk/32/26684_2.png) [@JJK](https://discuss.elastic.co/u/JJK)
#### Post date: [March 2, 2022, 3:30am UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/1 "2022-03-02T03:30:09Z")

</div>

I have a tags keyword field defined simply as:

```auto
'tags' => [
               'type' => 'keyword'
               ],

```

I have a tag field that stores:

```auto
"tags" : [
      "523",
      "523 az"
    ],

```

I have a simple search setup as:

```auto
 $params['body']['query']['bool']['must']['simple_query_string'] = [
          'query' => $string,
          'fields' => ['tags']
        ];

```

Searching for 523 works like a charm.  
Searching for 523 az fails.

Anything with a space in the keyword field fails.  
Is there a way to get this to work with `simple_query_string`?

---

<div class="post-metadata">

### Author: ![casterQ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casterq/32/93257_2.png) [@casterQ](https://discuss.elastic.co/u/casterQ)
#### Post date: [March 2, 2022, 3:50am UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/2 "2022-03-02T03:50:01Z")

</div>

It's works in kibana, you can try it

```auto
PUT index1
{
  "mappings": {
    "properties": {
      "tags": {
        "type": "keyword"
      }
    }
  }
}
POST index1/_doc
{
  "tags": [
    "523",
    "523 az"
  ]
}
POST index1/_search
{
  "query": {
    "simple_query_string": {
      "query": "523",
      "fields": [
        "tags"
      ]
    }
  }
}
POST index1/_search
{
  "query": {
    "simple_query_string": {
      "query": "523 az",
      "fields": [
        "tags"
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)
#### Post date: [March 14, 2022, 9:55am UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/3 "2022-03-14T09:55:05Z")

</div>

I'm finding the same thing - I can't target keyword fields that have spaces in using `simple_query_string` (es version 7.8.0)

My issue here: [Discrepancy (bug or misunderstanding) in how simple\_query\_string handles queries?](https://discuss.elastic.co/t/discrepancy-bug-or-misunderstanding-in-how-simple-query-string-handles-queries/299351)

---

<div class="post-metadata">

### Author: ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)
#### Post date: [March 14, 2022, 9:59am UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/4 "2022-03-14T09:59:27Z")

</div>

I tried this without the replicated tag, '523', ie:

```auto
PUT index1
{
  "mappings": {
    "properties": {
      "tags": {
        "type": "keyword"
      }
    }
  }
}

POST index1/_doc
{
  "tags": [
    "L120 2007:09:11 98C57U495891"
  ]
}
POST index1/_search
{
  "query": {
    "simple_query_string": {
      "query": "L120 2007:09:11 98C57U495891",
      "fields": [
        "tags"
      ]
    }
  }
}

```

With 0 results:

```auto
{
  "took" : 972,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}

```

I also tried this query:

```auto
POST index1/_search
{
  "query": {
    "simple_query_string": {
      "query": "L120",
      "fields": [
        "tags"
      ]
    }
  }
}

```

and also got 0 results.

---

<div class="post-metadata">

### Author: ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)
#### Post date: [March 14, 2022, 10:08am UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/5 "2022-03-14T10:08:12Z")

</div>

@JJK It might be worth mentioning that `multi_match` is your friend here, but only up to a point. For example:

```auto
POST index1/_search
{
  "query": {
    "multi_match": {
      "query": "L120 2007:09:11 98C57U495891",
      "fields": [
        "tags^1.0"
      ],
      "operator": "and"
    }
  }
}

```

works fine, but you can't do partial value or wildcard searches, for example:

```auto
POST index1/_search
{
  "query": {
    "multi_match": {
      "query": "98C57U495891",
      "fields": [
        "tags^1.0"
      ],
      "operator": "and"
    }
  }
}

```

returns 0 results and so does:

```auto
POST index1/_search
{
  "query": {
    "multi_match": {
      "query": "*98C57U495891",
      "fields": [
        "tags^1.0"
      ],
      "operator": "and"
    }
  }
}

```

Further to this, this works:

```auto
POST index1/_search
{
  "query": {
      "query_string": {
        "query": "(L120) and (2007\\:09\\:11) and (*98C57U495891)",
        "fields": [
          "tags^1.0"
        ]
    }
  }
}

```

yet this does not:

```auto
POST index1/_search
{
  "query": {
      "query_string": {
        "query": "(L120) and (2007\\:09\\:11) and (98C57U495891)",
        "fields": [
          "tags^1.0"
        ]
    }
  }
}

```

I am confounded.

---

<div class="post-metadata">

### Author: ![JJK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jjk/32/26684_2.png) [@JJK](https://discuss.elastic.co/u/JJK)
#### Post date: [March 14, 2022, 10:16pm UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/6 "2022-03-14T22:16:26Z")

</div>

Multimatch is not something I can use for my use case.

I have found that escaping the space will bring back the correct results.  
I'm still looking for why that might be. What setting is causing the required escape on a space?  
Can't find anything yet.

---

<div class="post-metadata">

### Author: ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)
#### Post date: [March 15, 2022, 9:30am UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/7 "2022-03-15T09:30:54Z")

</div>

Interesting...is it keyword (term level) vs text (match) again? If you don't escape the space, is it attempting to tokenize the query and compare constituent parts against the fields, and because they're terms (and it's not doing a wildcard) then it doesn't match?

---

<div class="post-metadata">

### Author: ![JJK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jjk/32/26684_2.png) [@JJK](https://discuss.elastic.co/u/JJK)
#### Post date: [March 15, 2022, 7:30pm UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/8 "2022-03-15T19:30:08Z")

</div>

Even using a wildcard won't return it, unless I'm escaping the space.

---

<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: [April 12, 2022, 7:30pm UTC](https://discuss.elastic.co/t/searching-on-keywords-that-have-spaces-fail-using-simple-query-string/298577/9 "2022-04-12T19:30:51Z")

</div>

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