Elasticsearch alphanumeric range search

Hi ,
I need some suggestion on how to search for any alphanumeric range search if its possible in elasticsearch.
I have gone through the below link , I am not able to find any solution for alphanumeric search.
https://www.elastic.co/guide/en/elasticsearch/reference/current/range.html
Example . I have a field called "page_number" which stores D1-D4. if user search for page number something like D3 , is there any possible way to search in elasticsearch in between pages?

Please help me suggest a solution.

You would not be able to use the range datatype for that. Instead, you could separately index the bottom and upper end of the range, and then query those values using a bool query.

For example, given this document:

PUT my_index/_doc/1
{
  "page_number_min": "D1",
  "page_number_max": "D4"
}

You can query like this:

GET my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "page_number_min.keyword": {
              "lte": "D3"
            }
          }
        },
        {
          "range": {
            "page_number_max.keyword": {
              "gte": "D3"
            }
          }
        }
      ]
    }
  }
}

Thanks a lot @abdon I shall try your solution

Hi @abdon ,
I have tried the method that you have mentioned above but its ain't working.
I have made pageno and endpageno as the image below as keyword field. but its not working

Please help in giving some suggestion to it.

Try querying pageno.keyword instead of pageno and endpageno.keyword instead of endpageno.

This has to do with that fact that your query is not analyzed, but the text fields pageno and endpageno are.

HI @abdon,
Thanks for your reply . But for me, pageno and endpageno is already a keyword field so it will not be analyzed.
Please see the below screenshot:
image

Oh, I see. You switched the lte and gte. Try this:

        {
          "range": {
            "pageno": {
              "lte": "D5"
            }
          }
        },
        {
          "range": {
            "endpageno": {
              "gte": "D5"
            }
          }
        }

Was the recommendation to use a filter clause not a should clause?

If you use a should clause you need to set the minimum_should_match

Thanks a lot @abdon its working fine

Good to hear! But Ignacio had a good point too: those range queries should be in a filter clause instead of a should clause.

HI @abdona and @Ignacio_Vera,
Thanks for helping me out in this, I have used as a part of filter to search for the page number range and its working for both alphanumeric and integer data.

Thanks a lot both for your help

1 Like