Vahid Kharazi
Vahid Kharazi

Reputation: 6033

Elasticsearch cardinality aggregation return zero

I have an index with the following mapping:

{"vahid":{"mappings":{"doc":{"properties":{"@timestamp":{"type":"date"},"@version":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"message":{"properties":{"context":{"properties":{"adID":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"fID":{"type":"long"},"zoneID":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"filename":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"funcName":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"hostname":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"lineno":{"type":"long"},"logLevel":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"message":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"pid":{"type":"long"},"timestamp":{"type":"float"}}}}}}}}

in summary, the mapping for adID field is

{"adID":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}

and I want to do a cardinality aggregation to count the number of unique adIDs

: agg = { 
...:     "aggs" : { 
...:         "type_count" : { 
...:             "cardinality" : { 
...:                 "field" : "message.context.keyword.adID" 
...:             } 
...:         } 
...:     } 
...: }  

and the result is

{'took': 13,
 'timed_out': False,
 '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
 'hits': {'total': 7,
  'max_score': 1.0,
  'hits': [{'_index': 'vahid',
    '_type': 'doc',
    '_id': '0XrVPWkBu4YGVBHQV9Sa',
    '_score': 1.0,
    '_source': {'message': {'context': {'adID': 1}}}},
   {'_index': 'vahid',
    '_type': 'doc',
    '_id': '0nrWPWkBu4YGVBHQO9TR',
    '_score': 1.0,
    '_source': {'message': {'context': {'fID': 1}}}},
   {'_index': 'vahid',
    '_type': 'doc',
    '_id': '03rXPWkBu4YGVBHQKdQt',
    '_score': 1.0,
    '_source': {'message': {'context': {'adID': 2}}}},
   {'_index': 'vahid',
    '_type': 'doc',
    '_id': '13rXPWkBu4YGVBHQ2NQG',
    '_score': 1.0,
    '_source': {'message': {'context': {'adID': 3}}}},
   {'_index': 'vahid',
    '_type': 'doc',
    '_id': '1XrXPWkBu4YGVBHQzdQG',
    '_score': 1.0,
    '_source': {'message': {'context': {'adID': 3}}}},
   {'_index': 'vahid',
    '_type': 'doc',
    '_id': '1nrXPWkBu4YGVBHQ09Qb',
    '_score': 1.0,
    '_source': {'message': {'context': {'adID': 3}}}},
   {'_index': 'vahid',
    '_type': 'doc',
    '_id': '1HrXPWkBu4YGVBHQbdRa',
    '_score': 1.0,
    '_source': {'message': {'context': {'adID': 3}}}}]},
 'aggregations': {'type_count': {'value': 0}}}

Why the result of the cardinality aggregation is zero?

Upvotes: 0

Views: 774

Answers (1)

beatrice
beatrice

Reputation: 4391

You haved used the field reference in the wrong order in your query

{ 
     "aggs" : { 
         "type_count" : { 
            "cardinality" : { 
                "field" : "message.context.adID.keyword" 
            } 
        } 
    }   
}  

Upvotes: 2

Related Questions