# Java API\[5.0\] Multiple range queries

**URL:** <https://discuss.elastic.co/t/java-api-5-0-multiple-range-queries/68554>\
**Category:** Elasticsearch\
**Created:** [December 9, 2016, 11:58am UTC](https://discuss.elastic.co/t/java-api-5-0-multiple-range-queries/68554 "2016-12-09T11:58:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![girish\_pathak](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@girish\_pathak](https://discuss.elastic.co/u/girish_pathak)\
**Post date:** [December 9, 2016, 11:58am UTC](https://discuss.elastic.co/t/java-api-5-0-multiple-range-queries/68554/1 "2016-12-09T11:58:23Z")

</div>

Is it possible to add multiple range queries using the Java API[5.0] ?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 9, 2016, 3:47pm UTC](https://discuss.elastic.co/t/java-api-5-0-multiple-range-queries/68554/2 "2016-12-09T15:47:28Z")

</div>

Sure. Combine them with a `boolQuery` in whatever way make the most sense to you. [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) is the documentation for that query. Most stuff should translate cleanly from the rest-based examples on that page to the java transport API.

---

<div class="post-metadata">

**Author:** ![girish\_pathak](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@girish\_pathak](https://discuss.elastic.co/u/girish_pathak)\
**Post date:** [December 12, 2016, 6:45am UTC](https://discuss.elastic.co/t/java-api-5-0-multiple-range-queries/68554/3 "2016-12-12T06:45:10Z")

</div>

Hi Nik I have gone through that link whichever you mentioned in above post , I meant to say that can we make multiple ranges on single field. e.g I have a requirement like, I have one duration field in facet that ranges from 0-15,16-30,31-45,46-60 and \>61 mins.

I tried range query like as below but it is not giving any result can you please help me where I am doing wrong or how can I achieve this ?

```
GET _search
{
    "query": {"bool" : {
    "filter" : [
      {
        "range" : {
          "callDuration" : {
            "from" : "60",
            "to" : "900",
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "range" : {
          "callDuration" : {
            "from" : "960",
            "to" : "1800",
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "range" : {
          "callDuration" : {
            "from" : "1860",
            "to" : "2700",
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "range" : {
          "callDuration" : {
            "from" : "2760",
            "to" : "3600",
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
          }
        }
      }
   
     ]  
    }
}
}
```

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [December 12, 2016, 2:34pm UTC](https://discuss.elastic.co/t/java-api-5-0-multiple-range-queries/68554/4 "2016-12-12T14:34:36Z")

</div>

In your example you're using a bool query with two filters. This means that you want to filter documents that have a callDuration between 60 and 900 AND 960 and 1800. What you want is all documents with a callDuration that is between 60 and 900 OR 960 and 1800. You can achieve this by changing your `filter` clauses into `should` clauses:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-query)

---

<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 9, 2017, 2:35pm UTC](https://discuss.elastic.co/t/java-api-5-0-multiple-range-queries/68554/5 "2017-01-09T14:35:07Z")

</div>

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