Elasticsearch或术语与日期范围相结合

时间:2016-10-10 08:41:13

标签: date elasticsearch range

我试图用包含范围的OR语句查询我的索引,为此我构建了以下查询(在文档的帮助下):

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

然而,这给了我一个"查询格式错误,在start_object之后没有字段"错误。完整的错误消息:

{
  "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
}

有什么建议吗?或者详细说明文档? 先谢谢你们!

编辑:

由于@or-weinberger,查询已得到修复,但实际上并未对日期进行过滤。我改变了#34;查询" to" validFrom"所以我的查询看起来像:

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

GTE显然是未来的,不应该与具有以下价值的文档相匹配(但它确实如此): enter image description here

我的日期字段已正确映射为日期字段: enter image description here

理想情况下,我只想过滤" yyyy / mm / dddd"部分。 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的查询确实格式不正确,过滤器应位于bool元素下,该元素应位于query元素下。此外,使用or / and时,您应为每个过滤器元素(范围/术语等)创建单独的对象,例如:

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