# How to get distinct values from elastic search

**URL:** <https://discuss.elastic.co/t/how-to-get-distinct-values-from-elastic-search/230020>\
**Category:** Elasticsearch\
**Created:** [April 27, 2020, 7:53pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-values-from-elastic-search/230020 "2020-04-27T19:53:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![msaundi](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@msaundi](https://discuss.elastic.co/u/msaundi)\
**Post date:** [April 27, 2020, 7:53pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-values-from-elastic-search/230020/1 "2020-04-27T19:53:15Z")

</div>

Hello,

I am trying to get distinct count form the elastic search. for the grouping I am using cardinality aggregation along with query string.  
I am using elastic 6.x with php client.  
below is my query.

```auto
    Array
    (
        [index] => o_contacts.o_contacts_profile
        [body] => Array
            (
                [query] => Array
                    (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [0] => Array
                                        (
                                            [term] => Array
                                                (
                                                    [status_flag] => g
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [contact_id] => Array
                                                        (
                                                            [0] => 5
                                                            [1] => 8
                                                            [2] => 1
                                                            [3] => 4
                                                            [4] => 2
                                                            [5] => 7
                                                        )
                                                )
                                        )
                                    [2] => Array
                                        (
                                            [match] => Array
                                                (
                                                    [status] => 1
                                                )
                                        )
                                    [3] => Array
                                        (
                                            [match] => Array
                                                (
                                                    [white_list_flag] => 1
                                                )
                                        )

                                    [4] => Array
                                        (
                                            [match] => Array
                                                (
                                                    [bounce_flag] => 1
                                                )

                                        )

                                    [5] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [range] => Array
                                                        (
                                                            [age] => Array
                                                                (
                                                                    [gte] => 0
                                                                    [lte] => 95
                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [range] => Array
                                                        (
                                                            [delivery_timestamp] => Array
                                                                (
                                                                    [gte] => 0
                                                                )

                                                        )

                                                )

                                        )

                                    [6] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [gender] => Array
                                                        (
                                                            [1] => Male
                                                            [2] => Female
							    [3] => Unspecified			
                                                        )

                                                )

                                        )

                                    [7] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [loc] => Array
                                                        (
                                                            [0] => England
                                                        )
                                                )
                                        )

                                    [8] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [town] => Array
                                                        (
                                                            [0] => Cambridge
                                                        )
                                                )
                                        )

                                    [9] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [region] => Array
                                                        (
                                                            [0] => East
                                                        )

                                                )

                                        )

                                )

                            [must_not] => Array
                                (
                                    [0] => Array
                                        (
                                            [term] => Array
                                                (
                                                    [sunset_flag] => 1
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [term] => Array
                                                (
                                                    [global_flag] => 1
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [term] => Array
                                                (
                                                    [flag] => 1
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [contact_email_id] => Array
                                                        (
                                                        )

                                                )

                                        )

                                    [4] => Array
                                        (
                                            [terms] => Array
                                                (
                                                    [email_domain_id] => Array
                                                        (
                                                            [0] => 100
                                                        )

                                                )

                                        )

                                )

                        )

                )

            [aggs] => Array
                (
                    [count] => Array
                        (
                            [cardinality] => Array
                                (
                                    [field] => contact_email_id
                                    [precision_threshold] => 10000
                                )

                        )

                )

        )

    [size] => 0
)

```

I have bulk collection. When ever I am trying to get unique data from the elastic it is showing greater count than the available.  
i.e

[aggregations] =\> Array  
(  
[count] =\> Array  
(  
[value] =\> 55882  
)  
)

but the expected count is 52500. (In order to complete the task the count should be equal)  
Kindly let me know is there any other way to solve this issue.

```auto

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 25, 2020, 8:03pm UTC](https://discuss.elastic.co/t/how-to-get-distinct-values-from-elastic-search/230020/2 "2020-05-25T20:03:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
