Scripting in cardinality aggregation

How does scripting in cardinality aggregation works. Is all values for the given field iterated and hashes for those are computed on the fly or is my assumption wrong? Can anyone explain exactly how scripting in cardinality works?

You can find the explanation here:

Hashes are computed on the fly with a noticeable performance hit.

Hi jim,
Thanks for your reply. I just cant understand one thing from that document. if i mention a field name in cardinality script, say for example

"aggs" : {
"author_count" : {
"cardinality" : {
"script": "doc['author.first_name'].value"

is all the values for the field "author.first_name" is iterated once to compute the hashes and cardinality is calulated.

The cardinality for a field is the number of distinct values, if a document has more than one value for a field then each value is hashed and is added in the computation. For instance:
doc1: foo, bar
doc2: foo

.. has a cardinality of 2 (foo and bar).

Hi Jim,

Consider a case

doc1 : foo
doc2 : bar
doc3 : foo
doc4 : foo

In this case cardinality will be 2. my doubt is how many times hashes will be computed in cardinality scripting. will it be done 4 times in the above case.

Yes unless you use another field to precompute the hashes at indexing time:

1 Like

Thanks Jim that makes it clear now