java - 在 Elasticsearch 查询中格式化日期(检索期间)

标签 java groovy elasticsearch

我有一个 elasticsearch 索引,其中的字段“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”]


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

关于java - 在 Elasticsearch 查询中格式化日期(检索期间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27931241/

相关文章:

java - 如何保证gradle中的TestNG将JDK添加到IntelliJ的类路径中?

eclipse - 用于GantBuilder的Grails 1.4.0.M1(带有STS 2.7.0.M2)NoClassDefFoundError

python - 索引字典列表时出现“仅在某些xcontent字节或压缩的xcontent字节上调用压缩机检测”错误

elasticsearch - 聚合数据中的Elasticsearch过滤器

java - 使用递归反转字符串

java - 正则表达式过滤具有多个&符号 "&"的URL

java - 在STS中使用带有spring boot的jaxb2 maven插件时收到错误

elasticsearch - 用Elasticsearch进行Groovy编程

eclipse - Controller 中的 GGTS 重复方法名称/签名错误

python - 将自定义字段添加到 logstash