# Filter result by collapsed date

**URL:** <https://discuss.elastic.co/t/filter-result-by-collapsed-date/330235>\
**Category:** Elasticsearch\
**Created:** [April 18, 2023, 3:35pm UTC](https://discuss.elastic.co/t/filter-result-by-collapsed-date/330235 "2023-04-18T15:35:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mickael\_BARBIER](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mickael_barbier/32/118642_2.png) [@Mickael\_BARBIER](https://discuss.elastic.co/u/Mickael_BARBIER)\
**Post date:** [April 18, 2023, 3:35pm UTC](https://discuss.elastic.co/t/filter-result-by-collapsed-date/330235/1 "2023-04-18T15:35:32Z")

</div>

Hello,  
i have a topic/news system  
(a topic can have many news in different language)

i want to get the oldest news of each topic. And i want the result sorted by the oldest news displayedAt column.  
ex:  
topic1  
-News1-t1: 2023-04-15  
-News2-t1: 2023-04-18  
topic2  
-News1-t2: 2023-04-17  
-News2-t2: 2023-04-16

Must result:  
News1-t1  
News2-t2

My actual code use collapse:

```auto
GET news/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "type": "news"
          }
        },
        {
          "term": {
            "onlineStatus": "online"
          }
        },
        {
          "terms": {
            "writingStatus": [
              "validated",
              "finished"
            ]
          }
        },
        {
          "exists": {
            "field": "displayedAt"
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "deletedAt"
              }
            }
          }
        }
      ]
    }
  },
  "collapse": {
    "field": "topic.id",
    "inner_hits": {
      "name": "oldest_news",
      "size": 1,
      "sort": [
        {
          "displayedAt": {
            "order": "asc"
          }
          
        }
      ]
    },
    "aggs": {
      "oldest_displayedAt": {
        "max": {
          "field": "oldest_news.displayedAt"
        }
      }
    }
  },
  "sort": [
    {
      "oldest_displayedAt": {
        "order": "desc"
      }
    },
    {
      
      "displayedAt": {
        "order": "desc"
      }
    }
  ], 
      
  "from": 0
}

```

I also tried to get my result with aggs but i always get the same problem, i can't get my final result sorted as i want.

Thank by advance for your help.

---

<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 16, 2023, 3:35pm UTC](https://discuss.elastic.co/t/filter-result-by-collapsed-date/330235/2 "2023-05-16T15:35:33Z")

</div>

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