使用tokenizer在弹性搜索中对文档进行分组

时间:2019-12-23 14:34:32

标签: elasticsearch

我对ES没有很深的了解,但是我的老板给了我汇总/分组错误消息的任务,因此我们可以看到错误消息出现的频率。

文档看起来像

{
  "_index": "fluentd.php.error.20191223",
  "_type": "_doc",
  "_id": "0zYPM28B9xbIWtpXrXSt",
  "_version": 1,
  "_score": null,
  "_source": {
    "message": "WARNING Invalid argument supplied for foreach() /var/www/class/bootstrap.php:9 /product-1",
    "hostname": "webserver-1",
    "@timestamp": "2019-12-23T14:19:04+0100",
    "@log_name": "php.error"
  },
  "fields": {
    "@timestamp": [
      "2019-12-23T14:19:04.000Z"
    ]
  },
  "sort": [
    1577109544000
  ]
}
{
  "_index": "fluentd.php.error.20191223",
  "_type": "_doc",
  "_id": "0zYPM28B9xbIWtpXrXSt",
  "_version": 1,
  "_score": null,
  "_source": {
    "message": "WARNING Invalid argument supplied for foreach() /var/www/class/bootstrap.php:9 /newsletter.html",
    "hostname": "webserver-1",
    "@timestamp": "2019-12-23T14:19:04+0100",
    "@log_name": "php.error"
  },
  "fields": {
    "@timestamp": [
      "2019-12-23T14:19:04.000Z"
    ]
  },
  "sort": [
    1577109544000
  ]
}

罪魁祸首是,两个消息均不同,但都关注同一文件中的相同错误(在不同的url下)。

ES甚至有可能检测到这种语义吗?消息可以在许多方面有所不同。.

1 个答案:

答案 0 :(得分:0)

这是一个有趣的问题,我认为对此没有直接的答案。我能想到的一种方法是使用Script Terms Aggregation

GET /_search
{
  "aggs": {
    "group": {
      "terms": {
        "script": """
        int endIndex = doc['message.keyword'].value.indexOf("/");
        return doc['message.keyword'].value.substring(0,endIndex);"""
      }
    }
  }
}

以上给出的内容如下:

{
..
"aggregations" : {
    "group" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "WARNING Invalid argument supplied for foreach() ",
          "doc_count" : 2
        }
      ]
    }
  }
..
}

您可以根据脚本的要求使用脚本并根据数据分析对数据进行处理,并获取错误类型等。