Query in elasticsearch with multiple ranges on multiple dates

Viewed 32497

The following range_query returns a result as expected:

{"query": {
    "bool": {
      "must": [
        {
          "range": {
            "created_at": {
              "gte": "2013-12-09"
            }
          }
        }
      ]
    }
  }
}

But and-ing together several range queries, returns nothing:

{"query": {
    "bool":{
      "must": [
        {
          "and": [
            {
              "range": {
                "created_at": {
                  "gte": "2013-12-09"
                }
              }
            },
            {
              "range": {
                "happens_on": {
                  "lte": "2013-12-16"
                }
              }
            },
            {
              "range": {
                "created_at": {
                  "lte": "2013-12-14"
                }
              }
            }
          ]
        }
      ]
    }
  }
}

What is the correct way to use multiple range_queries on multiple fields?

EDIT: Ah, ok, so this is where I use a range_filter as opposed to range_query? This sounded promising, so I re-wrote my query using only a single range filter. Posting all of it here, in case I'm messing up the query someplace else. I'm performing a GET, and everything inside the source key is actually JSON, but I removed the escaped the hyphens for readability:

{
  "source": {
    "filtered": {
      "filter": {
        "and": [
          {
            "term": {
              "restricted": false
            }
          },
          {
            "not": {
              "term": {
                "deleted": true
              }
            }
          },
          {
            "range": {
              "happens_on": {
                "lte": "2013-12-16"
              }
            }
          }
        ]
      },
      "query": {
        "bool": {
          "must": [
          ]
        }
      }
    },
    "from": 0,
    "size": 10
  }
}

Regrettably, my problem is still the same: I'm not getting any hits.

EDIT2: So, going down the alley of ranges inside a must clause like Njal suggested. This gives me a multi-range query like this:

{
  "source": {
    "filter": {
      "and": [
        {
          "term": {
            "restricted": false
          }
        },
        {
          "not": {
            "term": {
              "deleted": true
            }
          }
        }
      ]
    },
    "from": 0,
    "query": {
      "bool": {
        "must": [
          {
            "range": {
              "happens_on": {
                "gte": "2013-12-06"
              }
            }
          },
          {
            "range": {
              "created_at": {
                "gte": "2013-12-15"
              }
            }
          },
          {
            "range": {
              "happens_on": {
                "lte": "2013-12-17"
              }
            }
          },
          {
            "range": {
              "created_at": {
                "lte": "2013-12-17"
              }
            }
          }
        ]
      }
    },
    "size": 10
  }
}

Still no results returned. Am I doing any obvious mistakes here?

1 Answers
Related