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

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

{
  "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 filters 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

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