# Sort nested array according to match on a multi match query

**URL:** <https://discuss.elastic.co/t/sort-nested-array-according-to-match-on-a-multi-match-query/59375>\
**Category:** Elasticsearch\
**Created:** [August 31, 2016, 12:18am UTC](https://discuss.elastic.co/t/sort-nested-array-according-to-match-on-a-multi-match-query/59375 "2016-08-31T00:18:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashwin-sureshkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashwin-sureshkumar/32/11700_2.png) [@ashwin-sureshkumar](https://discuss.elastic.co/u/ashwin-sureshkumar)\
**Post date:** [August 31, 2016, 12:18am UTC](https://discuss.elastic.co/t/sort-nested-array-according-to-match-on-a-multi-match-query/59375/1 "2016-08-31T00:18:54Z")

</div>

Hi Everyone,

I have a multi match query with below sample structure

```auto
{ 
   id : 1, 
  name : 'random name', 
  tags : [
     {
       id : 1 ,
       name : 'tag1',
     },
     {
       id : 2 ,
       name : 'tag2',
     },
     {
       id : 3 ,
       name : 'tag3',
     },
     {
       id : 4 ,
       name : 'tag34',
     }

  ]
}

```

I am search on `name, tag.name`, so when the search results match according to tags I want those tags array sorted according to matched results. So if I search `tag3`

The result should be

```auto
{ 
   id : 1, 
  name : 'random name', 
  tags : [
     {
       id : 4 ,
       name : 'tag34',
     },
     {
       id : 3 ,
       name : 'tag3',
     },
     {
       id : 1 ,
       name : 'tag1',
     },
     {
       id : 2 ,
       name : 'tag2',
     }
  ]
}

```

I have an UI to show first 2 matched tags or first 2 random tags in search results. If the tags are matched then matched tags are supposed to be shown.

Any help would be appreciated. Thanks in advance.

---

<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:** [July 5, 2017, 10:23pm UTC](https://discuss.elastic.co/t/sort-nested-array-according-to-match-on-a-multi-match-query/59375/2 "2017-07-05T22:23:49Z")

</div>


