日期范围查询不适用于Elastic 2.4群集

时间:2020-03-08 04:33:07

标签: elasticsearch date-range elasticsearch-query

带有日期范围查询的Elasticsearch查询无法正常工作。

FROM

2 个答案:

答案 0 :(得分:0)

您使用了错误的日期格式,而使用yyyy-mm-dd时正确的日期格式是dd-mm-yyyy。有关更多信息,请参考date data typedate range query ES文档。

正确的示例

索引映射

{
    "mappings": {
        "my_type": {
            "properties": {
                "sales_date": {
                    "type": "date"
                }
            }
        }
    }
}

索引样本文档

{
   "sales_date" :  "01-02-2020"
}

{
   "sales_date" :  "2020-02-05"
}

{
   "sales_date" :  "2020-03-08"
}
{
   "sales_date" :  "2020-03-15"
}

使用date range query

进行搜索查询
{
    "query": {
        "range" : {
            "sales_date" : {
                "gte" : "2020-02-01", --> NOTE DIFFERENCE
                "lte" : "2020-03-01"
            }
        }
    }
}

搜索结果

"hits": [
         {
            "_index": "so-60584496",
            "_type": "my_type",
            "_id": "1",
            "_score": 1.0,
            "_source": {
               "sales_date": "2020-02-01"
            }
         },
         {
            "_index": "so-60584496",
            "_type": "my_type",
            "_id": "2",
            "_score": 1.0,
            "_source": {
               "sales_date": "2020-02-05"
            }
         }
      ]

答案 1 :(得分:0)

对我来说,范围查询适用于具有映射的非嵌套日期:

  "mappings": {
            "_default_": {
                "properties": {
                    "date": {
                        "type": "date",
                        "format": "MM-dd-yyyy"
                            }
                      }
            }}

工作查询:

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "100",
            "fields": [
              "nodeId"
            ],
            "default_operator": "and"
          }
        },
        {
          "query_string": {
            "query": "DAILY",
            "fields": [
              "aggLevel"
            ],
            "default_operator": "and"
          }
        },
        {
          "query_string": {
            "query": "23",
            "fields": [
              "replId"
            ],
            "default_operator": "and"
          }
        },
        {
          "range": {
            "date": {
              "gte": "01-02-2020",
              "lte": "08-03-2020"
            }
          }
        }
      ]
    }
  }
}