弹性搜索查询中的格式日期(检索期间) [英] Format date in elasticsearch query (during retrieval)

查看:12
本文介绍了弹性搜索查询中的格式日期(检索期间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a elasticsearch index with a field "aDate" (and lot of other fields) with the following mapping

"aDate" : {
        "type" : "date",
        "format" : "date_optional_time"
}

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

When i query for a document i get a result like

"aDate" : 1421179734000,

我知道这是时代,内部 java/elasticsearch 日期格式,但我想要一个结果:

I know this is the epoch, the internal java/elasticsearch date format, but i want to have a result like:

"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 ).这看起来像

but it give strange results (script works basically, but aDate is the only field returned and _source is missing). This looks like

"hits": [{
        "_index": "idx1",
        "_type": "type2",
        "_id": "8770",
        "_score": 1.0,
        "fields": {
            "aDate": ["2015-01-12T17:15:47"]
        }
    },

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

I would prefer a solution without scripting if possible.

推荐答案

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

When you run a query in Elasticsearch you can request it to return the raw data, for example specifying fields:

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

   }
 }
}'

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

Will give you the date in the format that you originally stored it:

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

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

It's not possible to change the date format unless you use a script.

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

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

To apply a format, append it as follows:

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

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

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

To display the T, you'll need to use quotes but replace them with the Unicode equivalent:

"script":"use( groovy.time.TimeCategory ){ new Date( doc["aDate"].value ).format("yyyy-MM-ddu0027Tu0027HH:mm:ss") }"

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

在查询中使用 _source 指定要返回的字段:

Use _source in your query to specify the fields you want to return:

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" : "*",

Using asterisk will return all fields, e.g.: "_source" : "*",

"_source":{"name":"Terry","aDate":1421179736000},
  "fields" : {
    "aDate" : [ "2015-01-13T20:08:56.000Z" ]
  }

这篇关于弹性搜索查询中的格式日期(检索期间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