如何在Elasticsearch中对去HTML标签的结果实现高亮?
Elasticsearch 去HTML标签后内容高亮问题解决
问题背景
已实现Elasticsearch搜索结果去除HTML标签,但对去标签后的内容执行高亮时,当前高亮字段仍保留原HTML内容,需调整实现目标效果。
现有映射与设置
{ "settings": { "analysis": { "filter": { "my_pattern_replace_filter": { "type": "pattern_replace", "pattern": "\n", "replacement": "" } }, "analyzer": { "my_analyzer": { "tokenizer": "standard", "filter": [ "lowercase" ], "char_filter": [ "html_strip" ] }, "parsed_analyzer": { "type": "custom", "tokenizer": "keyword", "char_filter": [ "html_strip" ], "filter": [ "my_pattern_replace_filter" ] } } } }, "mappings": { "properties": { "html": { "type": "text", "analyzer": "my_analyzer", "fields": { "raw": { "type": "text", "fielddata": true, "analyzer": "parsed_analyzer" } } } } } }
当前搜索查询
POST idx_test/_search { "script_fields": { "raw": { "script": "doc['html.raw']" } }, "query": { "match": { "html": "more" } }, "highlight": { "fields": { "*":{ "pre_tags" : ["<strong>"], "post_tags" : ["</strong>"] } } } }
当前返回结果
"hits": [ { "_index": "idx_test2", "_type": "_doc", "_id": "GijDsYMBjgX3UBaguGxc", "_score": 0.2876821, "fields": { "raw": [ "Test More test" ] }, "highlight": { "html": [ "<html><body><h1=\"font-family: Arial\">Test</h1> <span><strong>More</strong> test</span></body></html>" ] } } ]
期望返回结果
"hits": [ { "_index": "idx_test2", "_type": "_doc", "_id": "GijDsYMBjgX3UBaguGxc", "_score": 0.2876821, "fields": { "raw": [ "Test <strong>More</strong> test" ] } } ]
解决方案
方案1:修改搜索查询,针对处理后字段高亮
直接在高亮配置中指定html.raw字段(已去除HTML标签),并调整参数返回完整高亮内容:
POST idx_test/_search { "query": { "match": { "html": "more" } }, "highlight": { "fields": { "html.raw": { "pre_tags": ["<strong>"], "post_tags": ["</strong>"], "number_of_fragments": 0 } } } }
返回结果示例(符合期望的高亮效果):
"hits": [ { "_index": "idx_test2", "_type": "_doc", "_id": "GijDsYMBjgX3UBaguGxc", "_score": 0.2876821, "highlight": { "html.raw": [ "Test <strong>More</strong> test" ] } } ]
方案2:调整映射,让原字段高亮使用去标签分析器
在映射中为html字段配置高亮使用parsed_analyzer,这样无需指定具体字段即可实现去标签后高亮:
修改后的映射片段:
"html": { "type": "text", "analyzer": "my_analyzer", "highlight": { "analyzer": "parsed_analyzer" }, "fields": { "raw": { "type": "text", "fielddata": true, "analyzer": "parsed_analyzer" } } }
使用原搜索查询即可得到去标签后的高亮结果。
内容的提问来源于stack exchange,提问作者user17047040
相关产品推荐
相关产品推荐

