# Elasticsearch alphanumeric range search

**URL:** <https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620>\
**Category:** Elasticsearch\
**Created:** [September 23, 2019, 4:01am UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620 "2019-09-23T04:01:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [September 23, 2019, 4:01am UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/1 "2019-09-23T04:01:12Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 23, 2019, 8:18am UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/2 "2019-09-23T08:18:27Z")

</div>

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:

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

```

You can query like this:

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

```

---

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [September 23, 2019, 4:07pm UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/3 "2019-09-23T16:07:40Z")

</div>

Thanks a lot @abdon I shall try your solution

---

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [September 30, 2019, 3:01pm UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/4 "2019-09-30T15:01:38Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/e/0e37ab52fbc4f953a66cfd4e31deb3190e3a1f55.png)

Please help in giving some suggestion to it.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 30, 2019, 3:06pm UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/5 "2019-09-30T15:06:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [September 30, 2019, 3:37pm UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/6 "2019-09-30T15:37:45Z")

</div>

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](https://us1.discourse-cdn.com/elastic/original/3X/8/0/80dcc6984e01aee57b3e8714d50d22a6ed5f5236.png)

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 30, 2019, 3:50pm UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/7 "2019-09-30T15:50:55Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [September 30, 2019, 4:36pm UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/8 "2019-09-30T16:36:58Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html)

---

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [October 8, 2019, 12:30am UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/9 "2019-10-08T00:30:24Z")

</div>

> [@abdon](#):
>
> lte

Thanks a lot @abdon its working fine

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 8, 2019, 6:51am UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/10 "2019-10-08T06:51:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anurup\_Borah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anurup_borah/32/54344_2.png) [@Anurup\_Borah](https://discuss.elastic.co/u/Anurup_Borah)\
**Post date:** [October 11, 2019, 1:55am UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/11 "2019-10-11T01:55:07Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/c/8c310d34f2c412f01be8301cb641aa63944e4bf8.png)

Thanks a lot both for your help

---

<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:** [November 8, 2019, 2:05am UTC](https://discuss.elastic.co/t/elasticsearch-alphanumeric-range-search/200620/12 "2019-11-08T02:05:48Z")

</div>

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