Search code examples
elasticsearchkibanakibana-4

bucket script not working - elasticsearch 2.4.2


I have tried to subtract the aggregations

{
  "query": {
    "match_all": {}
  },
  "size": 0,  
  "aggs": {
      "total_query_id": {
          "sum": {
            "field": "query_id"
          }
        },

      "total_num_results": {
        "sum": {
          "field": "num_results"
        }
    },               
    "minus_value": {
          "bucket_script": {
            "buckets_path": {
              "qid": "total_query_id",
              "nrs": "total_num_results"
            },
            "script": "qid - nrs"
          }
        } 
    }  
}

it throws the below error

"reason": "Invalid pipeline aggregation named [minus_value] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"

I have moved to back and forth minus_value node to aggs node but it does not solve my problem.

can anyone help me on this?


Solution

  • The idea is that pipeline aggregations must work on a parent bucket aggregation.

    It is not the case in your example, so you must have one parent aggregation. Since you have a match_all query, you could try using a global bucket aggregation and then embed your 3 aggregations inside it, like this:

    {
      "query": {
        "match_all": {}
      },
      "size": 0,
      "aggs": {
        "all": {
          "global": {},
          "aggs": {
            "total_query_id": {
              "sum": {
                "field": "query_id"
              }
            },
            "total_num_results": {
              "sum": {
                "field": "num_results"
              }
            },
            "minus_value": {
              "bucket_script": {
                "buckets_path": {
                  "qid": "total_query_id",
                  "nrs": "total_num_results"
                },
                "script": "qid - nrs"
              }
            }
          }
        }
      }
    }