# Get the most recent record which matches a field

**URL:** <https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635>\
**Category:** Elasticsearch\
**Created:** [March 2, 2020, 7:37am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635 "2020-03-02T07:37:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbozic00](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@mbozic00](https://discuss.elastic.co/u/mbozic00)\
**Post date:** [March 2, 2020, 7:37am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635/1 "2020-03-02T07:37:20Z")

</div>

Hi,

When I perform this query:

```
{
	"query": {
		"match": {
			"service_id": "foo"
		}
	},
	"size": 1
}

```

I get one entry (the earliest by timestamp) that matches the desired service\_id. However, I would like to retrieve the most recent matching entry. I do:

```
{
	"query": {
		"match": {
			"service_id": "foo"
		}
	},
	"sort": {
		"@timestamp": "desc"
	},
	"size": 1
}

```

But then I get the most recent entry regardless of service\_id. Sometimes the service\_id is "foo", but sometimes it's "bar", sometimes ... It's as if Elasticsearch completely ignores the "match" when I add the "sort" part. It doesn't help if I put the "sort" before the query, between the query and size, or at the end after the size.

Am I doing anything wrong? Could someone please help?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 2, 2020, 7:57am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635/2 "2020-03-02T07:57:36Z")

</div>

Welcome!

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![mbozic00](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@mbozic00](https://discuss.elastic.co/u/mbozic00)\
**Post date:** [March 2, 2020, 8:43am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635/3 "2020-03-02T08:43:25Z")

</div>

Hi, I performed a full reproduction. I put two entries like this:

```auto
DELETE index
PUT index/_doc/1
{
   "service_id": "this-is-my-foo",
   "timestamp": "2020-03-01T10:26:12.301Z"
}
PUT index/_doc/2
{
   "service_id": "this-is-my-bar",
   "timestamp": "2020-03-01T11:11:11.111Z"
}

```

Then I search like this:

```auto
POST /index/_search
{
  "query": {
    "match": {
      "service_id": "this-is-my-foo"
    }
  },
  "size": 1
}

```

This returns the entry with service\_id "this-is-my-foo". However, if I add a sort to my query:

```auto
POST /index/_search
{
  "query": {
    "match": {
      "service_id": "this-is-my-foo"
    }
  },
  "sort": { "timestamp" : "desc" },
  "size": 1
}

```

it returns the entry with service\_id "this-is-my-bar" (which does have a later timestamp, but I'm asking for entries that match service\_id "this-is-my-foo").

The unexpected behavior does not happen if the "service\_id"s are just "foo" and "bar", but it does happen if they have a common string at the start (which is also the case in my real data).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 2, 2020, 9:22am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635/4 "2020-03-02T09:22:50Z")

</div>

Try this:

```auto
GET /index/_search
{
  "query": {
    "match": {
      "service_id.keyword": "this-is-my-foo"
    }
  },
  "sort": { "timestamp" : "desc" },
  "size": 1
}

```

---

<div class="post-metadata">

**Author:** ![mbozic00](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@mbozic00](https://discuss.elastic.co/u/mbozic00)\
**Post date:** [March 2, 2020, 9:38am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635/5 "2020-03-02T09:38:28Z")

</div>

Thanks a lot, it worked. In the meantime I found another solution myself:

```auto
GET /index/_search
{
  "query": {
    "match": {
      "service_id": {
        "query": "this-is-my-foo",
        "minimum_should_match": "100%"
      }
    }
  },
  "sort": { "timestamp": "desc" },
  "size": 1
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 2, 2020, 10:04am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635/6 "2020-03-02T10:04:41Z")

</div>

I'd not do that. Instead I'd use the right mapping for your use case.

---

<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:** [March 30, 2020, 10:04am UTC](https://discuss.elastic.co/t/get-the-most-recent-record-which-matches-a-field/221635/7 "2020-03-30T10:04:45Z")

</div>

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