# How to do match multiple nested object in one document with inner hits highlighted in elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-do-match-multiple-nested-object-in-one-document-with-inner-hits-highlighted-in-elasticsearch/258842>\
**Category:** Elasticsearch\
**Tags:** docker\
**Created:** [December 16, 2020, 10:42am UTC](https://discuss.elastic.co/t/how-to-do-match-multiple-nested-object-in-one-document-with-inner-hits-highlighted-in-elasticsearch/258842 "2020-12-16T10:42:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Code\_Wizard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/code_wizard/32/78058_2.png) [@Code\_Wizard](https://discuss.elastic.co/u/Code_Wizard)\
**Post date:** [December 16, 2020, 10:42am UTC](https://discuss.elastic.co/t/how-to-do-match-multiple-nested-object-in-one-document-with-inner-hits-highlighted-in-elasticsearch/258842/1 "2020-12-16T10:42:07Z")

</div>

How do I write a query with two search terms which matches nested objects with inner hits highlighted.

Below is the sample usecase:

I have a mapping:

```auto
"mappings": {
      "properties": {
        "grocery_name": {
          "type": "text"
        },
        "items": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "text"
            },
            "stock": {
              "type": "integer"
            },
            "category": {
              "type": "text"
            }
          }
        }
      }
    }

```

and the data looks like below

```auto
  "grocery_name": "Elastic Eats",
  "items": [
    {
      "name": "Red banana",
      "stock": "12",
      "category": "fruit"
    },
    {
      "name": "Cavendish banana",
      "stock": "10",
      "category": "fruit"
    },
    {
      "name": "peach",
      "stock": "10",
      "category": "fruit"
    },
    {
      "name": "carrot",
      "stock": "9",
      "category": "vegetable"
    },
    {
      "name": "broccoli",
      "stock": "5",
      "category": "vegetable"
    }
  ]
}

```

here if i want a document which has _ **peach** _ and _ **carrot** _ both in nested docs i can do search with multiple nested queries like below

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "items",
            "query": {
              "match_phrase": {
                "items.name": {
                  "query": "carrot"
                }
              }
            }
          }
        },
        {
          "nested": {
            "path": "items",
            "query": {
              "match_phrase": {
                "items.name": {
                  "query": "peach"
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

the above query works perfectly, but if i add inner hits i can't add for both nested queries , if i do i get following error

> [inner\_hits] already contains an entry for key[items]

i want to get each matched nested object highlighted, as **\_source** highlighting is not supported in elasticsearch is there any way i can do highlighting for each nested object?

---

<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:** [January 13, 2021, 10:42am UTC](https://discuss.elastic.co/t/how-to-do-match-multiple-nested-object-in-one-document-with-inner-hits-highlighted-in-elasticsearch/258842/2 "2021-01-13T10:42:20Z")

</div>

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