简体   繁体   English

如何为一段数据中的值数创建脚本字段?

[英]How do I create a scripted field for the number of values in a piece of data?

I am gathering data in Elasticsearch and visualizing in Kibana. 我正在收集Elasticsearch中的数据并在Kibana中进行可视化。 My data contains a variable number of fields for each entry, and I am looking to generate a histogram tracking the number of fields for each entry. 我的数据包含每个条目的可变数量的字段,我希望生成一个直方图,跟踪每个条目的字段数。 For example here are two of my pieces of data 例如,这是我的两个数据

{
   "extensions": {
      "@app/default-preset": "^2.2.0",
      "@app/redux": "^2.0.0",
      "@app/jest-plugin": "latest",
      "@app/next": "^2.0.0",
      "@app/cli": "latest",
      "@app/log": "^2.2.2",
      "@app/auth": "^2.0.0",
      "@app/intl": "^3.0.0"
    }
}
{
    "extensions": {
      "@app/default-preset": "^2.2.0",
      "@app/mocha-plugin": "latest",
      "@app/redux": "^2.0.0",
      "@app/cookies": "^2.0.0",
      "@app/next": "^2.0.0",
      "@app/intl-plugin": "^3.0.0",
      "@app/log": "^2.0.0",
      "@app/cli": "latest",
      "@app/auth": "^2.0.0",
      "@app/intl": "^3.1.1"
    }
}

So for the first I would like the value 7 and for the second I would like the value 10 . 所以对于第一个我想要值7和第二个我想值10 Even better would have a way to track the most common extensions, but that's a separate question. 更好的方法是跟踪最常见的扩展,但这是一个单独的问题。 I have tried, and failed, to create a scripted field for this. 我已经尝试过,并且失败了,为此创建了一个脚本字段。 In Javascript this would be Object.keys(extensions).length , but I have no idea how to do this in the painless language. 在Javascript中,这将是Object.keys(extensions).length ,但我不知道如何在painless语言中执行此操作。

Something like the following should work: 像下面这样的东西应该工作:

GET <your_index>/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "doc_field_count": {
      "script": "doc['extensions'].size()"
    }
  }
}

For counting the popular ones, I'd suggest you make another scripted field where you can use the .keySet() method to get the extension names as a Set. 为了统计流行的,我建议您创建另一个脚本字段,您可以使用.keySet()方法将扩展名称作为Set。 Later, you can analyze your newly created with with aggregations... 稍后,您可以使用聚合分析新创建的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM