Home > Software design >  How to return Highlighting tags in-line within the results using elastic search 7
How to return Highlighting tags in-line within the results using elastic search 7

Time:10-22

I am using Spring Data Elastic Search to build a search service in Spring Boot in front of a cluster with Elastic Search 7.15. I am working on highlighting, however I can see in the documentation just the option of returning the highlighted fields in a separate section as shown below:

   "hits": [
  {
    "_index": "my-index-000001",
    "_type": "_doc",
    "_id": "1",
    "_score": 1.6011951,
    "_source": {
      "message": "some message with the number 1",
      "context": "bar"
    },
    "highlight": {
      "message": [
        " with the <em>number</em>",
        " <em>1</em>"
      ]
    }
  }
]}

What I would like instead is returning the highlighting tags in-line, for instance:

   "hits": [
  {
    "_index": "my-index-000001",
    "_type": "_doc",
    "_id": "1",
    "_score": 1.6011951,
    "_source": {
     "message": [
        " with the <em>number</em>",
        " <em>1</em>"
      ]  ,
      "context": "bar"
    }
  }
] }

Is there any configuration that I could change in order to return the highlighting tags in the results instead?

CodePudding user response:

from the docs:

The _source field contains the original JSON document body that was passed at index time.

Elasticsearch will not return anything different in _source than what was passed in. And to be honest - I don't see why you want to do that, you would loose the information what you have stored in the document in the returned answer.

Besides that the returned highlights are an array of Strings, the field in the _source was a String. How should that be mapped to an entity that expectes a String in the message property?

  • Related