Highlight highlight fields that are not matched by the search

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:

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

The three documents that I pushed:

id 1

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

id 2

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

id 3

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

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

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

Hi @TheFireCookie

Try apply highlight inside inner hits:

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

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