【问题标题】:filtering by date in elasticsearch在弹性搜索中按日期过滤
【发布时间】:2014-03-04 23:49:47
【问题描述】:

我正在尝试搜索在某个范围内具有日期字段的所有项目,但它失败了(不返回任何结果)

查询:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "last_updated": {
            "from": "2013-01-01 00:00:00"
          }
        }
      }
    }
  }
}

映射:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true"
        },
        "properties": {
            "cards": {
                "type": "integer"
            },
            "last_updated": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss"
            }
        }
    }
}

结果:

 {
        took: 1
        timed_out: false
        _shards: {
            total: 1
            successful: 1
            failed: 0
        }
        hits: {
            total: 0
            max_score: null
            hits: [ ]
        }
    }

由具有数值的整数字段(“卡片”)的范围过滤的相同查询可以正常工作。 将日期更改为非常早的开始 (1900-01-01 00:00:00) 也不会显示任何结果。

我做错了什么?

顺便说一句,我知道我在映射中启用了 _timestamp,但这不是我过滤的字段。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    对我来说似乎工作正常:

    curl -XPUT localhost:9200/test -d '{
        "settings": {
            "index.number_of_shards": 1,
            "index.number_of_replicas": 0
        },
        "mappings": {
            "doc": {
                "_timestamp": {
                    "enabled": "true"
                },
                "properties": {
                    "cards": {
                        "type": "integer"
                    },
                    "last_updated": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss"
                    }
                }
            }
        }
    }
    '
    curl -XPOST localhost:9200/test/doc/1 -d '{
        "last_updated": "2012-01-01 12:13:14"
    }
    '
    curl -XPOST localhost:9200/test/doc/2 -d '{
        "last_updated": "2013-02-02 01:02:03"
    }
    '
    curl -X POST 'http://localhost:9200/test/_refresh'
    echo
    curl -X GET 'http://localhost:9200/test/doc/_search?pretty' -d '{
        "query": {
            "filtered": {
                "query": {
                    "match_all": {}
                },
                "filter": {
                    "range": {
                        "last_updated": {
                            "gte": "2013-01-01 00:00:00"
                        }
                    }
                }
            }
        }
    }
    '
    

    【讨论】:

    • 谢谢。这是我的一个错字 - 多么尴尬。如果有人需要查看测试用例,我将把问题留在这里并标记为已接受,以供参考。还是删除它更有意义?
    • @eran 在 google 中第四次匹配“elasticsearch 日期搜索(至少对我而言)。保留它,它对我很有用 =)
    • @Gil 你能说得更具体点吗?您说的是查询的哪一部分?
    • @imotov 根据elasticsearch.org/guide/en/elasticsearch/reference/current/…,范围过滤器需要gtegt等,而不是from
    • 如何比较相等日期?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2020-07-27
    • 2020-07-25
    • 2012-11-07
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多