es查询时间long转date_在Elasticsearch查询中格式化日期(在检索过程中)

小编典典

在Elasticsearch中运行查询时,您可以请求其返回原始数据,例如,指定字段:

curl -XGET http://localhost:9200/myindex/date-test/_search?pretty -d '

{

"fields" : "aDate",

"query":{

"match_all":{

}

}

}'

将以您最初存储日期的格式为您提供日期:

{

"_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" ]

返回script_fields和源

在查询中使用 _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 ) }"

}

}

}'

将返回我的name字段:

"_source":{"name":"Terry"},

"fields" : {

"aDate" : [ "2015-01-13T20:08:56.000Z" ]

}

使用星号将返回所有字段,例如: "_source" : "*",

"_source":{"name":"Terry","aDate":1421179736000},

"fields" : {

"aDate" : [ "2015-01-13T20:08:56.000Z" ]

}

2020-06-22

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值