栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Elasticsearch查询中格式化日期(在检索过程中)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在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
,您需要使用引号,但将其替换为等效的Unipre:

"script":"use( groovy.time.TimeCategory ){ new Date( doc["aDate"].value ).format("yyyy-MM-ddu0027Tu0027HH: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" ]  }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399488.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号