Elasticsearch Remove remove duplicae and descending order

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"
                        }
                      }
                    ]
}

See field collapsing

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?

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