elasticsearch - 聚合 "LastUpdated"属性或 _timestamp

标签 elasticsearch

我的 Elasticsearch 映射大致如下所示:

{
  "myIndex": {
    "mappings": {
      "myType": {
        "_timestamp": {
          "enabled": true,
          "store": true
        },
        "properties": {
          "LastUpdated": {
            "type": "date",
            "format": "dateOptionalTime"
          }
         /*  lots of other properties */
        }
      }
    }
  }
}

所以,_timestamp已启用,并且每个文档上还有一个 LastUpated 属性。 LastUpdated 可以具有与 _timestamp 不同的值:有时,文档会得到物理更新(例如对非规范化数据的更新),这会更新 _timestamp,但 LastUpdated 保持不变,因为从业务角度来看,文档实际上并未“更新”。

此外,还有许多文档没有 LastUpdated 值(主要是旧数据)。

我想做的是运行一个聚合来计算每个日历日的文档数量(请忽略日期需要与午夜对齐的事实)。对于每个文档,如果存在则使用 LastUpdated,否则使用 _timestamp。

这是我尝试过的:
{
  "aggregations": {
    "counts": {
      "terms": {
        "script": "doc.LastUpdated == empty ? doc._timestamp : doc.LastUpdated"
      }
    }
  }
}

分桶化似乎在某种程度上起作用,但结果中的键看起来很奇怪:
buckets: [
  {
    key: org.elasticsearch.index.fielddata.ScriptDocValues$Longs@7ba1f463doc_count: 300544
  }{
    key: org.elasticsearch.index.fielddata.ScriptDocValues$Longs@5a298acbdoc_count: 257222
  }{
    key: org.elasticsearch.index.fielddata.ScriptDocValues$Longs@6e451b5edoc_count: 101117
  },
  ...
]

运行此聚合并在结果中获取有意义的键(即时间戳)的正确方法是什么?

最佳答案

我已经为你测试并制作了一个时髦的脚本,

POST index/type/_search
{
   "aggs": {
      "counts": {
         "terms": {
            "script": "ts=doc['_timestamp'].getValue();v=doc['LastUpdated'].getValue();rv=v?:ts;rv",
            "lang": "groovy"
         }
      }
   }
}

这将返回所需的结果。

希望这可以帮助!!谢谢!!

关于elasticsearch - 聚合 "LastUpdated"属性或 _timestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296724/

相关文章:

ElasticSearch:获取 bucket scripted_metric 中的 bucket key

java - JEST 批量请求问题

elasticsearch - 弹性NEST反序列化错误的字段

elasticsearch - ElasticSearch搜索查询中的多个条件

elasticsearch - 从搜索结果中删除重复项

search - 从Elasticsearch中的精确单词匹配开始?

elasticsearch - ElasticSearch 快照/恢复功能是否会导致在恢复期间再次分析数据?

json - Elasticsearch 聚合增强了对嵌套查询的过滤

java.lang.ClassNotFoundException : org. elasticsearch.http.AbstractHttpServerTransport

elasticsearch - Elasticsearch 查询中的精确匹配