elasticsearch 如何在多个聚合项目中使用Kibana记录

m0rkklqb  于 7个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(48)

我们正在对我们的应用程序日志文件运行报告,这些日志文件收集在ElasticSearch存储中。

@timestamp:
    Oct 24, 2023 @ 20:15:52.043
Action:
    INSTALL
Components:
    a; b
UserName:
    U12345

字符串
最后,我需要运行一个查询,它将按组件分组,并返回安装计数和完成这些操作的唯一用户。
我可以在查询中按原始Components字段分组,结果如下:

{
   "key" : "a;b",
   "doc_count" : 10,
   "unique_user_ids" : {
        "value" : 2
   }
},
{
   "key" : "a;c",
   "doc_count" : 8,
   "unique_user_ids" : {
        "value" : 1
   }
},


然而,我不知道如何计算每个记录的次数,因为一个组件被列出,得到的结果如下

{
   "key" : "a",
   "doc_count" : 18,
   "unique_user_ids" : {
        "value" : 3
   }
},
{
   "key" : "b",
   "doc_count" : 10,
   "unique_user_ids" : {
        "value" : 2
   }
},
{
   "key" : "c",
   "doc_count" : 8,
   "unique_user_ids" : {
        "value" : 1
   }
},

0qx6xfy6

0qx6xfy61#

如果你可以控制你要进入的数据,最好的方法是在索引它之前拆分键。换句话说,你的记录应该变成:

{
   "key" : ["a", "b"],
   "user_name" : "foo"
},
{
   "key" : ["a", "c"],
   "user_name" : "bar"
},

字符串
一个不需要重新索引数据的慢得多的解决方案是使用运行时字段来拆分组件。运行时字段可以在查询或in the index mapping中定义。下面是如何在查询中执行的示例:

DELETE test
PUT test
{
  "mappings": {
    "properties": {
      "key": {
        "type": "keyword"
      },
      "user_name": {
        "type": "keyword"
      }
    }
  }
}

PUT test/_doc/1
{
   "key" : "a;b",
   "user_name" : "foo"
}

PUT test/_doc/2?refresh
{
   "key" : "a;c",
   "user_name" : "bar"
}

GET test/_search
{
  "size": 0,
  "runtime_mappings": {
    "keys": {
      "type": "keyword",
      "script": """
        def combined_key = /;/.split(doc['key'].value);
        for (key in combined_key) {
          emit(key)
        }
      """
    }
  },
  "aggs": {
    "genres": {
      "terms": {
        "field": "keys"
      }
    }
  }
}

相关问题