# Error when trying to aggregate a bucket script with another bucket script

**URL:** https://discuss.elastic.co/t/error-when-trying-to-aggregate-a-bucket-script-with-another-bucket-script/194416
**Category:** Elasticsearch
**Created:** [August 8, 2019, 11:26am UTC](https://discuss.elastic.co/t/error-when-trying-to-aggregate-a-bucket-script-with-another-bucket-script/194416 "2019-08-08T11:26:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![liorg2](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@liorg2](https://discuss.elastic.co/u/liorg2)
#### Post date: [August 8, 2019, 11:26am UTC](https://discuss.elastic.co/t/error-when-trying-to-aggregate-a-bucket-script-with-another-bucket-script/194416/1 "2019-08-08T11:26:23Z")

</div>

Hi,  
I'm having trouble to make this query work (original fields name were changed).  
What I'm basically trying to do, is calculate a score for each entity (each entity has a different score weights), and then calculate one score for both of them (in the real query there will be about 20 entities )

the problem is with the following part, which returns an error:

![image](https://us1.discourse-cdn.com/elastic/original/3X/c/e/ce3c46b15dde5d9a06e0ba2061b099f600567cd7.png)

```
{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "aggregation_execution_exception",
      "reason": "buckets_path must reference either a number value or a single value numeric metric aggregation, got: java.lang.Object[]"
    }
  },
  "status": 503
}

```

Here is the full query:

```
GET logs2019/_search
{
  "size": 0,
  "aggs": {
    "dates": {
      "date_histogram": {
        "field": "date_time",
        "interval": "1h",
        "min_doc_count": 1
      },
      "aggs": {
        "entity_123": {
          "filters": {
            "filters": [
              {
                "term": {
                  "entity": 123
                }
              }
            ]
          },
          "aggs": {
            "entity_score": {
              "bucket_script": {
                "buckets_path": {
                  "A": "SCORE_A",
                  "B": "SCORE_B",
                  "A_entity_weight": 0.6,
                  "B_entity_weight": 0.4
                },
                "script": "params.A * params.A_entity_weight +params.B * params.B_entity_weight"
              }
            },
            "bucket1_A": {
              "filter": {
                "range": {
                  "field1": {
                    "gte": 0,
                    "lte": 10
                  }
                }
              }
            },
            "bucket2_A": {
              "filter": {
                "range": {
                  "field2": {
                    "gt": 10,
                    "lte": 20
                  }
                }
              }
            },
            "SCORE_A": {
              "bucket_script": {
                "buckets_path": {
                  "A": "bucket1_A._count",
                  "B": "bucket2_A._count"
                },
                "script": {
                  "id": "calcScore"
                }
              }
            },
            "bucket1_B": {
              "filter": {
                "range": {
                  "field_2": {
                    "gte": 0,
                    "lte": 10
                  }
                }
              }
            },
            "bucket2_B": {
              "filter": {
                "range": {
                  "field_2": {
                    "gt": 10,
                    "lte": 20
                  }
                }
              }
            },
            "SCORE_B": {
              "bucket_script": {
                "buckets_path": {
                  "A": "bucket1_B._count",
                  "B": "bucket2_B._count"
                },
                "script": {
                  "id": "calcScore"
                }
              }
            }
          }
        },
        "entity_456": {
          "filters": {
            "filters": [
              {
                "term": {
                  "entity": 456
                }
              }
            ]
          },
          "aggs": {
            "entity_score": {
              "bucket_script": {
                "buckets_path": {
                  "A": "SCORE_A",
                  "B": "SCORE_B",
                  "A_entity_weight": 0.9,
                  "B_entity_weight": 0.1
                },
                "script": "params.A * params.A_entity_weight +params.B * params.B_entity_weight"
              }
            },
            "bucket1_A": {
              "filter": {
                "range": {
                  "field1": {
                    "gte": 0,
                    "lte": 10
                  }
                }
              }
            },
            "bucket2_A": {
              "filter": {
                "range": {
                  "field2": {
                    "gt": 10,
                    "lte": 20
                  }
                }
              }
            },
            "SCORE_A": {
              "bucket_script": {
                "buckets_path": {
                  "A": "bucket1_A._count",
                  "B": "bucket2_A._count"
                },
                "script": {
                  "id": "calcScore"
                }
              }
            },
            "bucket1_B": {
              "filter": {
                "range": {
                  "field_2": {
                    "gte": 0,
                    "lte": 10
                  }
                }
              }
            },
            "bucket2_B": {
              "filter": {
                "range": {
                  "field_2": {
                    "gt": 10,
                    "lte": 20
                  }
                }
              }
            },
            "SCORE_B": {
              "bucket_script": {
                "buckets_path": {
                  "A": "bucket1_B._count",
                  "B": "bucket2_B._count"
                },
                "script": {
                  "id": "calcScore"
                }
              }
            }
          }
        },
        "overall_score": {
          "bucket_script": {
            "buckets_path": {
              "score_123": "entity_123>entity_score",
              "score_456": "entity_456>entity_score"
            },
            "script": "params.score_123 * 0.2 + params.score_456*0.8 "
          }
        }
      }
    }
  }
}

```

I have tried to change the filter **s** aggregation into filter aggregation, but got another error:

`org.elasticsearch.search.aggregations.bucket.filter.InternalFilter cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation`

from that part:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/e/ce1e74385f3fe268ee4f6651a450833a031d8ca9.png)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 5, 2019, 11:27am UTC](https://discuss.elastic.co/t/error-when-trying-to-aggregate-a-bucket-script-with-another-bucket-script/194416/2 "2019-09-05T11:27:41Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
