# Searching with combinations of space and with and without hypens

**URL:** <https://discuss.elastic.co/t/searching-with-combinations-of-space-and-with-and-without-hypens/37119>\
**Category:** Elasticsearch\
**Created:** [December 14, 2015, 12:09pm UTC](https://discuss.elastic.co/t/searching-with-combinations-of-space-and-with-and-without-hypens/37119 "2015-12-14T12:09:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imran\_Azad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imran_azad/32/4394_2.png) [@Imran\_Azad](https://discuss.elastic.co/u/Imran_Azad)\
**Post date:** [December 14, 2015, 12:09pm UTC](https://discuss.elastic.co/t/searching-with-combinations-of-space-and-with-and-without-hypens/37119/1 "2015-12-14T12:09:46Z")

</div>

I am using elastic search 2.0 and would like to be able to search for hyphenated terms using a combination of different queries for example for the word anti-emetic it should be possible to search with:

- anti-emetic
- antiemetic
- anti emetic

What's the best way to achieve this?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [December 14, 2015, 6:36pm UTC](https://discuss.elastic.co/t/searching-with-combinations-of-space-and-with-and-without-hypens/37119/2 "2015-12-14T18:36:06Z")

</div>

The tricky bit is the "anti emetic" case, since it looks like two different tokens. You can get partially there by normalizing the hyphen case to either the "split" or "merged" case. For example:

```auto
PUT /test
{
   "settings": {
      "analysis": {
         "analyzer": {
            "merged_hyphens": {
               "tokenizer": "whitespace",
               "filter": ["lowercase", "word_delim"]
            }
         },
         "filter": {
            "word_delim": {
               "type": "word_delimiter",
               "catenate_words": true,
               "generate_word_parts": false,
               "generate_number_parts": false
            }
         }
      }
   },
   "mappings": {
      "test": {
         "properties": {
            "foo": {
               "type": "string",
               "fields": {
                  "merged": {
                     "type": "string",
                     "analyzer": "merged_hyphens"
                  }
               }
            }
         }
      }
   }
}

PUT /test/test/1
{
    "foo": "anti-emetic"
}
PUT /test/test/2
{
    "foo": "antiemetic"
}
PUT /test/test/3
{
    "foo": "anti emetic"
}

GET /test/_search
{
    "query": {
        "multi_match": {
           "query": "anti-emetic",
           "fields": ["foo", "foo.merged"]
        }
    }
}

```

That creates an analyzer that will merge hyphens into a single token using the `word_delimiter`. This makes hyphen search work fine. A search for `"anti-emetic"` will find all three variations.

The problem is the other two. A search for `"anti emetic"` will only find `["anti-emetic", "anti emetic"]`, while a search for `"antiemetic"` will only find `["anti-emetic", "antiemetic"]`.

I don't think there is a good resolution to this. If there is a small list of suffixes like this, you could use a char\_filter to normalize the text into a hyphen, or perhaps a synonym list. Or if there is a case change (`AntiEmetic`) word\_delimiter can normalize based on that.

But if they are just two different tokens, it's hard for Elasticsearch to know that those tokens are "special" and should be merged, while other random tokens are not.

---

<div class="post-metadata">

**Author:** ![Imran\_Azad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imran_azad/32/4394_2.png) [@Imran\_Azad](https://discuss.elastic.co/u/Imran_Azad)\
**Post date:** [December 15, 2015, 11:36am UTC](https://discuss.elastic.co/t/searching-with-combinations-of-space-and-with-and-without-hypens/37119/3 "2015-12-15T11:36:18Z")

</div>

This is brilliant, many thanks. I've got it working, however I'm having issues with duplicate results with the highlighter. For example searching for "antiemetic" highlights the term in foo as well as foo.merged, is there a way I can deduplicate them to only show one?

---

<div class="post-metadata">

**Author:** ![Mallikarjuna\_J\_S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mallikarjuna_j_s/32/51778_2.png) [@Mallikarjuna\_J\_S](https://discuss.elastic.co/u/Mallikarjuna_J_S)\
**Post date:** [April 11, 2017, 2:20pm UTC](https://discuss.elastic.co/t/searching-with-combinations-of-space-and-with-and-without-hypens/37119/4 "2017-04-11T14:20:12Z")

</div>

Along with @polyfractal 's answer , you can refer to this [link](https://arjunjs.wordpress.com/2017/04/02/elasticsearch-how-to-search-for-a-word-without-spaces-if-word-itself-is-combination-of-two-different-words/) to achieve search for words with and without space.

> **[Elasticsearch \[How to search for a word without spaces, if word itself is...](https://arjunjs.wordpress.com/2017/04/02/elasticsearch-how-to-search-for-a-word-without-spaces-if-word-itself-is-combination-of-two-different-words/)**
>
> I was using  Elasticsearch for search and there was this specific problem I encountered in one of the applications I was working on. So, the problem was this there are numerous words in English tha…

---

<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:** [July 5, 2017, 10:01pm UTC](https://discuss.elastic.co/t/searching-with-combinations-of-space-and-with-and-without-hypens/37119/5 "2017-07-05T22:01:17Z")

</div>


