# Why do N-gram Tokenizers don't treat + (plus) characters as part of a token?

**URL:** <https://discuss.elastic.co/t/why-do-n-gram-tokenizers-dont-treat-plus-characters-as-part-of-a-token/281096>\
**Category:** Elasticsearch\
**Created:** [August 11, 2021, 3:38pm UTC](https://discuss.elastic.co/t/why-do-n-gram-tokenizers-dont-treat-plus-characters-as-part-of-a-token/281096 "2021-08-11T15:38:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssephillip](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@ssephillip](https://discuss.elastic.co/u/ssephillip)\
**Post date:** [August 11, 2021, 3:38pm UTC](https://discuss.elastic.co/t/why-do-n-gram-tokenizers-dont-treat-plus-characters-as-part-of-a-token/281096/1 "2021-08-11T15:38:18Z")

</div>

Hello everyone,

I have a few questions about the behavior of the N-Gram Tokenizer.  
I am relatively new to this forum, so please forgive me if I'm asking this question in a wrong way.

I created my own custom tokenizer to use it with my custom analyzer.

The tokenizer looks as follows:

```auto
"titleNgramTokenizer": {
        "type": "ngram",
        "min_gram": 2,
        "max_gram": 2,
        "token_chars": []
      }

```

The analyzer looks as follows:

```auto
"titleNgramAnalyzer": {
        "tokenizer": "titleNgramTokenizer",
        "filter": [
          "lowercase"
        ]
      }

```

  

Generally everything works fine. But...

My questions:

1. I would expect that all characters are treated as part of a token as it is described in the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html).

> Character classes that should be included in a token. [...] Defaults to `[]` (keep all characters).

However all tokens are treated as part of a token **except the + character**. The [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html) of the N-Gram Tokenizer specifies under "custom\_token\_chars":

> Custom characters that should be treated as part of a token. For example, setting this to +-\_ will make the tokenizer treat the plus, minus and underscore sign as part of a token.

So i assume this behavior is known and implemented on purpose.  
**What is the reason for this implementation?** Why is the + character treated differently? And why is it not included in any character classes such as e.g. "symbol".

1. In my implementation the - (minus) character and the \_ character are treated as part of a token, the + character is not. From the above quoted part of the documentation (custom\_token\_chars), I would assume that all three characters +-\_ would **not** be treated as part of a token.  
**Any ideas on why only the + character is not treated as part of a token?** Or am I reading too much into this part of the documentation?  
  

2. My solution to this problem would be to define the + character as custom token char as described in the above quoted documentation. **Are there any dangers of doing this (i.e. of treating the + character as part of a token)?** I am not using regex or query\_string queries.

  

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 11, 2021, 4:38pm UTC](https://discuss.elastic.co/t/why-do-n-gram-tokenizers-dont-treat-plus-characters-as-part-of-a-token/281096/2 "2021-08-11T16:38:11Z")

</div>

Hi Phillip,  
I think you may have misinterpreted the docs. Plus symbols are kept by default. The analyze API is useful for debugging this:

```auto
POST test/_analyze
{
  "analyzer": "titleNgramAnalyzer",
  "text": ["foo+bar"]
}

```

The docs for the `token_chars` param says:

> Defaults to `[]` (keep all characters).

Meaning it keeps pluses, minuses and everything else.  
When it is a non-empty array that controls what characters are kept (and everything else is thrown away). The class value `["letter"]` would only keep letters and not numbers. The value "custom" means you'll list the characters you want in the "custom\_token\_chars" parameter and the example randomly picks pluses and minuses. That particular choice might make more sense if your `token_chars was set to `["digit", "custom"]` - that way you'd get the numbers along with any negative/positive symbols.

---

<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:** [September 8, 2021, 4:38pm UTC](https://discuss.elastic.co/t/why-do-n-gram-tokenizers-dont-treat-plus-characters-as-part-of-a-token/281096/3 "2021-09-08T16:38:50Z")

</div>

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