一尘不染

在Elasticsearch中突出显示单词的一部分

elasticsearch

我已经使用n-
gram标记器在elasticsearch中提出了自动建议。现在,我想在自动建议列表中突出显示用户输入的字符序列。为此,我使用了elasticsearch中可用的荧光笔,我的代码如下所示,但是在输出中,完整的术语被突出显示了我要去哪里了。

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

结果是

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

映射

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

设定

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

如何突出显示软件而不是软件开发。


阅读 259

收藏
2020-06-22

共1个答案

一尘不染

在这种情况下,应使用ngram标记器而不是ngram过滤器突出显示。 with_positions_offsets需要帮助更快地突出显示。

这是可行的设置和映射:

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

映射

"competency":{
    "properties": {
        "competency_display_name":{
            "type":"string",
            "index_analyzer": "index_ngram_analyzer",
            "search_analyzer": "search_term_analyzer",
            "term_vector":"with_positions_offsets" 
        }
    }
}
2020-06-22