# Keyword analyzer but allow redundant white spaces

**URL:** <https://discuss.elastic.co/t/keyword-analyzer-but-allow-redundant-white-spaces/110441>\
**Category:** Elasticsearch\
**Created:** [December 6, 2017, 1:07am UTC](https://discuss.elastic.co/t/keyword-analyzer-but-allow-redundant-white-spaces/110441 "2017-12-06T01:07:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 6, 2017, 1:07am UTC](https://discuss.elastic.co/t/keyword-analyzer-but-allow-redundant-white-spaces/110441/1 "2017-12-06T01:07:03Z")

</div>

My requirement is, that for example

POST /index/type/1  
{  
"title": "Hello Elasticsearch"  
}

I want to return the doc with exactly matched title but allowing redundant white spaces between words.  
For example,  
Searching "Hello[sapce]elasticsearch" and "Hello[sapce][sapce][sapce]Elasticsearch" will return the document. But "Hello" or "Elasticsearch" won't return document.

Any suggestion for what analyzer/tokenizer/filter I should use?

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [December 6, 2017, 10:32am UTC](https://discuss.elastic.co/t/keyword-analyzer-but-allow-redundant-white-spaces/110441/2 "2017-12-06T10:32:12Z")

</div>

Hi,

you can use a combination of the "pattern\_replace" and "trim" token filters to remove redundant whitespaces in your input fields and also in the query. Unfortunately you cannot use those filters as "normalizers" for "keyword" fields, but you can define the field as a "text" field and use a "keyword\_tokenizer" to get almost the same effect. Here is what I mean:

```auto
PUT index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "char_filter": [],
          "filter": [
            "whitespace_normalization",
            "trim"
          ]
        }
      },
      "filter": {
        "whitespace_normalization": {
          "type": "pattern_replace",
          "pattern": "\\s+",
          "replacement": " "
        }
      }
    }
  },
  "mappings": {
    "type": {
      "properties": {
        "title": {
          "type": "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}

```

The pattern\_replace filter should replace multiple whitespace characters by just one. It does this at index and query time for this field. So if you index:

```auto
PUT /index/type/1
{
  "title" : "Elasticsearch In Action "
}

```

You should be able to query it with a different whitespace distribution as well:

```auto
POST /index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch In Action "
    }
  }
}

```

You can check the token produced by the analysis only contains one whitespace between words:

```auto
GET /index/_analyze
{
  "analyzer": "my_analyzer", 
  "text" : " Elasticsearch In Action "
}

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 18, 2017, 4:34pm UTC](https://discuss.elastic.co/t/keyword-analyzer-but-allow-redundant-white-spaces/110441/3 "2017-12-18T16:34:36Z")

</div>

Thanks! It works as I expected!

---

<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:** [January 15, 2018, 4:35pm UTC](https://discuss.elastic.co/t/keyword-analyzer-but-allow-redundant-white-spaces/110441/4 "2018-01-15T16:35:03Z")

</div>

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