elasticsearch查询中的格式化日期(检索期间)

sal*_*lyh 10 java groovy elasticsearch

我有一个带有字段"aDate"(以及许多其他字段)的elasticsearch索引,其中包含以下映射

"aDate" : {
        "type" : "date",
        "format" : "date_optional_time"
}
Run Code Online (Sandbox Code Playgroud)

当我查询文档时,我得到一个结果

"aDate" : 1421179734000,
Run Code Online (Sandbox Code Playgroud)

我知道这是epoch,内部java/elasticsearch日期格式,但我希望得到如下结果:

"aDate" : "2015-01-13T20:08:54",
Run Code Online (Sandbox Code Playgroud)

我玩脚本

{  
 "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));"
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

但它给出了奇怪的结果(脚本基本上工作,但aDate是唯一返回的字段,而_source缺失).这看起来像

"hits": [{
        "_index": "idx1",
        "_type": "type2",
        "_id": "8770",
        "_score": 1.0,
        "fields": {
            "aDate": ["2015-01-12T17:15:47"]
        }
    },
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我更喜欢没有脚本的解决方案.

Oll*_*ank 7

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

curl -XGET http://localhost:9200/myindex/date-test/_search?pretty -d '
{
 "fields" : "aDate",
 "query":{  
   "match_all":{  

   }
 }
}'
Run Code Online (Sandbox Code Playgroud)

将以您最初存储它的格式给出日期:

{
  "_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 ]
  }
Run Code Online (Sandbox Code Playgroud)

除非您使用脚本,否则无法更改日期格式.

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 )  }"
   }
 }
}'
Run Code Online (Sandbox Code Playgroud)

将返回:

{
  "_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" ]
  }
}
Run Code Online (Sandbox Code Playgroud)

要应用格式,请按如下方式附加:

"script":"use( groovy.time.TimeCategory ){ new Date( doc[\"aDate\"].value ).format(\"yyyy-MM-dd\")   }"
Run Code Online (Sandbox Code Playgroud)

将返回 "aDate" : [ "2015-01-13" ]

要显示T,您需要使用引号,但用等效的Unicode替换它们:

"script":"use( groovy.time.TimeCategory ){ new Date( doc[\"aDate\"].value ).format(\"yyyy-MM-dd\u0027T\u0027HH:mm:ss\") }"
Run Code Online (Sandbox Code Playgroud)

回报 "aDate" : [ "2015-01-13T20:08:54" ]


返回script_fields和source

在查询中使用_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 )  }"
    }
  }
 }'
Run Code Online (Sandbox Code Playgroud)

将返回我的name领域:

"_source":{"name":"Terry"},
  "fields" : {
    "aDate" : [ "2015-01-13T20:08:56.000Z" ]
  }
Run Code Online (Sandbox Code Playgroud)

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

"_source":{"name":"Terry","aDate":1421179736000},
  "fields" : {
    "aDate" : [ "2015-01-13T20:08:56.000Z" ]
  }
Run Code Online (Sandbox Code Playgroud)


Arc*_*hon 7

从5.0.0开始,es使用Painless作为脚本语言:链接

试试这个(在 6.3.2 中工作)

"script":"doc['aDate'].value.toString('yyyy-MM-dd HH:mm:ss')"
Run Code Online (Sandbox Code Playgroud)