高亮 - Highlight
POST /api/:target/_search
搜索的时候支持高亮文本
进行高亮查询需要几下几步:
- 启用索引中的 
highlightable - 在启用 
highlightable后创建文档 - 使用 
highlight查询数据 
开启高亮
首先,在索引的 mapping 中启用突出显示功能。
更新 mapping:
POST /api/:target/_mapping
{"properties": {"content": {"type": "text","index": true,"store": true,"highlightable": true}}}
或者
在创建索引的时候设置 mapping
POST /api/index
{"name": "article","storage_type": "disk","mappings": {"properties": {"content": {"type": "text","index": true,"store": true,"highlightable": true}}}}
请求
POST http://localhost:4080/api/stackoverflow-6/_search
请求体:
{"search_type": "match","query": {"term": "shell window",},"from": 0,"max_results": 20,"highlight": {"pre_tags": ["<pre>"],"post_tags": ["</pre>"],"fields": {"title": {"pre_tags": [],"post_tags": []},"content": {"pre_tags": [],"post_tags": []},}}}
pre_tags 和 post_tags 可以为空,默认为 <mark> 和 </mark>。
fields 是一个包含多个字段的映射,至少需要一个字段。
{"search_type": "match","query": {"term": "shell window",},"from": 0,"max_results": 20,"highlight": {"fields": {"content": {},}}}
