Rick van Lieshout
Rick van Lieshout

Reputation: 2316

Elasticsearch or term combined with date range

I'm trying to query my index with an OR statement which includes a range, to do this I constructed the following query (with help from the documentation):

{
    "query": {
        "match_all": {}
    },
    "size": 50,
    "filter": {
        "or": [{
            "term": {
                "fromPlace": "liverpool"
            },
            "range" : {
                "query" : {
                    "gte": "01/01/2012",
                    "lte": "2013",
                    "format": "dd/MM/yyyy||yyyy"
                }
            }
        }]
    }
}

This, however, gives me a "query malformed, no field after start_object" error. The complete error message:

{
  "error": {
    "root_cause": [
      {
        "type": "query_parsing_exception",
        "reason": "[_na] query malformed, no field after start_object",
        "index": "quote-lanes2",
        "line": 13,
        "col": 15
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "quote-lanes2",
        "node": "opqibf4zRxOXTHz0QIAaeA",
        "reason": {
          "type": "query_parsing_exception",
          "reason": "[_na] query malformed, no field after start_object",
          "index": "quote-lanes2",
          "line": 13,
          "col": 15
        }
      }
    ]
  },
  "status": 400
}

Any suggestions? Or an elaboration on the documentation? Thanks in advance guys!

EDIT:

So the query was fixed thanks to @or-weinberger, but it doesn't actually filter on the date. I changed "query" to "validFrom" so that my query looks like:

{
  "size": 50,
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "or": [
          {
            "term": {
              "fromPlace": "liverpool"
            }
          },
          {
            "range": {
              "validFrom": {
                "gte": "2017-03-22T08:43:11",
                "format": "strict_date_optional_time||epoch_millis"
              }
            }
          }
        ]
      }
    }
  }
}

The GTE is obviously in the future and shouldn't match a document with the following value (yet it does): enter image description here

My date fields are correctly mapped as date fields: enter image description here

Ideally I'd want to filter only on the "yyyy/mm/dddd" part. Any ideas?

Upvotes: 0

Views: 2706

Answers (1)

Or Weinberger
Or Weinberger

Reputation: 7482

Your query is indeed malformed, the filter should be under a bool element which should be under the query element. Also, when using or/and you should create separate objects for each filter element (range/term etc..) for example:

{
  "size": 50,
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "or": [
          {
            "term": {
              "fromPlace": "liverpool"
            }
          },
          {
            "range": {
              "query": {
                "gte": "01/01/2012",
                "lte": "2013",
                "format": "dd/MM/yyyy||yyyy"
              }
            }
          }
        ]
      }
    }
  }
}

Upvotes: 2

Related Questions