# Elasticsearch Remove remove duplicae and descending order

**URL:** <https://discuss.elastic.co/t/elasticsearch-remove-remove-duplicae-and-descending-order/107761>\
**Category:** Elasticsearch\
**Created:** [November 15, 2017, 2:08pm UTC](https://discuss.elastic.co/t/elasticsearch-remove-remove-duplicae-and-descending-order/107761 "2017-11-15T14:08:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tushar\_chevulkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tushar_chevulkar/32/46270_2.png) [@tushar\_chevulkar](https://discuss.elastic.co/u/tushar_chevulkar)\
**Post date:** [November 15, 2017, 2:08pm UTC](https://discuss.elastic.co/t/elasticsearch-remove-remove-duplicae-and-descending-order/107761/1 "2017-11-15T14:08:41Z")

</div>

I have news articles form multiple sources saved and each source have different category I need to write a query which will reverse time sort the article also i don't need more than 3 articles from a particular source I am using the below query but the results are wrong can any one tell me what am i doing wrong.

```
{
					  "query": {
						"bool": {
						  "must": [
							{
							  "match_phrase": {
								  "category": "Digital"
								}
							},
							{
							  "match_phrase": {
								  "type": "Local"
								}
							} 
						  ]
						}
						
					  },
					  
    						
    						
    						"aggs":{
                      "dedup" : {
                        "terms":{
                          "field": "source"
                         },
                         "aggs":{
                           "dedup_docs":{
                             "top_hits":{
                               "sort": [
                                  {
                                      "pub_date": {
                                          "order": "desc"
                                      }
                                  }
                              ],
                              "size": 2
                             }
                           }
                         }    
                      }
                    },"sort": [
                      {
                        "pub_date": {
                          "order": "desc"
                        }
                      }
                    ]
}
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [November 15, 2017, 2:49pm UTC](https://discuss.elastic.co/t/elasticsearch-remove-remove-duplicae-and-descending-order/107761/2 "2017-11-15T14:49:18Z")

</div>

See [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)

---

<div class="post-metadata">

**Author:** ![tushar\_chevulkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tushar_chevulkar/32/46270_2.png) [@tushar\_chevulkar](https://discuss.elastic.co/u/tushar_chevulkar)\
**Post date:** [November 15, 2017, 6:02pm UTC](https://discuss.elastic.co/t/elasticsearch-remove-remove-duplicae-and-descending-order/107761/3 "2017-11-15T18:02:35Z")

</div>

I tried the following query but it solved my problem partially. after running the below query i am only getting one result of each source but i want to allow upto 3.

```
{
    "query": {
        "bool": {
						  "must": [
							{
							  "match_phrase": {
								  "category": "Digital"
								}
							},
							{
							  "match_phrase": {
								  "type": "Local"
								}
							} 
						  ]
						}
						
    },
    "collapse" : {
        "field" : "source.keyword",
         "max_concurrent_group_searches": 3
    },
    "sort": [
      {
        "pub_date": {
          "order": "desc"
        }
      }
    ]
}

```

Any solution?

---

<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:** [December 13, 2017, 6:02pm UTC](https://discuss.elastic.co/t/elasticsearch-remove-remove-duplicae-and-descending-order/107761/4 "2017-12-13T18:02:48Z")

</div>

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