# How to get item highest matching rate in nested data!

**URL:** <https://discuss.elastic.co/t/how-to-get-item-highest-matching-rate-in-nested-data/195840>\
**Category:** Elasticsearch\
**Created:** [August 20, 2019, 4:32am UTC](https://discuss.elastic.co/t/how-to-get-item-highest-matching-rate-in-nested-data/195840 "2019-08-20T04:32:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vu\_Thien](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vu_thien/32/52583_2.png) [@Vu\_Thien](https://discuss.elastic.co/u/Vu_Thien)\
**Post date:** [August 20, 2019, 4:32am UTC](https://discuss.elastic.co/t/how-to-get-item-highest-matching-rate-in-nested-data/195840/1 "2019-08-20T04:32:30Z")

</div>

Hi, I have an index with nested data.

```auto
[
  {
    "shop": {
      "name": "The shop",
      "products": [
        {
          "name": "the product 1",
          "price": 1000
        },
        {
          "name": "product 2",
          "price": 1000
        }
      ]
    }
  },
  {
    "shop": {
      "name": "The shop 2",
      "products": [
        {
          "name": "shop 2 product 1",
          "price": 1000
        },
        {
          "name": "shop 2 product 2",
          "price": 1000
        }
      ]
    }
  }
]

```

I query nested data with this query:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "products",
            "query": {
              "multi_match": {
                "query": "the product",
                "fields": [
                  "products.name"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

And get a response like that

```auto
{
  "hits": [
    {
      "_index": "shops",
      "_score": 223.1123,
      "_source": {
        "name": "The shop",
        "products": [
          {
            "name": "the product 1",
            "price": 1000
          },
          {
            "name": "product 2",
            "price": 1000
          }
        ]
      }
    },
    {
      "_index": "shops",
      "_score": 203.1123,
      "_source": {
        "name": "The shop 2",
        "products": [
          {
            "name": "shop 2 product 1",
            "price": 1000
          },
          {
            "name": "shop 2 product 2",
            "price": 1000
          }
        ]
      }
    }
  ]
}

```

Currently, I don't `the product 1` and `product 2` which one has a higher score.  
It's possible in elasticseach to response percent matching for each field or something like that.  
Thank you!

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 20, 2019, 10:51am UTC](https://discuss.elastic.co/t/how-to-get-item-highest-matching-rate-in-nested-data/195840/2 "2019-08-20T10:51:37Z")

</div>

Yes, you can add [`inner_hits`](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-request-body.html#request-body-search-inner-hits) to your `nested` query. This will by default return you the top three best matching nested objects and their scores:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "products",
            "query": {
              "multi_match": {
                "query": "the product",
                "fields": [
                  "products.name"
                ]
              }
            },
            "inner_hits": {
            }
          }
        }
      ]
    }
  }
}

```

Btw, that `bool` query with just a single `must` clause does not do anything. The following query will return the exact same results:

```auto
{
  "query": {
    "nested": {
      "path": "products",
      "query": {
        "multi_match": {
          "query": "the product",
          "fields": [
            "products.name"
          ]
        }
      },
      "inner_hits": {}
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Vu\_Thien](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vu_thien/32/52583_2.png) [@Vu\_Thien](https://discuss.elastic.co/u/Vu_Thien)\
**Post date:** [August 21, 2019, 2:01am UTC](https://discuss.elastic.co/t/how-to-get-item-highest-matching-rate-in-nested-data/195840/3 "2019-08-21T02:01:36Z")

</div>

Thank you so much 🤤

---

<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:** [September 18, 2019, 2:01am UTC](https://discuss.elastic.co/t/how-to-get-item-highest-matching-rate-in-nested-data/195840/4 "2019-09-18T02:01:48Z")

</div>

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