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

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:

{
  "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

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:

Hope that helps!