如何在 Elasticsearch 中实现这个 sql 查询结果

How to achive this sql query result in Elastic Search

我有一个错误索引,其中包含 php 应用程序记录的所有错误。现在我想要一个 DSL 查询 returns 通过消息和计数来区分不同的错误。

类似于mysql查询:

SELECT *, COUNT(*) AS total FROM 错误GROUP BY消息;

我的索引映射:

"mappings": {
  "errors": {
    "properties": {
      "message": {
        "type": "keyword"
      },
      "trace": {
        "type": "keyword"
      },
      "file": {
        "type": "keyword"
      }
    }
  }
}

预期结果:

Message | File | Count

Undefined variable $param at line 20 in index.php | project/index.php | 10

Undefined variable $opt at line 15 in helper.php | project/helper.php | 4

..........

我正在使用弹性搜索 5.6。提前致谢。

top hits 我认为聚合可以帮助您。

GET errors/_search?size=0
{
  "aggs": {
    "error-counts": {
      "terms": {
        "field": "message"
      },
      "aggs": {
        "messages": {
          "top_hits": {
            "size": 100
          }
        }
      }
    }
  }
}

这将为每条消息创建存储桶,每个存储桶中有一个总计数,每个存储桶中有一个包含 100 条记录的列表。