【问题标题】:Elasticsearch date range query ignores month fieldElasticsearch 日期范围查询忽略月份字段
【发布时间】:2014-05-02 06:43:32
【问题描述】:

我一直使用 Elasticsearch 作为我的数据存储,其中有很多文档。我的问题是,我发现 Elasticsearch 忽略了映射日期字段的月份部分。

这是我的索引和查询中的内容,如果我错了,请告诉我:

curl -XPUT 'http://localhost:9200/tt6/' -d '{}'
curl -XPUT 'http://localhost:9200/tt6/tweet/_mapping' -d '{"tweet" : {"properties" : {"date" : {"type" : "date", "format": "YYYY-MM-DD HH:mm:ss" }}}}'
curl -XPUT 'http://localhost:9200/tt6/tweet/1' -d '{"date": "2014-02-14 04:00:45"}'

curl -XGET 'http://localhost:9200/tt6/_search' -d '
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tweet.date": {
              "from": "2014-12-01 00:00:00",
              "to": "2014-12-30 00:00:00"
            }
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "facets": {}
}'

我的回答是

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "tt6",
        "_type": "tweet",
        "_id": "1",
        "_score": 1,
        "_source": {
          "date": "2014-02-14 04:00:45",
          "name": "test"
        }
      }
    ]
  }
}

对于给定的日期范围,在 2014 年 12 月 1 日和 2014 年 12 月 30 日之间应该没有响应,但 Elasticsearch 会返回范围之外的条目。为什么会这样?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您的日期格式不正确。请将您的映射调用更改为使用yyyy-MM-dd HH:mm:ss,您将获得预期的结果。

    curl -XPUT 'http://localhost:9200/tt6/tweet/_mapping' -d '{"tweet" : {"properties" : {"date" : {"type" : "date", "format": "yyyy-MM-dd HH:mm:ss" }}}}'

    有关正确格式值的更多详细信息,请参阅Elasticsearch Date Range 参考资料(特别是日期/格式模式部分)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      相关资源
      最近更新 更多