如果我使用时间戳(例如1428956853627)在弹性搜索中存储日期,那么在没有时间值的情况下,我还能按天查询该记录吗



例如,如果我有一个带有日期字段的映射,如:

$ 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的时间戳,是否有某种方法可以查询到"普通旧"日期?例如,如果我想看到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"}}}}}'

我想,至少,由于地图声明"发布"为日期,我可以通过按范围检索它

$ 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。弹性搜索能做到吗?

日期范围查询是使用的方法。

{
"query": {
"filtered": {
"filter": {
"range": {
"posted": {
"gt": "2015-04-13",
"lt": "2015-04-15"
}
}
}
}
}
}

您提供的日期是2015年,而不是您在查询中所做的2014年。这就是范围查询不起作用的原因。此外,ES之所以接受此时间格式,只是因为它包含在dateOptionalTime下指定的格式中。如果您需要在这里查看可用的格式,请检查它。如果需要接受其他格式,则需要在映射中提及其他格式。

相关内容

最新更新