【问题标题】:Using filters to count values in Kibana / Visualize?使用过滤器计算 Kibana / Visualize 中的值?
【发布时间】:2017-06-16 01:06:14
【问题描述】:

(我对 ELK 堆栈还很陌生,可能会问一些明显的问题......)

我有描述客户信息的文件,包括姓名、地址、年龄等数据... 有时,并非所有这些字段都存在,我想知道填写了这些字段的文档数量。

如果数据看起来像:

PUT customers
{
  "mappings": {
    "customer": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "category": {
          "type": "keyword"
        },
        "email": {
          "type": "text"
        },
        "age": {
          "type": "integer"
        },
        "address": {
          "type": "text"
        }
      }
    }
  }
}


POST _bulk
{"index":{"_index":"customers","_type":"customer"}}
{"id":"1","category":"aa","email":"sam@test.com"}
{"index":{"_index":"customers","_type":"customer"}}
{"id": "2", "category" : "aa", "age": "5"}
{"index":{"_index":"customers","_type":"customer"}}
{"id": "3", "category" : "aa", "email": "bob@test.com", "age": "36"}
{"index":{"_index":"customers","_type":"customer"}}
{"id": "4", "category" : "bb", "email": "kim@test.com", "age": "42", "address": "london"}

我们的想法是在 Kibana 中可视化如下数据表:

+----------+-------+-------+-----+---------+
| category | total | email | age | address |
+----------+-------+-------+-----+---------+
| aa       |     3 |     2 |   2 |       0 |
| bb       |     1 |     1 |   1 |       1 |
+----------+-------+-------+-----+---------+

(例如:我们有 3 个客户属于“aa”类别;其中 2 个提供了他们的电子邮件,2 个提供了他们的年龄,没有一个提供了它的地址)

我可以通过如下查询弄清楚如何做到这一点:

POST /customers/_search?size=0
{
  "aggs": {
    "category": {
      "terms": {
        "field": "category"
      },
      "aggs": {
        "count_email": {
          "filter": {
            "exists": {
              "field": "email"
            }
          }
        },
        "count_age": {
          "filter": {
            "exists": {
              "field": "age"
            }
          }
        },
        "count_address": {
          "filter": {
            "exists": {
              "field": "address"
            }
          }
        }
      }
    }
  }
}

但我无法在 Kibana Visualize 中找到如何做到这一点。 我应该使用脚本字段吗? JSON 输入 ?如何 ?有没有更好的办法?

感谢您的建议。

【问题讨论】:

  • 这没有给你合适的桌子?结果如何?
  • 我更新了我的答案以包含 kibana 的起始 url...这更接近您的需要吗?

标签: elasticsearch kibana


【解决方案1】:

在 UI 中,我可以使用关键字词过滤器来拆分行。

下面是一个帮助您入门的网址。

该调用将创建一个数据表,按计数聚合并按类别关键字词拆分行。

http://localhost:5601/app/kibana#/visualize/create?type=table&indexPattern=customers&_g=()&_a=(filters:!(),linked:!f,query:(query_string:(analyze_wildcard:!t,query:'*')),uiState:(vis:(params:(sort:(columnIndex:!n,direction:!n)))),vis:(aggs:!((enabled:!t,id:'1',params:(),schema:metric,type:count),(enabled:!t,id:'2',params:(field:category.keyword,order:desc,orderBy:_term,size:2),schema:bucket,type:terms)),listeners:(),params:(perPage:10,showMeticsAtAllLevels:!f,showPartialRows:!f,showTotal:!f,sort:(columnIndex:!n,direction:!n),totalFunc:sum),title:'CategoryTable',type:table))

【讨论】:

  • 感谢您的回答。为了避免误解,我用索引描述更新了问题。实际上没有数组数据类型的地方(这似乎是nested类型有用的情况?)。
  • 我现在看到了...随着更改,我看不出您的代码有任何问题。你得到什么结果?给我几分钟来安装堆栈并设置您的场景
  • 我在问题中输入的搜索查询给出了预期的结果,但我的问题是能够在 Kibana / Visualize 中重现它。
  • 我正在使用您的数据集玩弄可视化工具。似乎您想使用拆分行..我不确定它在 json 中的外观,但它会根据 category.keyword 正确分割您的数据
  • 按类别分割数据以获取文档数是可以的。问题是获取 emal 不为空、年龄不为空、地址不为空的文档的总数(请参见问题中的表格示例)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
相关资源
最近更新 更多