# Search two ranges at the same time

**URL:** <https://discuss.elastic.co/t/search-two-ranges-at-the-same-time/213104>\
**Category:** Elasticsearch\
**Created:** [December 26, 2019, 6:57pm UTC](https://discuss.elastic.co/t/search-two-ranges-at-the-same-time/213104 "2019-12-26T18:57:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChaosMTA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chaosmta/32/23372_2.png) [@ChaosMTA](https://discuss.elastic.co/u/ChaosMTA)\
**Post date:** [December 26, 2019, 6:57pm UTC](https://discuss.elastic.co/t/search-two-ranges-at-the-same-time/213104/1 "2019-12-26T18:57:11Z")

</div>

Hey guys,

I just have a question about a nested query I’m trying to do. So I have a index with a nested object. In that nested object I have two range types a date range and a double range.

I want to create a query where “Everything between these dates AND this double range”.  
Here’s a query that works for one at a time:

```
GET partition/_search
{
   "query": {
      "nested": {
        "path": "specs",
        "query": {
          "bool": {
            "must": [
              { “term": 
                {"specs.key": "timestamp"} 
              },
              { "range":
                 {"specs.date_range": {
                     "gte":"1970-01-29T00:41:00"}
                 }      
              }
            ]
          }
        }
      }
   }
}

```

I’m just trying to extend this to work with the double range. I seem to only be able to get one to work at a time otherwise nothing is returned. Here is my attempt of searching both ranges.

```
GET partition/_search
{
    "query": {
        "nested": {
            "path": "specs",
            "query": {
                "bool": {
                    "must": [
                        {"term":
                             {"specs.key":"timestamp"}
                         },
                        { "range":
                            {"specs.date_range": {
                                "gte":"1970-01-29T00:41:00"}
                            }
                        },
                        {
                          "term":{"specs.key”:"cost"}
                        },
                        {
                          "range": {"specs.cost_range": {
                            "gte": 10,
                            "lte": 20
                          }
                        }
                        }
                    ]}
            }
        }
    }
}

```

Here’s an example entry that SHOULD match that query I made:

```
  {
    "_index" : "partition",
    "_type" : "_doc",
    "_id" : "0",
    "_score" : 1.0,
    "_source" : {
      "specs" : [
        {
          "key" : "timestamp",
          "date_range" : {
            "gte" : 2487372217,
            "lte" : 2546383872
          }
        },
        {
          "key" : “cost",
          “cost_range" : {
            "gte" : 0,
            "lte" : 0
          }
        }
      ]
    }
  }

```

Thanks for any help you’re able to provide!

---

<div class="post-metadata">

**Author:** ![ChaosMTA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chaosmta/32/23372_2.png) [@ChaosMTA](https://discuss.elastic.co/u/ChaosMTA)\
**Post date:** [December 26, 2019, 7:03pm UTC](https://discuss.elastic.co/t/search-two-ranges-at-the-same-time/213104/2 "2019-12-26T19:03:38Z")

</div>

I believe this isn’t working because not all the objects in `specs` contains a cost\_range and a date\_range. In english it’s more of:

In the `specs` array, return all results which contain an object with `key==“timestamp”` in the date\_range and `key==“cost”` in the cost\_range

---

<div class="post-metadata">

**Author:** ![ChaosMTA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chaosmta/32/23372_2.png) [@ChaosMTA](https://discuss.elastic.co/u/ChaosMTA)\
**Post date:** [December 27, 2019, 4:22am UTC](https://discuss.elastic.co/t/search-two-ranges-at-the-same-time/213104/3 "2019-12-27T04:22:15Z")

</div>

I was able to solve this. Just in case anyone runs into a similar problem. What you have to do is run separate nested queries inside the bool query.

Here’s an example:

```
{
    "query":{
        "bool":{
            "must":[
                {
                    "nested":{
                        "path":"specs",
                        "query":{
                            "bool":{
                                "must":[
                                    {
                                        "term":{
                                            "specs.key”:"cost"
                                        }
                                    },
                                    {
                                        "range":{
                                            "specs.cost_range":{
                                                "gte":10
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "nested":{
                        "path":"specs",
                        "query":{
                            "bool":{
                                "must":[
                                    {
                                        "term":{
                                            "specs.key":"timestamp"
                                        }
                                    },
                                    {
                                        "range":{
                                            "specs.date_range":{
                                                "gte":0
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}
```

---

<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 24, 2020, 4:22am UTC](https://discuss.elastic.co/t/search-two-ranges-at-the-same-time/213104/4 "2020-01-24T04:22:19Z")

</div>

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