# Custom analyser for numeric string

**URL:** <https://discuss.elastic.co/t/custom-analyser-for-numeric-string/338529>\
**Category:** Elasticsearch\
**Created:** [July 17, 2023, 9:53am UTC](https://discuss.elastic.co/t/custom-analyser-for-numeric-string/338529 "2023-07-17T09:53:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmkhitaryan](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@hmkhitaryan](https://discuss.elastic.co/u/hmkhitaryan)\
**Post date:** [July 17, 2023, 9:53am UTC](https://discuss.elastic.co/t/custom-analyser-for-numeric-string/338529/1 "2023-07-17T09:53:03Z")

</div>

Hi everyone. I have this kind of issue: I have a numeric string field, seperated with dots, like "1.1.2", "11.2.1", and the like. I have a requirement to do sorting by this field, and when I try to sort by that field, it does String sorting. So I should write an analyser, so that here, in this sample, "11.2.1" comes first when 'desc' sorting by this field. I do my mapping and setting this way:

```auto
PUT your_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_sort_analyzer": {
          "tokenizer": "dot_tokenizer",
          "filter": [
            "numeric_sort_filter"
          ]
        }
      },
      "tokenizer": {
        "dot_tokenizer": {
          "type": "pattern",
          "pattern": "\\."
        }
      },
      "filter": {
        "numeric_sort_filter": {
          "type": "pattern_replace",
          "pattern": "(\\d+)",
          "replacement": "$1",
          "lowercase": true
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "your_field": {
        "type": "text",
        "analyzer": "custom_sort_analyzer",
        "fielddata": true
      }
    }
  }
}

```

But again, getting not correct behavior when I'm doing this kind of sorting :

```auto
{
"query": "my_query",
"sort": [
    {
      "my_field": {
           "order": "asc"
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![PodarcisMuralis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/podarcismuralis/32/122606_2.png) [@PodarcisMuralis](https://discuss.elastic.co/u/PodarcisMuralis)\
**Post date:** [July 17, 2023, 7:15pm UTC](https://discuss.elastic.co/t/custom-analyser-for-numeric-string/338529/2 "2023-07-17T19:15:39Z")

</div>

Hi.

You defined field as text. I think it is not suitable for numeric sorting. Keyword or keyword with numeric type would be better.

Please try this

```auto
PUT your_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_sort_analyzer": {
          "tokenizer": "dot_tokenizer",
          "filter": [
            "numeric_sort_filter"
          ]
        }
      },
      "tokenizer": {
        "dot_tokenizer": {
          "type": "pattern",
          "pattern": "\\."
        }
      },
      "filter": {
        "numeric_sort_filter": {
          "type": "pattern_replace",
          "pattern": "(\\d+)",
          "replacement": "$1",
          "lowercase": true
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "your_field": {
        "type": "keyword",
        "normalizer": "custom_sort_analyzer",
        "fielddata": true
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![hmkhitaryan](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@hmkhitaryan](https://discuss.elastic.co/u/hmkhitaryan)\
**Post date:** [July 18, 2023, 8:28pm UTC](https://discuss.elastic.co/t/custom-analyser-for-numeric-string/338529/3 "2023-07-18T20:28:07Z")

</div>

> [@PodarcisMuralis](#):
>
> `normalizer`

I tried what you suggest, and getting this error

```auto
"Mapping definition for [id] has unsupported parameters: [analyzer : custom_sort_analyzer]"

```

---

<div class="post-metadata">

**Author:** ![PodarcisMuralis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/podarcismuralis/32/122606_2.png) [@PodarcisMuralis](https://discuss.elastic.co/u/PodarcisMuralis)\
**Post date:** [July 18, 2023, 8:32pm UTC](https://discuss.elastic.co/t/custom-analyser-for-numeric-string/338529/4 "2023-07-18T20:32:19Z")

</div>

If this doesn't help, then i am out of ideas.

Please try this.

```auto
PUT your_index
{
  "settings": {
    "analysis": {
      "normalizer": {
        "custom_sort_normalizer": {
          "type": "custom",
          "filter": ["numeric_sort_filter"]
        }
      },
      "filter": {
        "numeric_sort_filter": {
          "type": "pattern_replace",
          "pattern": "(\\d+)",
          "replacement": "$1",
          "lowercase": true
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "your_field": {
        "type": "keyword",
        "normalizer": "custom_sort_normalizer"
      }
    }
  }
}

```

---

<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:** [August 15, 2023, 8:32pm UTC](https://discuss.elastic.co/t/custom-analyser-for-numeric-string/338529/5 "2023-08-15T20:32:24Z")

</div>

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