我已经从自动生成的时间戳转到映射的时间戳了,查询不再起作用。使用自动生成的时间戳时,我能够执行这种查询:

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上的时间戳记的数值。有什么更好的希望吗?

最佳答案

您的问题中仍然缺少一些东西。我只能猜测,您可能已经发布了自纪元以来的秒数(而不是毫秒数),因此您的映射未正确应用,因此您还没有透露其他错误。这是根据您迄今为止选择揭示的数据得出的示例,它可以很好地工作。请尝试修改示例以适合您的情况。

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

关于elasticsearch - 根据映射的时间戳进行过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32405663/

10-10 02:35