Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting part of word in elasticsearch

I have made a auto-suggester in elastic search using n-gram tokenizer. Now I want to highlight the user entered character sequence in the auto suggest list. For this purpose I used the highlighter available in elastic search my code is as below but in the output the complete term is being highlighted where am I going wrong.

{
    "query": {
        "query_string": {
            "query": "soft",
            "default_field": "competency_display_name"
        }
    },
    "highlight": {
        "pre_tags": ["<b>"],
        "post_tags": ["</b>"],
        "fields": {
            "competency_display_name": {}
        }
    }
}

and the result is

{
   "took": 8,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "competency_auto_suggest",
            "_type": "competency",
            "_id": "4",
            "_score": 1,
            "_source": {
               "review": null,
               "competency_title": "Software Development",
               "id": 4,
               "competency_display_name": "Software Development"
            },
            "highlight": {
               "competency_display_name": [
                  "<b>Software Development</b>"
               ]
            }
         }
      ]
   }
}

mapping

"competency":{
    "properties": {
        "competency_display_name":{
            "type":"string",
            "index_analyzer": "index_ngram_analyzer",
            "search_analyzer": "search_term_analyzer"
        }
    }
}

settings

"analysis": {
    "filter": {
        "ngram_tokenizer": {
            "type": "nGram",
            "min_gram": "1",
            "max_gram": "15",
            "token_chars": [ "letter", "digit" ]
        }
    },
    "analyzer": {
        "index_ngram_analyzer": {
            "type": "custom",
            "tokenizer": "keyword",
            "filter": [ "ngram_tokenizer", "lowercase" ]
        },
        "search_term_analyzer": {
            "type": "custom",
            "tokenizer": "keyword",
            "filter": "lowercase" 
        }
    }
}

how to highlight Soft instead of Software Development.

like image 308
Sar009 Avatar asked Jan 23 '14 14:01

Sar009


1 Answers

You should use ngram tokenizer instead of ngram filter to highlight in this case. with_positions_offsets is needed to help highlighting more faster.

Here's the workable settings & mapping :

"analysis": {
    "tokenizer": {
        "ngram_tokenizer": {
            "type": "nGram",
            "min_gram": "1",
            "max_gram": "15",
            "token_chars": [ "letter", "digit" ]
        }
    },
    "analyzer": {
        "index_ngram_analyzer": {
            "type": "custom",
            "tokenizer": "ngram_tokenizer",
            "filter": [ "lowercase" ]
        },
        "search_term_analyzer": {
            "type": "custom",
            "tokenizer": "keyword",
            "filter": "lowercase" 
        }
    }
}

mapping

"competency":{
    "properties": {
        "competency_display_name":{
            "type":"string",
            "index_analyzer": "index_ngram_analyzer",
            "search_analyzer": "search_term_analyzer",
            "term_vector":"with_positions_offsets" 
        }
    }
}
like image 100
Duc.Duong Avatar answered Oct 07 '22 17:10

Duc.Duong