简体   繁体   中英

How to aggregate on size of nested document in ElasticSearch?

nested field name is transactions.

each document has a list of transactions.

I wish to aggregate on the number of transactions. Information i seek is the number of documents with more than one transaction.

I tried the following:

"aggs": {
"tx_count": {
  "terms": {
    "script": {
      "inline": "if(doc['transactions'].size>1){return 1;} else {return 0;}"
    }
  }
}

}

Also tried using this aggregation within a nested aggregation. Is there a way to do this? I'm on ES version 5.6 currently.

You can use params._source to calculate the transactions length:

{
  "size": 0,
  "aggs": {
    "tx_count": {
      "terms": {
        "script": {
          "inline": "if (params._source['transactions'].size() > 1) { return 1; } else { return 0; }"
        }
      }
    }
  }
}

BTW notice that it's .size() , not .size .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM