Custom analyser for numeric string

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:

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 :

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

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

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
      }
    }
  }
}

I tried what you suggest, and getting this error

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

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

Please try this.

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"
      }
    }
  }
}

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