# Getting last document per specific field

**URL:** <https://discuss.elastic.co/t/getting-last-document-per-specific-field/300564>\
**Category:** Elasticsearch\
**Created:** [March 24, 2022, 11:23am UTC](https://discuss.elastic.co/t/getting-last-document-per-specific-field/300564 "2022-03-24T11:23:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malo\_Maisonneuve](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/malo_maisonneuve/32/101718_2.png) [@Malo\_Maisonneuve](https://discuss.elastic.co/u/Malo_Maisonneuve)\
**Post date:** [March 24, 2022, 11:23am UTC](https://discuss.elastic.co/t/getting-last-document-per-specific-field/300564/1 "2022-03-24T11:23:10Z")

</div>

Hi,

I'm collecting logs from multiple devices, and I'm trying to retrieve the last log from each device.  
For instance, I'd like to have :

- "deviceId": 4568 =\> {content of last log according to timestamp for this device}
- "deviceId": 7865 =\> {content of last log according to timestamp for this device}

I've tried to make it work with collapse and inner hits, but it doesn't seem to return what I'm looking for.

Current attempt:

```auto
GET data-*/_search
{
  "size": 10, 
  "query": {
    "exists": {
      "field": "deviceId"
    }
  },
  "collapse": {
    "field": "deviceId.keyword",
    "inner_hits": {
      "name": "most_recent",
      "size": 1,
      "sort": [ 
        {"@timestamp": {"order": "desc", "missing" : "_last"} }
        ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 24, 2022, 1:11pm UTC](https://discuss.elastic.co/t/getting-last-document-per-specific-field/300564/2 "2022-03-24T13:11:43Z")

</div>

Can you compile a small reproducible example to try out locally with 4-5 documents? Also the Elasticsearch version you are using would be helpful.

Thanks!

---

<div class="post-metadata">

**Author:** ![Malo\_Maisonneuve](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/malo_maisonneuve/32/101718_2.png) [@Malo\_Maisonneuve](https://discuss.elastic.co/u/Malo_Maisonneuve)\
**Post date:** [March 24, 2022, 2:52pm UTC](https://discuss.elastic.co/t/getting-last-document-per-specific-field/300564/3 "2022-03-24T14:52:27Z")

</div>

Thanks for the quick response!  
I'm using the 7.16.3 version of Elasticsearch.

Here are 5 documents:

```auto
PUT /test
{
  "mappings":{
    "properties": {
      "deviceId" : { "type" : "text" },
      "@timestamp": {"type" : "date"},
      "channel": {"type": "text"}
    }
  }
}

POST test/_doc/1
{
  "deviceId" : "456",
  "@timestamp" : "2022-03-24T11:00:00.000Z",
  "channel" : "A"
}

POST test/_doc/2
{
  "deviceId" : "456",
  "@timestamp" : "2022-03-24T12:00:00.000Z",
  "channel" : "B"
}

POST test/_doc/3
{
  "deviceId" : "123",
  "@timestamp" : "2022-03-24T11:00:00.000Z",
  "channel" : "C"
}

POST test/_doc/4
{
  "deviceId" : "123",
  "@timestamp" : "2022-03-24T12:00:00.000Z",
  "channel" : "D"
}

POST test/_doc/5
{
  "deviceId" : "123",
  "@timestamp" : "2022-03-24T13:00:00.000Z",
  "channel" : "E"
}

```

I want my query to return both documents below:

```auto
{
  "deviceId" : "456",
  "@timestamp" : "2022-03-24T12:00:00.000Z",
  "channel" : "B"
}
{
  "deviceId" : "123",
  "@timestamp" : "2022-03-24T13:00:00.000Z",
  "channel" : "E"
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 28, 2022, 8:15am UTC](https://discuss.elastic.co/t/getting-last-document-per-specific-field/300564/4 "2022-03-28T08:15:22Z")

</div>

I changed your sample to not map the `deviceId` as sorting is not possible on text fields. Then the following looks like it worked:

```auto
GET test/_search
{
  "collapse": {
    "field": "deviceId.keyword"
  },
  "sort": [
    {
      "@timestamp": {
        "order": "desc"
      }
    }
  ]
}

```

---

<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:** [April 25, 2022, 8:16am UTC](https://discuss.elastic.co/t/getting-last-document-per-specific-field/300564/5 "2022-04-25T08:16:03Z")

</div>

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