Is there a filter for indexing/searching ordinals as their numeric values?

Viewed 44

I'd like to index and search 101st, 101nd, etc, as 101, 102, etc. Does ElasticSearch have a filter that does that?

1 Answers

You can simply use the pattern_replace char filter, to create tokens like 101, 102 from the text 101st, 101nd or 102.

I created a custom-analyzer which removed text and keep only the number so that searching for 100, 102 10 matches the tokens in the inverted index and comes in the search result.

Working example

Index def for custom analyzer using pattern_replace.

{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "tokenizer": "standard",
                    "char_filter": [
                        "my_char_filter"
                    ],
                    "filter": [
                        "lowercase"
                    ]
                }
            },
            "char_filter": {
                "my_char_filter": {
                    "type": "pattern_replace",
                    "pattern": "[a-z]+", --> note this
                    "replacement": ""
                }
            }
        }
    }
}

test this custom-analyzer using the analyze API.

POST /{your-index-name}/_analyze

    {
        "analyzer": "my_analyzer",
        "text": "101nd"
    }

{
    "tokens": [
        {
            "token": "101",
            "start_offset": 0,
            "end_offset": 5,
            "type": "<NUM>",
            "position": 0
        }
    ]
}

Another sample

POST /{your-index-name}/_analyze

{
    "analyzer": "my_analyzer",
    "text": "102st"
}

{
    "tokens": [
        {
            "token": "102",
            "start_offset": 0,
            "end_offset": 5,
            "type": "<NUM>",
            "position": 0
        }
    ]
}
Related