【问题标题】:How to achive this sql query result in Elastic Search如何在 Elasticsearch 中实现这个 sql 查询结果
【发布时间】:2019-08-29 00:08:04
【问题描述】:

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

类似于mysql查询:

SELECT *, COUNT(*) AS total FROM errorsGROUP BYmessage;

为我的索引映射:

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

预期结果:

留言 |文件 |数

index.php 中第 20 行的未定义变量 $param |项目/index.php | 10

helper.php 中第 15 行的未定义变量 $opt |项目/helper.php | 4

.......

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

【问题讨论】:

  • 我期待您的查询是这样的 => SELECT message, COUNT(*) AS total FROM errors GROUP BY message。我想你正在寻找这个 -> elastic.co/guide/en/elasticsearch/reference/5.6/…
  • @Kaushik 我只知道按消息分组的解决方案并获得计数。这是简单的按消息分组。我希望 * 数据也在选择中。而已。谢谢。类似于 mysql ( select *,count() from ... group by message )

标签: elasticsearch elasticsearch-dsl


【解决方案1】:

top hits 聚合可以让你到达那里,我想。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多