java 弹性搜索查询中的格式日期(检索期间)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27931241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:38:45  来源:igfitidea点击:

Format date in elasticsearch query (during retrieval)

javagroovyelasticsearch

提问by salyh

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

我有一个带有以下映射的字段“aDate”(以及许多其他字段)的弹性搜索索引

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

When i query for a document i get a result like

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

"aDate" : 1421179734000,

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

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

"aDate" : "2015-01-13T20:08:54",

I play around with scripting

我玩脚本

{  
 "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));"
   }
 }
}

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

但它给出了奇怪的结果(脚本基本上可以工作,但 aDate 是唯一返回的字段并且 _source 丢失)。这看起来像

"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.

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

采纳答案by Olly Cruickshank

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

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

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 )  }"
   }
 }
}'

Will return:

将返回:

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

will return "aDate" : [ "2015-01-13" ]

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

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

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

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

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

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



To return script_fields and source

返回 script_fields 和源

Use _sourcein your query to specify the fields you want to return:

在查询中使用_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 )  }"
    }
  }
 }'

Will return my namefield:

将返回我的name领域:

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

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

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

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

回答by TheTufik

As LabOctoCatmentioned, Olly Cruickshankanswer no longer works in elastic 2.2. I changed the script to:

正如LabOctoCat 所提到的,Olly Cruickshank 的回答不再适用于 elastic 2.2。我将脚本更改为:

"script":"new Date(doc['time'].value)"

You can format the date according to this.

您可以根据格式化日期。

回答by Archon

Since 5.0.0, es use Painlessas script language: link

从 5.0.0 开始,esPainless用作脚本语言:链接

Try this (work in 6.3.2)

试试这个(在 6.3.2 中工作)

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

回答by Kwex

Thanks @Archon for your suggestion. I used your answer as a guide to remove the time element from a datetime field in Elasticsearch

感谢@Archon 的建议。我使用您的回答作为从 Elasticsearch 中的日期时间字段中删除时间元素的指南

{
    "aggs": {
        "grp_by_date": {
            "terms": {
                "size": 200,
                "script": "doc['TransactionReconciliationsCreated'].value.toString('yyyy-MM-dd')"
            }
        }
    }
}

回答by Alain Collins

Scripting it only computes the answer when the row is extracted. This is expensive, and keeps you from using any date-related search functions in Elasticsearch.

编写它只在提取行时计算答案。这很昂贵,并且使您无法在 Elasticsearch 中使用任何与日期相关的搜索功能。

You should create an elasticsearch "date" field before inserting it. Looks like a java Date() object will do.

您应该在插入之前创建一个 elasticsearch“日期”字段。看起来像一个 java Date() 对象会做