# Distinct field values for a search condition

**URL:** <https://discuss.elastic.co/t/distinct-field-values-for-a-search-condition/104947>\
**Category:** Elasticsearch\
**Created:** [October 23, 2017, 6:04pm UTC](https://discuss.elastic.co/t/distinct-field-values-for-a-search-condition/104947 "2017-10-23T18:04:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![swaplogstash](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@swaplogstash](https://discuss.elastic.co/u/swaplogstash)\
**Post date:** [October 23, 2017, 6:04pm UTC](https://discuss.elastic.co/t/distinct-field-values-for-a-search-condition/104947/1 "2017-10-23T18:04:36Z")

</div>

elastic search query , I need to get unqiue results. I have documents with below mapping.

```
   "appId": {
                    "type": "text"
                },
                "date": {
                    "type": "date"
                },
                "search": {
                    "type": "keyword"
                },
                "type": {
                    "type": "keyword"
                },
                "userId": {
                    "type": "text"
                }

```

I need to query on **userId** and search result should have unique combination of search and type feilds and should display only latest date result.  
dedup is giving count but not distinct results.

Sample data:  
{

```
            "_source": {
                "date": "2017-10-23T16:43:59.757Z",
                "appId": "xxx",
                "search": "$0",
                "type": "lenient",
                "userId": "USER1234"
            }
        },
        {
           
            "_source": {
                "date": "2017-10-23T17:30:06.544Z",
                "appId": "xxx",
                "search": "RAVI",
                "type": "index",
                "userId": "USER1234"
            }
        },
        {
           
            "_source": {
                "date": "2017-10-23T16:48:16.779Z",
                "appId": "xxx",
                "search": "$0",
                "type": "lenient",
                "userId": "USER124"
            }
        },
        {
            
            "_source": {
                "date": "2017-10-23T17:29:55.993Z",
                "appId": "xxx",
                "search": "RAVI",
                "type": "index",
                "userId": "USER1234"
            }
        },
         {
            "_source": {
                "date": "2017-10-23T17:29:55.993Z",
                "appId": "xxx",
                "search": "RAVI",
                "type": "index_mtnsummary",
                "userId": "2560559ADKV"
            }
        }

```

when searched for userid USER1234, query should display only below latest date records.  
( select distinct search ,type from xxxindex where userID='USER1234')  
{

```
            "_source": {
                "date": "2017-10-23T17:30:06.544Z",
                "appId": "xxx",
                "search": "RAVI",
                "type": "index",
                "userId": "USER1234"
            }
        },
        {
           
            "_source": {
                "date": "2017-10-23T16:48:16.779Z",
                "appId": "xxx",
                "search": "$0",
                "type": "lenient",
                "userId": "USER124"
            }
        }
```

---

<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:** [November 20, 2017, 6:05pm UTC](https://discuss.elastic.co/t/distinct-field-values-for-a-search-condition/104947/2 "2017-11-20T18:05:00Z")

</div>

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