{"action":"closed","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/30647","repository_url":"https://api.github.com/repos/elastic/elasticsearch","labels_url":"https://api.github.com/repos/elastic/elasticsearch/issues/30647/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/30647/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/30647/events","html_url":"https://github.com/elastic/elasticsearch/issues/30647","id":323562171,"number":30647,"title":"Cardinality on a keyword field inside a composite aggregation doesn't work","user":{"login":"ohadravid","id":2358365,"avatar_url":"https://avatars3.githubusercontent.com/u/2358365?v=4","gravatar_id":"","url":"https://api.github.com/users/ohadravid","html_url":"https://github.com/ohadravid","followers_url":"https://api.github.com/users/ohadravid/followers","following_url":"https://api.github.com/users/ohadravid/following{/other_user}","gists_url":"https://api.github.com/users/ohadravid/gists{/gist_id}","starred_url":"https://api.github.com/users/ohadravid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ohadravid/subscriptions","organizations_url":"https://api.github.com/users/ohadravid/orgs","repos_url":"https://api.github.com/users/ohadravid/repos","events_url":"https://api.github.com/users/ohadravid/events{/privacy}","received_events_url":"https://api.github.com/users/ohadravid/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-05-16T10:30:07Z","updated_at":"2018-05-16T10:39:15Z","closed_at":"2018-05-16T10:39:15Z","author_association":"NONE","body":"<!--\r\n\r\n** Please read the guidelines below. **\r\n\r\nIssues that do not follow these guidelines are likely to be closed.\r\n\r\n1.  GitHub is reserved for bug reports and feature requests. The best place to\r\n    ask a general question is at the Elastic [forums](https://discuss.elastic.co).\r\n    GitHub is not the place for general questions.\r\n\r\n2.  Is this bug report or feature request for a supported OS? If not, it\r\n    is likely to be closed.  See https://www.elastic.co/support/matrix#show_os\r\n\r\n3.  Please fill out EITHER the feature request block or the bug report block\r\n    below, and delete the other block.\r\n\r\n-->\r\n<!-- Bug report -->\r\n\r\n**Elasticsearch version** (`bin/elasticsearch --version`):  6.2.4\r\n\r\n**Plugins installed**: []\r\n\r\n**JVM version** (`java -version`): 1.8.0_161\r\n\r\n**OS version** (`uname -a` if on a Unix-like system): Linux 9220caee2267 4.9.87-linuxkit-aufs #1 SMP Wed Mar 14 15:12:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux\r\n\r\n**Description of the problem including expected versus actual behavior**:\r\n\r\nWhen using a `cardinality` aggregation on a `keyword` type field inside a  composite aggregation,\r\n0 is returned instead of the actual cardinality.\r\n\r\n**Steps to reproduce**:\r\n\r\n1. Using the latest docker `docker.elastic.co/elasticsearch/elasticsearch:6.2.4`, create the following document & mapping.\r\n\r\n```python\r\n#/usr/bin/python\r\nes = elasticsearch.Elasticsearch('localhost')\r\n\r\nes.indices.put_mapping('sample', {\r\n    'properties': {\r\n        'user_name': {\r\n            'type': 'keyword'   \r\n        }\r\n    }\r\n}, index='sample')\r\n\r\nes.index('sample', 'sample', {\r\n    'port': 80,\r\n    'user_name': 'some_user',\r\n    'session_id': 12\r\n})\r\n```\r\n\r\n2. Run the query:\r\n```json\r\nGET sample/_search\r\n{\r\n  \"query\": {\r\n    \"match_all\": {}\r\n  },\r\n  \"aggs\": {\r\n    \"my_buckets\": {\r\n      \"composite\": {\r\n        \"size\": 10,\r\n        \"sources\": [\r\n          {\r\n            \"port_agg\": {\r\n              \"terms\": {\r\n                \"field\": \"port\"\r\n              }\r\n            }\r\n          }\r\n        ]\r\n      },\r\n      \"aggs\": {\r\n        \"dc_username\": {\r\n          \"cardinality\": {\r\n            \"field\": \"user_name\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n  },\r\n  \"size\": 0\r\n}\r\n```\r\n\r\nThe result is:\r\n```json\r\n{\r\n  \"took\": 2,\r\n  \"timed_out\": false,\r\n  \"_shards\": {\r\n    \"total\": 5,\r\n    \"successful\": 5,\r\n    \"skipped\": 0,\r\n    \"failed\": 0\r\n  },\r\n  \"hits\": {\r\n    \"total\": 1,\r\n    \"max_score\": 1,\r\n    \"hits\": []\r\n  },\r\n  \"aggregations\": {\r\n    \"my_buckets\": {\r\n      \"buckets\": [\r\n        {\r\n          \"key\": {\r\n            \"port_agg\": 80\r\n          },\r\n          \"doc_count\": 1,\r\n          \"dc_username\": {\r\n            \"value\": 0\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nWhere `dc_username` is 0, instead of 1.\r\n\r\nUsing `terms` instead will return the correct result:\r\n```json\r\nGET sample/_search\r\n{\r\n  \"query\": {\r\n    \"match_all\": {}\r\n  },\r\n  \"aggs\": {\r\n    \"my_buckets\": {\r\n      \"terms\": {\r\n        \"size\": 10,\r\n        \"field\": \"port\"\r\n      },\r\n      \"aggs\": {\r\n        \"dc_username\": {\r\n          \"cardinality\": {\r\n            \"field\": \"user_name\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n  },\r\n  \"size\": 0\r\n}\r\n```\r\n\r\n```json\r\n{\r\n  \"took\": 23,\r\n  \"timed_out\": false,\r\n  \"_shards\": {\r\n    \"total\": 5,\r\n    \"successful\": 5,\r\n    \"skipped\": 0,\r\n    \"failed\": 0\r\n  },\r\n  \"hits\": {\r\n    \"total\": 1,\r\n    \"max_score\": 0,\r\n    \"hits\": []\r\n  },\r\n  \"aggregations\": {\r\n    \"my_buckets\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": 80,\r\n          \"doc_count\": 1,\r\n          \"dc_username\": {\r\n            \"value\": 1\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n**Provide logs (if relevant)**:\r\n\r\n"}}
{"actor":{"display_login":"jimczi"}}