0

I am gathering data in Elasticsearch and visualizing in 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. 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.

1 Answers1

0

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. Later, you can analyze your newly created with with aggregations...

Muhammad Ali
  • 712
  • 7
  • 14