# Query whether an integer is within a range of field values

**URL:** <https://discuss.elastic.co/t/query-whether-an-integer-is-within-a-range-of-field-values/142597>\
**Category:** Elasticsearch\
**Created:** [August 1, 2018, 2:39pm UTC](https://discuss.elastic.co/t/query-whether-an-integer-is-within-a-range-of-field-values/142597 "2018-08-01T14:39:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![achton](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/achton/32/33962_2.png) [@achton](https://discuss.elastic.co/u/achton)\
**Post date:** [August 1, 2018, 2:39pm UTC](https://discuss.elastic.co/t/query-whether-an-integer-is-within-a-range-of-field-values/142597/1 "2018-08-01T14:39:50Z")

</div>

I have an index where documents contain two fields that together make up a range, ie:

```
    "office_postal_from" : [
      "1500",
      "1800",
      "2000"
    ],
    "office_postal_to" : [
      "1799",
      "1999",
      "2000"
    ]

```

([See this gist for a complete example](https://gist.github.com/achton/8d47582bddcd97c08a3a45d923f300ca))

So in the case above, there are 3 ranges: 1500-1799, 1800-1999, 2000-2000 (disregard the obvious case for improving the range data here).

How can I query ES with the question: "give me all documents in which the postal code 1650 is within a range in the document" - and then receive the above document as one of the results?

I'm not very proficient with ES, but I have looked around quite a bit now, and have been entirely unable to get ideas on how to achieve this.

I'm using ES 5.2 btw.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 1, 2018, 3:01pm UTC](https://discuss.elastic.co/t/query-whether-an-integer-is-within-a-range-of-field-values/142597/2 "2018-08-01T15:01:44Z")

</div>

What about this? [https://www.elastic.co/guide/en/elasticsearch/reference/6.3/range.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/range.html)

---

<div class="post-metadata">

**Author:** ![achton](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/achton/32/33962_2.png) [@achton](https://discuss.elastic.co/u/achton)\
**Post date:** [August 1, 2018, 3:21pm UTC](https://discuss.elastic.co/t/query-whether-an-integer-is-within-a-range-of-field-values/142597/3 "2018-08-01T15:21:52Z")

</div>

@dadoonet Thanks - I will have a look at range types (again).

Just so I understand you - are you suggesting that I change the field datatype to `integer_range` so that I can do a term query with the value? The indexed data is actually coming from a CMS, so I was hoping to avoid that approach, but I can see that range types do in fact seem to fit the bill.

---

<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:** [August 29, 2018, 3:21pm UTC](https://discuss.elastic.co/t/query-whether-an-integer-is-within-a-range-of-field-values/142597/4 "2018-08-29T15:21:56Z")

</div>

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