跟着这篇走完,你会把 OpenSearch 搜索结果「命中词高亮」这件事配齐:用 highlight 把命中词包上 <em>、用 fragment_size 控片段长度、用 number_of_fragments 控片段条数、用 pre_tags/post_tags 换自定义标签,最后把长文本裁成搜索结果页上那几行带高亮的摘要。整个过程 6 步,每步一条验证命令。环境沿用从零跑通 OpenSearch 本地搜索服务:端口 9200、用户 admin、密码 Devfix@2026;你换了值替换掉就行。
先说清一个贯穿整篇的认知:highlight 不改你的数据。它和 Meilisearch 的 _formatted 副本是一个道理——高亮结果不写回索引,而是引擎在返回体的每个 hit 里额外放一个 highlight 对象,原始 _source 原样保留。前端渲染高亮读 highlight,存数据读 _source。这篇默认你已经跑通了本地搜索服务,Docker 启动、建索引、bulk 导入、curl 带 -sk -u admin:Devfix@2026 的写法全部复用,不再重复。如果你玩过从零跑通 Meilisearch 高亮与结果展示,这篇是对称篇:同一个问题,Meilisearch 用搜索参数 attributesToHighlight,OpenSearch 用查询 DSL 里的 highlight 块。

前置条件
| 项 | 要求 | 说明 |
|---|---|---|
| OpenSearch | 3.x(Docker latest 镜像) | 单节点,security 开启,HTTPS 端口 9200 |
| 前置文章 | 已跑通 opensearch-local-search | 复用容器、密码、curl 写法 |
| curl | 任意版本 | 全文用 curl 调 REST API |
先确认服务活着:
curl -sk -u admin:Devfix@2026 "https://localhost:9200"
返回里有 cluster_name 就满足条件。报连接失败说明容器没起来,回第一篇第二步。
第一步:建 movies_hl 索引,灌带长 synopsis 的 5 条电影
高亮要演示「长文本裁成片段」,所以先得有长文本。建一个 movies_hl 索引,synopsis 是 text 类型、值是一两百字符的剧情简介(这是演示 fragment_size 的前提):
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_hl?pretty" \
-H "Content-Type: application/json" \
-d '{
"settings": { "index.number_of_shards": 1, "index.number_of_replicas": 0 },
"mappings": {
"properties": {
"title": { "type": "text" },
"year": { "type": "integer" },
"genre": { "type": "keyword" },
"synopsis": { "type": "text" }
}
}
}'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "movies_hl"
}
灌 5 条电影,synopsis 刻意写得够长(超过默认片段长度 100 字符),第三步才看得出裁剪效果:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_hl/_bulk?pretty" \
-H "Content-Type: application/x-ndjson" \
--data-binary '{"index":{"_id":"1"}}
{"title":"Let the Wind Rise","year":2013,"genre":"drama","synopsis":"A young woman discovers she can control the wind during a devastating storm that threatens her coastal hometown. As the winds grow stronger with each passing day, she must learn to master her newfound power before the hurricane destroys everything she loves."}
{"index":{"_id":"2"}}
{"title":"Gone with the Wind","year":1939,"genre":"drama","synopsis":"Set against the backdrop of the American Civil War, this sweeping epic follows Scarlett O Hara as she navigates love, loss, and survival. From the burning of Atlanta to the rebuilding of Tara, her story spans years of hardship and quiet resilience."}
{"index":{"_id":"3"}}
{"title":"The Rise of Machines","year":2024,"genre":"sci-fi","synopsis":"In a near future where robots serve every human need, a rogue artificial intelligence begins taking over the global network. A small team of engineers races against the clock to shut it down before every machine on Earth turns against its creators."}
{"index":{"_id":"4"}}
{"title":"Windmill Valley","year":2020,"genre":"drama","synopsis":"A quiet farming community has relied on its windmills for generations, but when a mysterious corporation buys up the surrounding land, the villagers must fight back. One farmer daughter leads a stubborn resistance to save their way of life."}
{"index":{"_id":"5"}}
{"title":"The Martian","year":2015,"genre":"sci-fi","synopsis":"Stranded alone on Mars after a mission goes catastrophically wrong, an astronaut must draw on his ingenuity and a dwindling supply of resources to stay alive. Meanwhile, NASA scrambles to bring him home before the harsh red planet claims him."}
'
{
"errors" : false,
"items" : [ { "index" : { "_id" : "1", "status" : 201 } }, ... ]
}
errors 是 false、5 条都 201 就进去了。
验证命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_hl/_count?pretty"
{
"count" : 5,
"_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }
}
返回 "count": 5 就通过。
第二步:基本高亮——看 highlight 返回结构长什么样
先做最简单的:搜 title 里的 wind,高亮 title。highlight 是搜索请求体的顶层参数,和 query 平级(不是 query 的子字段):
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } }, "highlight": { "fields": { "title": {} } } }'
{
"hits" : {
"total" : { "value" : 2, "relation" : "eq" },
"hits" : [
{
"_id" : "1",
"_score" : 0.63,
"_source" : { "title" : "Let the Wind Rise", "year" : 2013, "genre" : "drama" },
"highlight" : {
"title" : [ "Let the <em>Wind</em> Rise" ]
}
},
{
"_id" : "2",
"_score" : 0.63,
"_source" : { "title" : "Gone with the Wind", "year" : 1939, "genre" : "drama" },
"highlight" : {
"title" : [ "Gone with the <em>Wind</em>" ]
}
}
]
}
}
三个观察点,是后面的伏笔:
- 多了个
highlight对象。命中词Wind被<em>/</em>包住,放在highlight.title里。 highlight.title是数组,不是字符串——因为一个字段可能被切成多个片段(第三步就看到)。- 原始
_source.title没动。_source.title还是干净的Let the Wind Rise,不带标签。
所以前端渲染标题高亮读 hit.highlight.title[0],存数据读 hit._source.title,两处各司其职。
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } }, "highlight": { "fields": { "title": {} } } }' \
| grep -o '<em>Wind</em>'
能 grep 到 <em>Wind</em> 就通过。
第三步:fragment_size + number_of_fragments 控制片段(承重步)
这是全文承重的一步。title 短,一句话就高亮完了;但 synopsis 有一两百字符,搜索结果页只要几行摘要。这时两个参数上场:fragment_size 定每个片段多少字符,number_of_fragments 定返回几条片段。
先搜 synopsis 里的 wind,高亮 synopsis,用 fragment_size: 60、number_of_fragments: 2:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "synopsis": "wind" } }, "highlight": { "fields": { "synopsis": { "fragment_size": 60, "number_of_fragments": 2 } } } }'
{
"hits" : {
"hits" : [
{
"_id" : "1",
"_source" : { "title" : "Let the Wind Rise" },
"highlight" : {
"synopsis" : [
"A young woman discovers she can control the <em>wind</em> during a devastating storm",
"that threatens her coastal hometown. As the <em>wind</em>s grow stronger with each"
]
}
}
]
}
}
逐段说清两个参数:
fragment_size是字符数,不是词数。60就是每个片段约 60 个字符。这是和 Meilisearch 的cropLength(词数)最不一样的地方——OpenSearch 按字符,Meilisearch 按词。两个引擎同一件事,单位不同,见常见问题第 2 项。number_of_fragments是片段条数。2就是最多返回 2 段,按相关度挑最相关的 2 段(不是从头截前两段)。- 高亮器按句子边界切。默认
unified高亮器尽量让片段落在完整句子上,所以fragment_size: 60出来的片段可能比 60 字符略长一点(要把句子收完整)。
再看一个「整字段」的用法:number_of_fragments: 0 表示返回整个字段的高亮全文(不是「0 条片段」),此时 fragment_size 被忽略:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "synopsis": "wind" } }, "highlight": { "fields": { "synopsis": { "number_of_fragments": 0 } } } }'
返回的 highlight.synopsis[0] 是整个 synopsis 的完整文本,所有 wind 都被 <em> 包住。
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "synopsis": "wind" } }, "highlight": { "fields": { "synopsis": { "fragment_size": 60, "number_of_fragments": 2 } } } }' \
| grep -o '<em>wind</em>'
能 grep 到 <em>wind</em> 就通过。
第四步:自定义标签 pre_tags / post_tags
默认 <em> 够用,但如果你用的是 <mark>、或想给高亮加 class,就换 pre_tags / post_tags(注意是数组):
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } }, "highlight": { "pre_tags": [ "<mark>" ], "post_tags": [ "</mark>" ], "fields": { "title": {} } } }'
{
"highlight" : {
"title" : [ "Let the <mark>Wind</mark> Rise" ]
}
}
<em> 换成了 <mark>。pre_tags/post_tags 是顶层 highlight 的参数(对 fields 里所有字段生效),也可以放进单个字段里只对那个字段生效。两个标签要成对换,只换一半会产出没闭合的 HTML。
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } }, "highlight": { "pre_tags": [ "<mark>" ], "post_tags": [ "</mark>" ], "fields": { "title": {} } } }' \
| grep -o '<mark>Wind</mark>'
能 grep 到 <mark>Wind</mark> 就通过。
第五步:require_field_match——控制「只高亮查询匹配的字段」
默认 require_field_match: true,意思是「只高亮查询实际匹配到的字段」。搜 title 里的 wind,却想同时高亮 synopsis,默认会失败(synopsis 没被 match 查询匹配到):
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } }, "highlight": { "fields": { "synopsis": {} } } }'
这条返回的 hit 里没有 highlight.synopsis——因为查询只匹配了 title,synopsis 没被匹配,默认不高亮它。这正是新手常踩的「我明明列了 synopsis 怎么没高亮」,见常见问题第 1 项。
要跨字段高亮,关掉它:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } }, "highlight": { "require_field_match": false, "fields": { "synopsis": {} } } }'
现在 synopsis 也会被高亮——即使查询没直接匹配它,只要字段里出现了 wind 这个词就会被包标签。
验证命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } }, "highlight": { "require_field_match": false, "fields": { "synopsis": {} } } }' \
| grep -o '"synopsis"'
能 grep 到 "synopsis"(说明 synopsis 的高亮出现了)就通过。
第六步:组合——一个完整的高亮请求 + 收场
把前面的参数拼起来:搜 wind、高亮 title 和 synopsis 两个字段、synopsis 裁成 2 段各 80 字符、用 <mark> 标签:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_hl/_search?pretty" \
-H "Content-Type: application/json" \
-d '{
"query": { "match": { "title": "wind" } },
"highlight": {
"pre_tags": [ "<mark>" ],
"post_tags": [ "</mark>" ],
"require_field_match": false,
"fields": {
"title": {},
"synopsis": { "fragment_size": 80, "number_of_fragments": 2 }
}
}
}'
现在每个 hit 的 highlight 里同时有 title(短文本整段高亮)和 synopsis(长文本裁成 2 段带高亮),这正是搜索结果页「标题高亮 + 简介摘要」的完整形态。
收个尾:highlight 是纯查询期的行为,索引里的数据一个字没动,也无需 reindex——这点和改 mapping、改分析器不同。删除演示索引即可收场:
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/movies_hl?pretty"
{
"acknowledged" : true
}
核心概念
这张图把 highlight 的参数和返回结构的关系摆清楚:请求里 highlight 是顶层块,fields 列出要高亮的字段,fragment_size/number_of_fragments 控片段、pre_tags/post_tags 控标签、require_field_match 控是否跨字段;返回里每个 hit 多一个 highlight 对象(字段名 → 片段数组),原始 _source 不动。

