# Sort the Elasticsearch results based on the matched nested object in search-ui

**URL:** <https://discuss.elastic.co/t/sort-the-elasticsearch-results-based-on-the-matched-nested-object-in-search-ui/344752>\
**Category:** Elasticsearch\
**Created:** [October 10, 2023, 3:14pm UTC](https://discuss.elastic.co/t/sort-the-elasticsearch-results-based-on-the-matched-nested-object-in-search-ui/344752 "2023-10-10T15:14:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rommelcanoy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rommelcanoy/32/126425_2.png) [@rommelcanoy](https://discuss.elastic.co/u/rommelcanoy)\
**Post date:** [October 10, 2023, 3:14pm UTC](https://discuss.elastic.co/t/sort-the-elasticsearch-results-based-on-the-matched-nested-object-in-search-ui/344752/1 "2023-10-10T15:14:02Z")

</div>

I want to implement it in Search-UI. How should I do it? For instance, if users search for 'Mucopolysaccharidosis Type 1,' I want to sort the results based on the "score" of the matched nested condition inside this field 'medifind\_condition\_scores.' I have this query working in the Kibana DevTools.

Working query:

```auto
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "medifind_conditions": "Mucopolysaccharidosis Type 1"
        }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "double score = 0; for (item in params['_source']['medifind_condition_scores']) { if (item['condition'] == params.condition) { score = item['score']; break; } } return score;",
              "params": {
                "condition": "Mucopolysaccharidosis Type 1"
              }
            }
          }
        }
      ],
      "boost_mode": "replace"
    }
  }
}

```

This field, 'medifind\_condition\_scores,' will be present in each result. Sometimes, other results may not have this field.

```auto
 "medifind_condition_scores": [
 {
           "score": 96.99005856745995,
           "condition": "Mucopolysaccharidosis Type 1"
 },
{
           "score": 95.92013152310234,
           "condition": "Mucopolysaccharidoses"
},

```

Essentially, I want to implement the working query above in Search-UI, but I don't know how to do it. Or, is it possible to make this sorting in Search-UI itself?

---

<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:** [November 7, 2023, 3:14pm UTC](https://discuss.elastic.co/t/sort-the-elasticsearch-results-based-on-the-matched-nested-object-in-search-ui/344752/2 "2023-11-07T15:14:50Z")

</div>

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