java—日期之间的平均差值

aamkag61  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(316)

下面是我的elasticsearch数据片段。状态字段已嵌套。

status: [
                {
                  "updated_at": "2020-08-04 17:18:41",
                  "created_at": "2020-08-04 17:18:39",
                  "sub_stage": "Stage1"
                },
                {
                  "updated_at": "2020-08-04 17:21:15",
                  "created_at": "2020-08-04 17:18:41",
                  "sub_stage": "Stage2"
                },
                {
                  "updated_at": "2020-08-04 17:21:15",
                  "created_at": "2020-08-04 17:21:07",
                  "sub_stage": "Stage3"
                }
        ]

在基于某个字段进行聚合之后,我为每个bucket创建了一些文档,每个文档都有状态字段。现在,我想找出 stage1 以及 stage3 .
例如:假设 id = 1 bucket由100个文档组成。然后对于每个文档,我必须找到阶段1和阶段3之间的时间差。然后,最后取平均值。
我能做得很好,但我一直在寻找平均值。
经过努力,我使用了下面的脚本,但不知道它是否正确:

Map findEvent(List events, String type) {
  return events.find(it -> it.sub_stage == type);
}
return ChronoUnit.DAYS.between(Instant.parse(findEvent(params._source.events, 'Stage1').timestamp), Instant.parse(findEvent(params._source.events, 'Stage3').timestamp););

有什么方法可以用这个脚本或其他脚本在java中执行这个操作吗?
大致上,查询如下所示:

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool": {
                "should": [
                  {
                    "match": {
                      "status.sub_stage": {
                        "query": "Stage1",
                        "operator": "OR",
                        "prefix_length": 0,
                        "max_expansions": 50,
                        "fuzzy_transpositions": true,
                        "lenient": false,
                        "zero_terms_query": "NONE",
                        "auto_generate_synonyms_phrase_query": true,
                        "boost": 1.0
                      }
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "minimum_should_match": "1",
                "boost": 1.0
              }
            },
            "path": "status",
            "ignore_unmapped": false,
            "score_mode": "none",
            "boost": 1.0
          }
        }
      ],
      "adjust_pure_negative": true,
      "minimum_should_match": "1",
      "boost": 1.0
    }
  },
  "aggregations": {
    "id": {
      "terms": {
        "field": "id.keyword",
        "size": 1000,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      },
      "aggregations": {
        "avg time": {
          "avg": {
            "script": {
              "source": "Map findStage(List events, String type) { return events.find(it -> it.sub_stage == type); } return ChronoUnit.DAYS.between(Instant.parse(findStage(ctx._source.status, 'Stage1').timestamp), Instant.parse(findStage(ctx._source.status, 'Stage3').timestamp));",
              "lang": "painless"
            }
          }
        }
      }
    }
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题