PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How to aggregate on size of nested document in ElasticSearch?

 October 24, 2022     elasticsearch, java     No comments   

Issue

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 Nf 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;}"
        }
      }
    }
  }

I also tried using this aggregation within a nested aggregation. Is there a way to do this?

I'm on ES version 5.6 currently.


Solution

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.



Answered By - Joe - ElasticsearchBook.com
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing