# Word\_delimiter hyphen remove but retain

**URL:** <https://discuss.elastic.co/t/word-delimiter-hyphen-remove-but-retain/333074>\
**Category:** Elasticsearch\
**Created:** [May 10, 2023, 10:44am UTC](https://discuss.elastic.co/t/word-delimiter-hyphen-remove-but-retain/333074 "2023-05-10T10:44:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Damian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/damian/32/120867_2.png) [@Damian](https://discuss.elastic.co/u/Damian)\
**Post date:** [May 10, 2023, 10:44am UTC](https://discuss.elastic.co/t/word-delimiter-hyphen-remove-but-retain/333074/1 "2023-05-10T10:44:52Z")

</div>

Hi

I would like to retain the hyphen in between the words but remove at the beginning or at the end of the word.

For word -ecigarette I would like the hyphen to be removed and search for "ecigarette" return a result, however for e-cigarette I would like search "ecigarette" did not return results.

When I use "type\_table":["- =\> ALPHA"]  
for my custom alanyzer then the hyphen retains all the time and searches for "ecigarette" does not return resutls, when I do not use the "type\_table" then the phrase is split into two tokens  
e  
cigarette  
and search for cigarette also return results which for my settings is not correct.

for words like  
-e-cigarette  
how can I create token:  
"e-cigarette"

```auto

my custom analyzer:
 "custom_word_delimiter_filter": {
            "split_on_numerics": "false",
            "generate_word_parts": "true",
            "catenate_words": "true",
            "generate_number_parts": "true",
            "catenate_all": "false",
            "split_on_case_change": "true",
            "type": "word_delimiter",
            "catenate_numbers": "false",
            "stem_english_possessive": "false",
          "type_table":[" - => ALPHA"]
....
"my_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
           
            "filter": [
              "my_synonyms",
              "custom_word_delimiter_filter",
              "lowercase"
            ]
          }

```

thank you in advance for any suggestions

Damian

---

<div class="post-metadata">

**Author:** ![Priscilla\_Parodi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priscilla_parodi/32/43047_2.png) [@Priscilla\_Parodi](https://discuss.elastic.co/u/Priscilla_Parodi)\
**Post date:** [May 29, 2023, 7:37pm UTC](https://discuss.elastic.co/t/word-delimiter-hyphen-remove-but-retain/333074/2 "2023-05-29T19:37:48Z")

</div>

Hello @Damian,

Welcome to the community!

From my understanding, you want to create tokens retaining the hyphen between the word but removing it at the beginning or end of the word.

To achieve this, you can consider adding the [pattern\_replace character filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html). This filter utilizes [Java Regular Expressions](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html).

You can try something like this:

```auto
{
"type": "pattern_replace",
"pattern": "(^-|-$)",
"replacement": ""
}

```

In this case, the goal is for the pattern to match the hyphen at the beginning (^-) or end (-$) of the line and replace it with an empty string.

![Screen Shot 2023-05-29 at 3.14.49 PM](https://us1.discourse-cdn.com/elastic/original/3X/1/5/1597326d9c155008f66adc3212cc69cb203a912a.png)

You can try incorporating a similar filter into your existing config.

---

<div class="post-metadata">

**Author:** ![Priscilla\_Parodi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priscilla_parodi/32/43047_2.png) [@Priscilla\_Parodi](https://discuss.elastic.co/u/Priscilla_Parodi)\
**Post date:** [May 30, 2023, 4:40am UTC](https://discuss.elastic.co/t/word-delimiter-hyphen-remove-but-retain/333074/3 "2023-05-30T04:40:40Z")

</div>

Example:

```auto
PUT test-analyzer
{
  "settings": {
      "analysis" : {
        "analyzer" : {
          "my_analyzer": {
            "type" : "custom",
            "tokenizer" : "whitespace",
            "char_filter" : ["my_char_filter"]
          }
          },
            "char_filter" : {
              "my_char_filter":{
              "type": "pattern_replace",
              "pattern": "(^-|-$)",
              "replacement": ""
          }
            }
      }
  }
  }

```

```auto
GET test-analyzer/_analyze
{
  "analyzer": "my_analyzer",
  "text": "-e-cigarette"
}

```

Token:

 ![IMG_1308](https://us1.discourse-cdn.com/elastic/original/3X/6/2/620e7c86954daf9fe3d3bc5ad4cda556c839a75e.jpeg)

---

<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:** [June 27, 2023, 4:40am UTC](https://discuss.elastic.co/t/word-delimiter-hyphen-remove-but-retain/333074/4 "2023-06-27T04:40:51Z")

</div>

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