如果我使用时间戳(例如 1428956853627)在弹性搜索中存储日期,我是否仍可以按天查询该记录,而无需时间值?
If I store a date in elastic search using a timestamp (e.g. 1428956853627), can I still query that record by day, without the time value?
例如,如果我有一个带有日期字段的映射,例如:
$ curl -XGET http://localhost:9200/testing/blog/_mapping
{"testing":{
"mappings":{
"blog":{
"properties":{
"posted":{
"type":"date",
"format":"dateOptionalTime"
},
"title":{
"type":"string"
}
}
}
}
}
}
然后我插入一条记录,如:
$ curl -XPUT http://localhost:9200/testing/blog/1 -d
'{"title": "Elastic search", "posted": 1428956853627}'
{"_index":"testing","_type":"blog","_id":"1","_version":1,"created":true}
使用对应于 2015 年 4 月 13 日星期一 15:27:33 CDT 的时间戳,是否有某种方法可以查询在 "plain old" 日期之前退出?例如,如果我想查看 2015 年 4 月 13 日发布的帖子,我会尝试:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query":{"filtered": {"filter": {"term": {"posted": "2015-04-13"}}}}}'
并且没有命中返回:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
即使我在查询中包含时间戳,我仍然没有取回我的记录:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query": {"filtered": {"filter": {"term": {"posted": "2015-04-13T15:27:33"}}}}}'
我认为至少,由于映射将 "posted" 声明为日期,我可以通过以下方式按范围检索它:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query": {"filtered": {"filter": {"range": {"posted": {"gt": "2014-04-13", "lt": "2014-04-15"}}}}}}'
但即使这样也行不通。我似乎可以按日期查询条目的唯一方法是使用与我最初传入的完全相同的时间戳(长)值:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query": {"filtered": {"filter": {"term": {"posted": 1428956853627}}}}}'
但这并不是那么有用...在将所有日期插入弹性搜索之前,我是否必须将所有日期转换为字符串?理想情况下,我希望能够以更高的保真度保存,包括我需要它们的时间,但仍然搜索 2014-04-13。 elasticsearch 能做到吗?
日期范围查询是最佳选择。
{
"query": {
"filtered": {
"filter": {
"range": {
"posted": {
"gt": "2015-04-13",
"lt": "2015-04-15"
}
}
}
}
}
}
您提供的日期是 2015 年,而不是您在查询中所做的 2014 年。这就是范围查询不起作用的原因。
ES 也接受这种时间格式只是因为它包含在 dateOptionalTime 下指定的格式中。
如果您需要在此处查看可用格式,请检查它。
如果您需要接受其他格式,您需要在映射中提及其他格式。
例如,如果我有一个带有日期字段的映射,例如:
$ curl -XGET http://localhost:9200/testing/blog/_mapping
{"testing":{
"mappings":{
"blog":{
"properties":{
"posted":{
"type":"date",
"format":"dateOptionalTime"
},
"title":{
"type":"string"
}
}
}
}
}
}
然后我插入一条记录,如:
$ curl -XPUT http://localhost:9200/testing/blog/1 -d
'{"title": "Elastic search", "posted": 1428956853627}'
{"_index":"testing","_type":"blog","_id":"1","_version":1,"created":true}
使用对应于 2015 年 4 月 13 日星期一 15:27:33 CDT 的时间戳,是否有某种方法可以查询在 "plain old" 日期之前退出?例如,如果我想查看 2015 年 4 月 13 日发布的帖子,我会尝试:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query":{"filtered": {"filter": {"term": {"posted": "2015-04-13"}}}}}'
并且没有命中返回:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
即使我在查询中包含时间戳,我仍然没有取回我的记录:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query": {"filtered": {"filter": {"term": {"posted": "2015-04-13T15:27:33"}}}}}'
我认为至少,由于映射将 "posted" 声明为日期,我可以通过以下方式按范围检索它:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query": {"filtered": {"filter": {"range": {"posted": {"gt": "2014-04-13", "lt": "2014-04-15"}}}}}}'
但即使这样也行不通。我似乎可以按日期查询条目的唯一方法是使用与我最初传入的完全相同的时间戳(长)值:
$ curl -XGET http://localhost:9200/testing/blog/_search?pretty -d
'{"query": {"filtered": {"filter": {"term": {"posted": 1428956853627}}}}}'
但这并不是那么有用...在将所有日期插入弹性搜索之前,我是否必须将所有日期转换为字符串?理想情况下,我希望能够以更高的保真度保存,包括我需要它们的时间,但仍然搜索 2014-04-13。 elasticsearch 能做到吗?
日期范围查询是最佳选择。
{
"query": {
"filtered": {
"filter": {
"range": {
"posted": {
"gt": "2015-04-13",
"lt": "2015-04-15"
}
}
}
}
}
}
您提供的日期是 2015 年,而不是您在查询中所做的 2014 年。这就是范围查询不起作用的原因。 ES 也接受这种时间格式只是因为它包含在 dateOptionalTime 下指定的格式中。 如果您需要在此处查看可用格式,请检查它。 如果您需要接受其他格式,您需要在映射中提及其他格式。