简体   繁体   中英

Highlight in Elasticsearch

This is my elasticsearch query:

GET indexname/_search
{
  
    "fields": ["_id", "url","T"],
    "query" : {
     "bool": {"should": [
       {"simple_query_string": {
         "query": "white",
         "fields": ["T", "content"]
       }}
     ]}
    },
    "highlight" : {
      "pre_tags": ["<b>"], 
      "post_tags": ["</b>"], 
        "fields" : {
            "content" : {"fragment_size" : 150, "number_of_fragments" : 1}
        
        }
    }
}  

My elasticsearch query is searching for white in the fields "T" and "content", and I am highlighting the field "content" and inserting a pre and post tag b(bold). This is the result of my query

"hits": {
    "total": 922,
    "max_score": 2.369757,
    "hits": [
      {
        "_index": "indexname",
        "_type": "Searchtype",
        "_id": "http://www.example.com/de/unternehmenssuche-white-paper",
        "_score": 2.369757,
        "fields": {
          "T": [
            "White Paper Unternehmenssuche"
          ],
          "url": [
            "http://www.example.com/de/unternehmenssuche-white-paper"
          ]
        },
        "highlight": {
          "content": [
            "/Anwendungsbeispiele Ressourcen Blog <b>White</b> Papers in Deutsche Downloads Wiki Unternehmen Vorstellung der Search Executive"
          ]
        }
      }
....
...

I want my highlight result to look like this

"highlight": {
          "content": [
            "<b>...</b> /Anwendungsbeispiele Ressourcen Blog <b>White</b> Papers in Deutsche Downloads Wiki Unternehmen Vorstellung der Search Executive <b>...</b>"
          ]
        }

I want to add <b>...</b> before and after the highlight content. What should I add in my elasticsearch query to make results look like this?

As I stated in the comments I don't think this can be done in Elasticsearch. A highlighter just highlights the terms it matched and does no further postprocessing (And I found no evidence in the docs for Elasticsearch 2.3 that you could make it able to do so).

Anyway, my logical approach would be to add the <b>...</b> tags when you're rendering the HTML code.

{{ foreach hit in hits }}
<b>...</b> hit[content] <b>...</b>
{{ endfor }}

Something like this, just modify it to suit your template.

Use pre_tags and post_tags for this purpose. see configuring tags

GET /_search
{
    "query" : {
        "match": { "user": "kimchy" }
    },
    "highlight" : {
        "pre_tags" : ["<b>"],
        "post_tags" : ["</b>"],
        "fields" : {
            "content" : {}
        }
    }
}

Pre-Tags and Post-Tags will do in this regard.

Visit https://programs.wiki/wiki/elasticsearch-search-search-highlight-tag-customization.html for more customization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM