过滤映射的时间戳

时间:2015-09-04 19:56:27

标签: elasticsearch

我一直在从自动生成的时间戳移动到映射的时间戳,并且查询不再起作用。使用自动生成的时间戳时,我能够执行此类查询:

FilterBuilder filter = FilterBuilders.rangeFilter("_timestamp").from(zonedDateTime.toLocalDate().toString()).to(zonedDateTime.plusHours(1).toLocalDate().toString());

但是传递日期表达式如“now-1h”也可以。现在我已经介绍了以下映射:

"collections": {
  "_timestamp": {
    "enabled": true,
    "path": "my_date",
    "ignore_missing": false
  },
  "properties": {
    "author": {
      "type": "string",
      "index": "not_analyzed"
    },
    "name": {
      "type": "string",
      "analyzer": "lowercase"
    },
    "my_date": {
      "type": "date"
    }
  }
}

我将my_date存储为Unix EPOCH格式,我不能再查询了:

FilterBuilders.rangeFilter("_timestamp").from(zonedDateTime.toLocalDate().toString()).to(zonedDateTime.plusHours(1).toLocalDate().toString());

在查询时返回空结果

FilterBuilders.rangeFilter("my_date").from(zonedDateTime.toLocalDate().toString()).to(zonedDateTime.plusHours(1).toLocalDate().toString());

失败并出现异常

from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"my_date":{"from":"2015-09-04T19:52:15.001+01:00","to":"2015-09-04T21:52:15.001+01:00","include_lower":true,"include_upper":true}}}}}}]]]; nested: NumberFormatException[For input string: "2015-09-04T19:52:15.001+01:00"];

我可以做的唯一查询就是在my_date上使用时间戳的数值。有什么比我更好的希望吗?

1 个答案:

答案 0 :(得分:2)

您的问题中仍然缺少某些内容。我只能猜测你可能已经发布了的数量,因为纪元而不是毫秒数,你的映射没有正确应用,所以出现了其他问题#39; t尚未披露。这是基于您到目前为止所选择显示的数据的示例,它完美地运行。请尝试修改示例以符合您的情况。

curl -XDELETE localhost:9200/test
curl -XPUT localhost:9200/test -d '{
    "mappings": {
        "collections": {
            "_timestamp": {
                "enabled": true,
                "path": "my_date",
                "ignore_missing": false
            },
            "properties": {
                "my_date": {
                    "type": "date"
                }
            }
          }
        }
    }
}'
curl -XGET "localhost:9200/test/_mapping?pretty"
curl -XPOST "localhost:9200/test/collections?refresh" -d '{"my_date": "1441421651000"}'
curl -XGET "localhost:9200/test/collections/_search?pretty" -d '{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "range": {
                    "my_date": {
                        "from": "2015-09-04T19:52:15.001+01:00",
                        "to": "2015-09-05T21:52:15.001+01:00",
                        "include_lower": true,
                        "include_upper": true
                    }
                }
            }
        }
    }
}'