# How can I change my query from \_msearch to \_search using the bucket aggregation?

**URL:** <https://discuss.elastic.co/t/how-can-i-change-my-query-from-msearch-to-search-using-the-bucket-aggregation/309018>\
**Category:** Elasticsearch\
**Created:** [July 6, 2022, 1:30pm UTC](https://discuss.elastic.co/t/how-can-i-change-my-query-from-msearch-to-search-using-the-bucket-aggregation/309018 "2022-07-06T13:30:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![V\_o](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/v_o/32/102170_2.png) [@V\_o](https://discuss.elastic.co/u/V_o)\
**Post date:** [July 6, 2022, 1:30pm UTC](https://discuss.elastic.co/t/how-can-i-change-my-query-from-msearch-to-search-using-the-bucket-aggregation/309018/1 "2022-07-06T13:30:47Z")

</div>

Hello, I am new to Elasticsearch.

Does anybody have an idea on how to change this query from an  
currently \_msearch query to an \_search query? It would also be convenient if I didn't have to make a separate query for every "CAR" but instead solve it with only one query. I would like to use the bucket aggregation instead.  
This is my the query where I search for every Car separately:

```auto
GET /index/_msearch
{}
{"query": {"match": {"name": "CAR_RED"}},"size": 1,"sort": {"time":{"order": "desc"}}}
{}
{"query": {"match": {"name": "CAR_BLACK"}},"size": 1,"sort": {"time":{"order": "desc"}}}
{}
{"query": {"match": {"name": "CAR_WHITE"}},"size": 1,"sort": {"time":{"order": "desc"}}}

```

At the moment I am trying to solve it with the bucket aggregation but I always get an error.

```auto
GET /index/_search
{
  "size": 0,
    "aggs" : {
      "CARS":{
        "terms":{"field":"name.keyword"}
      },
      "bucket_sort": {
        "sort": [
        {"time":{"order": "desc"}}
        ],
        "size": 1
      }
    }
  }

```

It would be awesome if anyone could help me with this query.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [July 6, 2022, 3:50pm UTC](https://discuss.elastic.co/t/how-can-i-change-my-query-from-msearch-to-search-using-the-bucket-aggregation/309018/2 "2022-07-06T15:50:46Z")

</div>

Hi,

You need [top\_hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) as sub-aggregation for terms aggregation.

```auto
{
 	"size": 0,
   	"aggs" : {
		"CARS":{
			"terms":{
				"field":"name.keyword"
			},
			"aggs":{
				"latest":{
					"top_hits":{
						"sort":[
							{
								"time": {"order": "desc"}
							}
						],
						"size": 1
					}
				}
			}
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![V\_o](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/v_o/32/102170_2.png) [@V\_o](https://discuss.elastic.co/u/V_o)\
**Post date:** [July 6, 2022, 7:58pm UTC](https://discuss.elastic.co/t/how-can-i-change-my-query-from-msearch-to-search-using-the-bucket-aggregation/309018/3 "2022-07-06T19:58:37Z")

</div>

Thank you 🙂

---

<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:** [August 3, 2022, 7:58pm UTC](https://discuss.elastic.co/t/how-can-i-change-my-query-from-msearch-to-search-using-the-bucket-aggregation/309018/4 "2022-08-03T19:58:48Z")

</div>

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