[英]Elasticsearch range query date format for java not returning all the results
[英]Format date in elasticsearch query (during retrieval)
我有一個帶有以下映射的字段“aDate”(以及許多其他字段)的彈性搜索索引
"aDate" : {
"type" : "date",
"format" : "date_optional_time"
}
當我查詢文檔時,我得到的結果是
"aDate" : 1421179734000,
我知道這是時代,內部 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 中運行查詢時,您可以請求它返回原始數據,例如指定fields :
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" ]
在查詢中使用_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" : "*",
"_source":{"name":"Terry","aDate":1421179736000},
"fields" : {
"aDate" : [ "2015-01-13T20:08:56.000Z" ]
}
正如LabOctoCat 所提到的, Olly Cruickshank 的回答不再適用於 elastic 2.2。 我將腳本更改為:
"script":"new Date(doc['time'].value)"
您可以根據此格式化日期。
從 5.0.0 開始,es 使用Painless
作為腳本語言: 鏈接
試試這個(在 6.3.2 中工作)
"script":"doc['aDate'].value.toString('yyyy-MM-dd HH:mm:ss')"
編寫它只在提取行時計算答案。 這很昂貴,並且使您無法在 Elasticsearch 中使用任何與日期相關的搜索功能。
您應該在插入之前創建一個elasticsearch“日期”字段。 看起來像一個 java Date() 對象會做。
感謝@Archon 的建議。 我使用您的答案作為指南從 Elasticsearch 的日期時間字段中刪除時間元素
{
"aggs": {
"grp_by_date": {
"terms": {
"size": 200,
"script": "doc['TransactionReconciliationsCreated'].value.toString('yyyy-MM-dd')"
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.