Search two ranges at the same time

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!

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

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
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

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