# Query which return number range

**URL:** <https://discuss.elastic.co/t/query-which-return-number-range/322349>\
**Category:** Kibana\
**Created:** [January 3, 2023, 8:25am UTC](https://discuss.elastic.co/t/query-which-return-number-range/322349 "2023-01-03T08:25:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serhii\_Maistruk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/serhii_maistruk/32/98588_2.png) [@Serhii\_Maistruk](https://discuss.elastic.co/u/Serhii_Maistruk)\
**Post date:** [January 3, 2023, 8:25am UTC](https://discuss.elastic.co/t/query-which-return-number-range/322349/1 "2023-01-03T08:25:25Z")

</div>

Hi there,  
maybe someone has an idea if i'm able to do following query using kibana vizualization:  
I have huge index tag filled with phone numbers, and i need to check if they are "sequential".  
For example, we have next data:  
phone\_number: "1234567890"  
phone\_number: "1234567891"  
phone\_number: "1234567892"  
phone\_number: "1234567893"  
phone\_number: "1234567894"  
phone\_number: "1234567895"  
phone\_number: "1234567896"  
phone\_number: "1234567897"  
phone\_number: "1234567898"  
phone\_number: "1234567899"  
i need to build a query which would return 12345678\*.  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)\
**Post date:** [January 3, 2023, 3:57pm UTC](https://discuss.elastic.co/t/query-which-return-number-range/322349/2 "2023-01-03T15:57:02Z")

</div>

Hi @Serhii_Maistruk.

I think you could use a histogram aggregation that uses an interval of 10. This will count the number of documents for a range rounded down to the nearest 10. Then you could look for responses that have a `doc_count` of 10 which might indicate that a complete series of sequential phone numbers exist.

However, you may have duplicate phone numbers in your index. So you may need an additional subaggregation for cardinality. In this case, you only want to count unique phone numbers and look for buckets that have a `unique_count` of 10. I pasted an example from Kibana's DevConsole below.

```auto
PUT phone
{
  "mappings": {
    "properties": {
      "phone_number": {
        "type": "integer"
      }
    }
  }
}

POST phone/_bulk
{"create":{}}
{"phone_number": "1234567890"}
{"create":{}}
{"phone_number": "1234567891"}
{"create":{}}
{"phone_number": "1234567892"}
{"create":{}}
{"phone_number": "1234567893"}
{"create":{}}
{"phone_number": "1234567894"}
{"create":{}}
{"phone_number": "1234567895"}
{"create":{}}
{"phone_number": "1234567896"}
{"create":{}}
{"phone_number": "1234567897"}
{"create":{}}
{"phone_number": "1234567898"}
{"create":{}}
{"phone_number": "1234567899"}
{"create":{}}
{"phone_number": "1234567885"}
{"create":{}}
{"phone_number": "1234567886"}
{"create":{}}
{"phone_number": "1234567887"}
{"create":{}}
{"phone_number": "1234567888"}
{"create":{}}
{"phone_number": "1234567889"}

POST /phone/_search?size=0
{
  "aggs": {
    "phone_ranges": {
      "histogram": {
        "field": "phone_number",
        "interval": 10
      },
      "aggs": {
        "unique_count": {
          "cardinality": {
            "field": "phone_number"
          }
        }
      }
    }
  }
}

```

---

<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:** [January 31, 2023, 3:57pm UTC](https://discuss.elastic.co/t/query-which-return-number-range/322349/3 "2023-01-31T15:57:31Z")

</div>

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