# Analyzer conditional token filter with regular expression

**URL:** <https://discuss.elastic.co/t/analyzer-conditional-token-filter-with-regular-expression/323943>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [January 25, 2023, 3:36pm UTC](https://discuss.elastic.co/t/analyzer-conditional-token-filter-with-regular-expression/323943 "2023-01-25T15:36:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wonder\_Garance](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wonder_garance/32/91180_2.png) [@Wonder\_Garance](https://discuss.elastic.co/u/Wonder_Garance)\
**Post date:** [January 25, 2023, 3:36pm UTC](https://discuss.elastic.co/t/analyzer-conditional-token-filter-with-regular-expression/323943/1 "2023-01-25T15:36:38Z")

</div>

Hello, in an analyzer conditional token filter, I use a painless script with the regular expression. If a token contains only letters et hypens, then the compound word in the token is splitted, otherwise no.

But my script below doesn't work. If I remove the conditional token filer, I get several words. What's wrong? Thanks.

```auto
GET /test/_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "condition",
      "filter": ["word_delimiter_graph"],
      "script": {
        "lang": "painless",
        "source": "token.toString() ==~ /^[A-Za-z-]+$/"
      }
    }
  ],
  "explain": true,
  "text": "WORD-IS-SPLITTED"
}

```

In the response, the word is not splitted:

```auto
"tokenfilters" : [
      {
        "name" : " __anonymous__ condition",
        "tokens" : [
          {
            "token" : "WORD-IS-SPLITTED",
            "start_offset" : 0,
            "end_offset" : 16,
            "type" : "word",
            "position" : 0,
            "bytes" : "[42 4f 49 2d 49 53 2d 47 50 45]",
            "keyword" : false,
            "positionLength" : 1,
            "termFrequency" : 1
          }
        ]
      }
    ]

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [January 25, 2023, 4:22pm UTC](https://discuss.elastic.co/t/analyzer-conditional-token-filter-with-regular-expression/323943/2 "2023-01-25T16:22:48Z")

</div>

Hi @Wonder_Garance

Replace token.toString() to token.getTerm().toString()

```auto
GET /test/_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "condition",
      "filter": ["word_delimiter_graph"],
      "script": {
        "lang": "painless",
        "source": "token.getTerm().toString() ==~ /^[A-Za-z-]+$/"
      }
    }
  ],
  "text": "WORD-IS-SPLITTED"
}

```

---

<div class="post-metadata">

**Author:** ![Wonder\_Garance](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wonder_garance/32/91180_2.png) [@Wonder\_Garance](https://discuss.elastic.co/u/Wonder_Garance)\
**Post date:** [January 25, 2023, 4:29pm UTC](https://discuss.elastic.co/t/analyzer-conditional-token-filter-with-regular-expression/323943/3 "2023-01-25T16:29:25Z")

</div>

Hi @RabBit_BR  
It works, thank you!

---

<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:** [February 22, 2023, 4:30pm UTC](https://discuss.elastic.co/t/analyzer-conditional-token-filter-with-regular-expression/323943/4 "2023-02-22T16:30:06Z")

</div>

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