# Highlight highlight fields that are not matched by the search

**URL:** <https://discuss.elastic.co/t/highlight-highlight-fields-that-are-not-matched-by-the-search/365293>\
**Category:** Elastic Search\
**Created:** [August 21, 2024, 3:06pm UTC](https://discuss.elastic.co/t/highlight-highlight-fields-that-are-not-matched-by-the-search/365293 "2024-08-21T15:06:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheFireCookie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thefirecookie/32/6839_2.png) [@TheFireCookie](https://discuss.elastic.co/u/TheFireCookie)\
**Post date:** [August 21, 2024, 3:06pm UTC](https://discuss.elastic.co/t/highlight-highlight-fields-that-are-not-matched-by-the-search/365293/1 "2024-08-21T15:06:22Z")

</div>

Hello,

I'm trying to have the highlight work on a nested object but I cannot seems to find a way that match my needs.

Elasticsearch version : 8.13.1

So let me show you this sample:  
My index:

```auto
{
    "mappings": {
        "dynamic": "false",
        "properties": {
            "nestedValueSample": {
                "type": "nested",
                "properties": {
                    "key": {
                        "type": "keyword"
                    },
                    "value": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

```

The three documents that I pushed:

id 1

```auto
{
    "nestedValueSample": [{
            "key": "key1",
            "value": "Hello from the first document, key 1"
        }, {
            "key": "key2",
            "value": "A value for key 2"
        }
    ]
}

```

id 2

```auto
{
    "nestedValueSample": [{
            "key": "key1",
            "value": "A value for key 1"
        }, {
            "key": "key2",
            "value": "Hello from the second document, key 2"
        }
    ]
}

```

id 3

```auto
{
    "nestedValueSample": [{
            "key": "key1",
            "value": "Hello for the third document, key 1"
        }, {
            "key": "key2",
            "value": "Hello from the third document, key 2"
        }
    ]
}

```

The highlight search that I do:

```auto
{
    "highlight": {
        "fields": {
            "nestedValueSample.value": {}
        }
    },
    "query": {
        "nested": {
            "path": "nestedValueSample",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "nestedValueSample.key": {
                                    "query": "key1"
                                }
                            }
                        },
                        {
                            "match": {
                                "nestedValueSample.value": {
                                    "query": "hello"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

```

And the answer from Elasticsearch

```auto
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.116739,
        "hits": [
            {
                "_index": "highlight-behavior-mme",
                "_id": "1",
                "_score": 1.116739,
                "_source": {
                    "nestedValueSample": [
                        {
                            "key": "key1",
                            "value": "Hello from the first document, key 1"
                        },
                        {
                            "key": "key2",
                            "value": "A value for key 2"
                        }
                    ]
                },
                "highlight": {
                    "nestedValueSample.value": [
                        "<em>Hello</em> from the first document, key 1"
                    ]
                }
            },
            {
                "_index": "highlight-behavior-mme",
                "_id": "3",
                "_score": 1.116739,
                "_source": {
                    "nestedValueSample": [
                        {
                            "key": "key1",
                            "value": "Hello for the third document, key 1"
                        },
                        {
                            "key": "key2",
                            "value": "Hello from the third document, key 2"
                        }
                    ]
                },
                "highlight": {
                    "nestedValueSample.value": [
                        "<em>Hello</em> for the third document, key 1",
                        "<em>Hello</em> from the third document, key 2"
                    ]
                }
            }
        ]
    }
}

```

So everything seems correct except that I would except the highlight to only return for the document 3 the value for the key1 as the search query is searching on "key"="key1" and "value" match "hello"  
So it seems like the key part of the search is ignored for the highlight.

Is there a way to work around that?

Thanks a lot,  
Matthias.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [August 21, 2024, 11:17pm UTC](https://discuss.elastic.co/t/highlight-highlight-fields-that-are-not-matched-by-the-search/365293/2 "2024-08-21T23:17:01Z")

</div>

Hi @TheFireCookie

Try apply highlight inside [inner hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html):

```auto
      "inner_hits": {
        "highlight": {
          "fields": {
            "nestedValueSample.value": {}
          }
        }
      }

```

---

<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, 2024, 11:17pm UTC](https://discuss.elastic.co/t/highlight-highlight-fields-that-are-not-matched-by-the-search/365293/3 "2024-09-18T23:17:07Z")

</div>

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