There's a nasty issue with filtering the aggregation results for nested documents

Regarding the following index:

  1. For each document's nested sub-document tradeRecords, internally group them and count the number of tradeRecords where tradeRecords.tradeDate falls between [1647273600000, 1674144000000], denoted as record_total. This record_total is a result for each document.
  2. Filter out all documents where record_total falls between [2,5].

json

Copy code

{
	"settings": {
		"number_of_shards": 1,
		"number_of_replicas": 0
	},
	"mappings": {
		"properties": {
			"id": {
				"type": "keyword"
			},
			"tradeRecords": {
				"type": "nested",
				"properties": {
					"tradeDate": {
						"type": "date"
					},
					"tradeId": {
						"type": "keyword"
					},
					"tradeType": {
						"type": "keyword"
					}
				}
			}
		}
	}
}

My query is throwing an error. Elasticsearch version is 7.13.4.

json

Copy code

{
	"size": 0,
	"aggs": {
		"documents": {
			"terms": {
				"field": "id",
				"size": 10
			},
			"aggs": {
				"nested_trade_records": {
					"nested": {
						"path": "tradeRecords"
					},
					"aggs": {
						"filtered_trade_records": {
							"filter": {
								"range": {
									"tradeRecords.tradeDate": {
										"gte": 1647273600000,
										"lte": 1674144000000
									}
								}
							}
						},
						"filtered_documents": {
							"bucket_selector": {
								"buckets_path": {
									"recordTotal": "filtered_trade_records._count"
								},
								"script": "params.recordTotal >= 2 && params.recordTotal <= 5"
							}
						}
					}
				}
			}
		}
	}
}

Detailed error message:

json

Copy code

{
    "error": {
        "root_cause": [],
        "type": "search_phase_execution_exception",
        "reason": "",
        "phase": "fetch",
        "grouped": true,
        "failed_shards": [],
        "caused_by": {
            "type": "class_cast_exception",
            "reason": "org.elasticsearch.search.aggregations.bucket.nested.InternalNested cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"
        }
    },
    "status": 500
}

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