elasticsearch - 无痛脚本,以嵌套结构检索最新日期

标签 elasticsearch elasticsearch-painless

我正在尝试修改此painless script,以便可以从嵌套结构中检索MAX日期。

假装我有这样的映射:

   {
      "mappings": {
         "hello": { 
          "properties": {
             "field1": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"},
             "field2": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"}, 
             "test": { "type": "nested", 
                 "properties": {
                     "field3": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"},
                     "field4": {"type": "integer"},
                     "test2": { "type": "nested", 
                         "properties": {
                         "field5": {"type" : "date", "format" : "yyyy-MM-dd HH:mm:ss"}
                   }
                 }  
               }
             }
          }
       }
    }
}

我要做的是从test.field3检索MAX日期

我已经尝试过一些方法,例如:
Date maxDate
for (int i = 0; i < params._source['test'].size(); i++) { 
    if (params._source['test'][i].field3 > score) {
        maxDate= params._source['test'][i].field3
    }
} return maxDate

任何帮助将大有帮助!我感到自己遇到了日期解析问题,但也许我错了。

如果有人正在玩游戏,这是一个文档:
{
    "field1" : "2018-01-02 10:00:00",
    "field2" : "2018-01-04 10:00:00", 
    "test": [ 
           {
           "field3": "2018-01-04 10:00:00",
           "field6": 1,
           "test2": [{
           "field4": "2018-01-04 10:00:00"
           }
           ]
       }
    ]
}

最佳答案

我会用Java 8 Streams这样做

 params._source.test.stream()
     .mapToLong(f -> new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').parse(f.field3).getTime())
     .max()
     .getAsLong()

关于elasticsearch - 无痛脚本,以嵌套结构检索最新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49890824/

相关文章:

macos - 将 ElasticSearch 集群变为绿色(OS X 上的集群设置)

elasticsearch - 如何在Kibana可视化中隐藏折叠菜单默认设置

elasticsearch - 如何使用jdbc River向elasticsearch添加多种对象类型?

elasticsearch - ElasticSearch多重匹配和得分

date - 如何在 Elasticsearch 中对嵌套日期和非嵌套日期进行日期运算?

elasticsearch - Elasticsearch无痛脚本按值获取嵌套字段?

elasticsearch - 当存储桶路径之一解析为 'bucket_script'时,ElasticSearch 'null'不执行

elasticsearch - ElasticSearch bool 查询到Solrj查询

elasticsearch - Elastic Search 如何使用 painless 删除对象属性

elasticsearch - 基于可用性的Elasticsearch无痛脚本搜索文档