Java 在elasticsearch查询中设置日期格式(检索期间)
我有一个elasticsearch索引,其中有一个字段“aDate”(以及许多其他字段),映射如下Java 在elasticsearch查询中设置日期格式(检索期间),java,groovy,
elasticsearch,Java,Groovy,
elasticsearch,我有一个elasticsearch索引,其中有一个字段“aDate”(以及许多其他字段),映射如下 "aDate" : { "type" : "date", "format" : "date_optional_time" } 当我查询文档时,会得到如下结果 "aDate" : 1421179734000, 我知道这是epoch,内部java/elasticsearch日期格式,但我希望得到如下结果: "aDate" : "2015-01-13T20:08:54
"aDate" : {
"type" : "date",
"format" : "date_optional_time"
}
当我查询文档时,会得到如下结果
"aDate" : 1421179734000,
我知道这是epoch,内部java/elasticsearch日期格式,但我希望得到如下结果:
"aDate" : "2015-01-13T20:08:54",
我玩脚本
{
"query":{
"match_all":{
}
},
"script_fields":{
"aDate":{
"script":"if (!_source.aDate?.equals('null')) new java.text.SimpleDateFormat('yyyy-MM-dd\\'T\\'HH:mm:ss').format(new java.util.Date(_source.aDate));"
}
}
}
但是它给出了奇怪的结果(脚本基本上可以工作,但是aDate是唯一返回的字段,并且缺少_source)。这看起来像
"hits": [{
"_index": "idx1",
"_type": "type2",
"_id": "8770",
"_score": 1.0,
"fields": {
"aDate": ["2015-01-12T17:15:47"]
}
},
如果可能,我更喜欢不使用脚本的解决方案。在Elasticsearch中运行查询时,您可以请求它返回原始数据,例如指定: 将以您最初存储的格式为您提供日期:
{
"_index" : "myindex",
"_type" : "date-test",
"_id" : "AUrlWNTAk1DYhbTcL2xO",
"_score" : 1.0,
"fields" : {
"aDate" : [ "2015-01-13T20:08:56" ]
}
}, {
"_index" : "myindex",
"_type" : "date-test",
"_id" : "AUrlQnFgk1DYhbTcL2xM",
"_score" : 1.0,
"fields" : {
"aDate" : [ 1421179734000 ]
}
除非使用脚本,否则无法更改日期格式
curl -XGET http://localhost:9200/myindex/date-test/_search?pretty -d '
{
"query":{
"match_all":{ }
},
"script_fields":{
"aDate":{
"script":"use( groovy.time.TimeCategory ) { new Date( doc[\"aDate\"].value ) }"
}
}
}'
将返回:
{
"_index" : "myindex",
"_type" : "date-test",
"_id" : "AUrlWNTAk1DYhbTcL2xO",
"_score" : 1.0,
"fields" : {
"aDate" : [ "2015-01-13T20:08:56.000Z" ]
}
}, {
"_index" : "myindex",
"_type" : "date-test",
"_id" : "AUrlQnFgk1DYhbTcL2xM",
"_score" : 1.0,
"fields" : {
"aDate" : [ "2015-01-13T20:08:54.000Z" ]
}
}
要应用格式,请按如下方式附加格式:
"script":"use( groovy.time.TimeCategory ){ new Date( doc[\"aDate\"].value ).format(\"yyyy-MM-dd\") }"
将返回“aDate”:[“2015-01-13”]
要显示T
,您需要使用引号,但要用等效的Unicode替换它们:
"script":"use( groovy.time.TimeCategory ){ new Date( doc[\"aDate\"].value ).format(\"yyyy-MM-dd\u0027T\u0027HH:mm:ss\") }"
返回“aDate”:[“2015-01-13T20:08:54”]
返回脚本\u字段和源 在查询中使用\u source指定要返回的字段:
curl -XGET http://localhost:9200/myindex/date-test/_search?pretty -d '
{ "_source" : "name",
"query":{
"match_all":{ }
},
"script_fields":{
"aDate":{
"script":"use( groovy.time.TimeCategory ) { new Date( doc[\"aDate\"].value ) }"
}
}
}'
将返回我的名称
字段:
"_source":{"name":"Terry"},
"fields" : {
"aDate" : [ "2015-01-13T20:08:56.000Z" ]
}
使用星号将返回所有字段,例如:“\u source”:“*”,
脚本仅在提取行时计算答案。这是昂贵的,并防止您在Elasticsearch中使用任何与日期相关的搜索功能 您应该在插入之前创建elasticsearch“日期”字段。看起来像java Date()对象。如前所述,answer在elastic 2.2中不再有效。我将脚本更改为:
"script":"new Date(doc['time'].value)"
您可以根据设置日期格式。自5.0.0以来,es使用
无痛
作为脚本语言:
试试这个(在6.3.2中使用)
谢谢@Arcon的建议。我使用您的答案作为指南,从Elasticsearch中的datetime字段中删除了时间元素
{
"aggs": {
"grp_by_date": {
"terms": {
"size": 200,
"script": "doc['TransactionReconciliationsCreated'].value.toString('yyyy-MM-dd')"
}
}
}
}
谢谢由于原始日期格式是epoch,所以我必须使用脚本。我怎样才能得到一个还包括源的结果?(见问题中粗体标记的文本)我已经更新了答案,包括使用_source返回其他字段。太好了,这项工作。如果我使用“_source”:“*”我会得到所有的源字段,这就是我想要使用的(groovy.time.TimeCategory)似乎不适用于ES 2.2。有什么好主意吗,(朗是很棒的)?类05C0662F39D36342A9A0A646DB3AA789DA16C191不在6.x中工作时没有此类属性:groovy。从5.0.0开始,es使用
无痛作为脚本语言
"script":"new Date(doc['time'].value)"
"script":"doc['aDate'].value.toString('yyyy-MM-dd HH:mm:ss')"
{
"aggs": {
"grp_by_date": {
"terms": {
"size": 200,
"script": "doc['TransactionReconciliationsCreated'].value.toString('yyyy-MM-dd')"
}
}
}
}