| 参数 | 作用 | 默认值 | 类比 |
|---|---|---|---|
highlight |
搜索请求顶层块,声明要高亮的字段 | 无(不传不高亮) | 荧光笔的开关 |
fragment_size |
每个片段的字符数 | 100 | 每行摘要留多长 |
number_of_fragments |
返回几条片段;0 = 整个字段 |
5 | 摘要最多显示几行 |
pre_tags / post_tags |
高亮标签(数组) | <em> / </em> |
用什么笔色划 |
require_field_match |
是否只高亮查询匹配的字段 | true |
只在查到的字段上划,还是全篇找词划 |
type |
高亮器:unified / plain / fvh / semantic |
unified |
按句子切还是按长度硬切 |
要点回顾
highlight不改数据,结果在hits[].highlight——字段名 → 片段数组,原始_source原样保留,前端渲染读highlight、存数据读_source。fragment_size是字符数、number_of_fragments是片段条数——一个管长度、一个管数量,别搞反;number_of_fragments: 0返回整个字段全文。highlight要和query平级——塞进query里会被忽略,字段名也要和 mapping 完全一致。require_field_match默认true——只高亮查询实际匹配的字段,跨字段高亮要设false。
常见问题
传了 highlight,返回里没有 highlight 对象
现象:搜完看 hits[0],没有 highlight 对象,或 highlight 里没有 <em>。
原因:highlight 要和 query 平级(塞进 query 里会被忽略);字段名要和 mapping 一致;且 require_field_match 默认 true,只高亮查询实际匹配的字段,你高亮了一个查询没碰到的字段。
高亮片段长度和条数对不上
现象:设了 fragment_size 和 number_of_fragments,片段长度、条数和预期不符。
原因:fragment_size 是片段字符数、number_of_fragments 是片段条数,两者语义不同;number_of_fragments: 0 是返回整个字段,不是 0 条片段。
相关文章:OpenSearch 高亮片段不对:fragment_size 和 number_of_fragments 语义搞反
高亮某个字段报错或没结果
现象:高亮 keyword 字段报错或没 highlight;或 fvh 高亮器报 term vector 相关错误。
原因:高亮只能作用在 text 字段——keyword 字段不分词、没有词位置信息;fvh 高亮器还要求 term_vector: with_positions_offsets。
