# How to Select Top 1 Record and Filter Within Nested Objects in Elasticsearch Mapping?

**URL:** <https://discuss.elastic.co/t/how-to-select-top-1-record-and-filter-within-nested-objects-in-elasticsearch-mapping/356169>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [March 26, 2024, 11:10am UTC](https://discuss.elastic.co/t/how-to-select-top-1-record-and-filter-within-nested-objects-in-elasticsearch-mapping/356169 "2024-03-26T11:10:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soso\_Kapianidze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/soso_kapianidze/32/122538_2.png) [@Soso\_Kapianidze](https://discuss.elastic.co/u/Soso_Kapianidze)\
**Post date:** [March 26, 2024, 11:10am UTC](https://discuss.elastic.co/t/how-to-select-top-1-record-and-filter-within-nested-objects-in-elasticsearch-mapping/356169/1 "2024-03-26T11:10:48Z")

</div>

I'm working with an Elasticsearch index where I have a mapping that includes a nested field called `servicePrices` . Within this nested field, I'm trying to select the top 1 record based on a specific field such as `matches` and then filter the selected record based on another field such as `price` . Here's a simplified version of my mapping:

```auto
{
  "services": {
    "mappings": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "servicePrices": {
          "type": "nested",
          "properties": {
            "age": {
              "type": "float"
            },
            "gender": {
              "type": "float"
            },
            "matches": {
              "type": "float"
            },
            "price": {
              "type": "nested",
              "properties": {
                "amount": {
                  "type": "float"
                },
                "currency": {
                  "type": "keyword"
                }
              }
            }
            // Other nested properties...
          }
        }
      }
    }
  }
}

```

Could someone provide guidance on how to accomplish this selection, filtering, and inclusion of nested fields

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [April 2, 2024, 4:38pm UTC](https://discuss.elastic.co/t/how-to-select-top-1-record-and-filter-within-nested-objects-in-elasticsearch-mapping/356169/2 "2024-04-02T16:38:32Z")

</div>

Hi @Soso_Kapianidze :

If I understood correctly, you want to:

- Sort your results based on a nested field to get the top result
- Perform a search on the nested fields for selecting specific inner hits that match the search.

Both should be doable - please check the docs on:

- [Sorting nested objects](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#nested-sorting)
- [Nested inner hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html#nested-inner-hits) for retrieving the nested fields for a query

Hope that helps!

---

<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 30, 2024, 4:38pm UTC](https://discuss.elastic.co/t/how-to-select-top-1-record-and-filter-within-nested-objects-in-elasticsearch-mapping/356169/3 "2024-04-30T16:38:34Z")

</div>

Closed on April 30, 2024, 4:38pm UTC

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