Elastic search Highlight in nested and normal object

Hi

I have created index and documents in the elastic search by using following calls:

PUT test
{
  "mappings": {
    "documents": {
      "properties": {
        "fulltext": {
          "type": "text"
        },
        "title": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "keyword"
            }
          }
        },
        "fields": {
          "type": "nested",
          "properties": {
            "uid": {
              "type": "keyword"
            },
            "value": {
              "type": "text",
              "copy_to": "fulltext"
            }
          }
        }
      }
    }
  }
}

PUT test/documents/1
{
  "title": "hello world",
  "fields": [
    {
      "uid": "fname",
      "value": "My first name is john"
    },
    {
      "uid": "lname",
      "value": "My last name is test"
    }
  ]
}

PUT test/documents/2
{
  "title": "java demo",
  "fields": [
    {
      "uid": "fname",
      "value": "My first name is Ritesh"
    },
    {
      "uid": "lname",
      "value": "My last name is testing"
    }
  ]
}

Now I am using highlight query to highlight matching text.by using following way:

POST test/_search
{
  "query": {
    "simple_query_string": {
      "query": "john"
    }
  },
 "highlight" : {
    "fields" : {
        "*" : {}
    }
 }
}

Above request giving me following response:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test",
        "_type": "documents",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "hello world",
          "fields": [
            {
              "uid": "fname",
              "value": "My first name is john"
            },
            {
              "uid": "lname",
              "value": "My last name is test"
            }
          ]
        },
        "highlight": {
          "fields.value": [
            "My first name is <em>john</em>"
          ]
        }
      }
    ]
  }
}

But my question is "Can we highlight fields.uid also (in this case fname) ?" by using above query
It helps me to understand search results belongs to which uid(fields.uid)
is any solution for this please help me into this?

Highlighter by default highlights only terms matching the search query. In your case it will be only "john". It can't highlight "fname", if you are not search for "fname" if your query.

If you want the highlighter highlight terms different from your search query, you can use highlight_query option.

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