在elasticsearch中按日期过滤

时间:2013-03-26 17:16:38

标签: elasticsearch

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

查询:

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

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

我做错了什么?

BTW,我知道我在映射中启用了_timestamp,但这不是我过滤的字段。

1 个答案:

答案 0 :(得分:30)

似乎对我很好:

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