高亮与建议 #
一、高亮显示 #
1.1 高亮概述 #
高亮显示用于在搜索结果中标记匹配的关键词,提升用户体验。
1.2 基本高亮 #
bash
# 开启高亮
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content"
1.3 响应格式 #
json
{
"response": {
"docs": [
{
"id": "doc-001",
"content": "Solr是一个强大的搜索引擎"
}
]
},
"highlighting": {
"doc-001": {
"content": ["<em>Solr</em>是一个强大的搜索引擎"]
}
}
}
二、高亮参数 #
2.1 基本参数 #
| 参数 | 说明 | 默认值 |
|---|---|---|
| hl | 是否高亮 | false |
| hl.fl | 高亮字段 | 空 |
| hl.snippets | 片段数量 | 1 |
| hl.fragsize | 片段大小 | 100 |
| hl.simple.pre | 前缀 | <em> |
| hl.simple.post | 后缀 | </em> |
| hl.encoder | 编码方式 | 空 |
| hl.maxAnalyzedChars | 最大分析字符 | 51200 |
2.2 自定义标签 #
bash
# 自定义高亮标签
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content" \
-d "hl.simple.pre=<span class='hl'>" \
-d "hl.simple.post=</span>"
2.3 多片段 #
bash
# 返回多个片段
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content" \
-d "hl.snippets=3"
2.4 片段大小 #
bash
# 设置片段大小
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content" \
-d "hl.fragsize=200"
三、高亮器类型 #
3.1 Unified Highlighter(推荐) #
bash
# 使用Unified Highlighter
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content" \
-d "hl.method=unified"
3.2 Original Highlighter #
bash
# 使用Original Highlighter
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content" \
-d "hl.method=original"
3.3 FastVector Highlighter #
bash
# 使用FastVector Highlighter
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content" \
-d "hl.method=fastVector"
Schema配置
xml
<field name="content" type="text_general" indexed="true" stored="true"
termVectors="true" termPositions="true" termOffsets="true"/>
3.4 高亮器对比 #
| 高亮器 | 性能 | 功能 | 要求 |
|---|---|---|---|
| Unified | 高 | 完整 | 无 |
| Original | 中 | 基础 | 无 |
| FastVector | 高 | 高级 | termVectors |
四、高亮配置 #
4.1 solrconfig.xml配置 #
xml
<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
<bool name="hl">true</bool>
<str name="hl.fl">title,content</str>
<str name="hl.simple.pre"><![CDATA[<em>]]></str>
<str name="hl.simple.post"><![CDATA[</em>]]></str>
<int name="hl.snippets">3</int>
<int name="hl.fragsize">150</int>
</lst>
</requestHandler>
4.2 字段特定配置 #
bash
# 字段特定配置
curl "http://localhost:8983/solr/mycore/select" \
-d "q=Solr" \
-d "hl=true" \
-d "hl.fl=title,content" \
-d "f.title.hl.snippets=1" \
-d "f.content.hl.snippets=3" \
-d "f.content.hl.fragsize=200"
五、拼写检查 #
5.1 配置拼写检查 #
solrconfig.xml
xml
<searchComponent name="spellcheck" class="solr.SpellCheckComponent">
<str name="queryAnalyzerFieldType">text_general</str>
<lst name="spellchecker">
<str name="name">default</str>
<str name="field">text</str>
<str name="classname">solr.DirectSolrSpellChecker</str>
<str name="distanceMeasure">internal</str>
<float name="accuracy">0.5</float>
<int name="maxEdits">2</int>
<int name="minPrefix">1</int>
<int name="maxInspections">5</int>
<int name="minQueryLength">4</int>
<float name="maxQueryFrequency">0.01</float>
</lst>
</searchComponent>
<requestHandler name="/spell" class="solr.SearchHandler">
<lst name="defaults">
<str name="spellcheck">true</str>
<str name="spellcheck.extendedResults">true</str>
<str name="spellcheck.count">10</str>
<str name="spellcheck.alternativeTermCount">5</str>
<str name="spellcheck.maxResultsForSuggest">5</str>
<str name="spellcheck.collate">true</str>
<str name="spellcheck.collateExtendedResults">true</str>
<str name="spellcheck.maxCollationTries">10</str>
<str name="spellcheck.maxCollations">5</str>
</lst>
<arr name="last-components">
<str>spellcheck</str>
</arr>
</requestHandler>
5.2 使用拼写检查 #
bash
# 拼写检查
curl "http://localhost:8983/solr/mycore/spell?q=slor&spellcheck=true"
5.3 响应示例 #
json
{
"spellcheck": {
"suggestions": [
"slor",
{
"numFound": 1,
"startOffset": 0,
"endOffset": 4,
"suggestion": ["solr"]
}
],
"collations": [
"collation",
"solr"
]
}
}
5.4 集成到搜索 #
bash
# 搜索时启用拼写检查
curl "http://localhost:8983/solr/mycore/select" \
-d "q=slor" \
-d "spellcheck=true" \
-d "spellcheck.collate=true"
六、自动建议 #
6.1 配置自动建议 #
solrconfig.xml
xml
<searchComponent name="suggest" class="solr.SuggestComponent">
<lst name="suggester">
<str name="name">mySuggester</str>
<str name="lookupImpl">AnalyzingInfixLookupFactory</str>
<str name="dictionaryImpl">DocumentDictionaryFactory</str>
<str name="field">title</str>
<str name="weightField">popularity</str>
<str name="suggestAnalyzerFieldType">text_general</str>
<str name="buildOnStartup">false</str>
</lst>
</searchComponent>
<requestHandler name="/suggest" class="solr.SearchHandler">
<lst name="defaults">
<str name="suggest">true</str>
<str name="suggest.count">10</str>
</lst>
<arr name="components">
<str>suggest</str>
</arr>
</requestHandler>
6.2 Lookup实现 #
| 实现 | 说明 |
|---|---|
| AnalyzingInfixLookupFactory | 中间匹配 |
| AnalyzingLookupFactory | 前缀匹配 |
| FuzzyLookupFactory | 模糊匹配 |
| BlendedInfixLookupFactory | 混合匹配 |
6.3 构建建议索引 #
bash
# 构建建议索引
curl "http://localhost:8983/solr/mycore/suggest?suggest.build=true"
6.4 获取建议 #
bash
# 获取建议
curl "http://localhost:8983/solr/mycore/suggest" \
-d "suggest=true" \
-d "suggest.dictionary=mySuggester" \
-d "suggest.q=Sol"
6.5 响应示例 #
json
{
"suggest": {
"mySuggester": {
"Sol": {
"numFound": 3,
"suggestions": [
{
"term": "Solr实战指南",
"weight": 100,
"payload": ""
},
{
"term": "SolrCloud集群",
"weight": 80,
"payload": ""
}
]
}
}
}
}
6.6 自动重建 #
xml
<lst name="suggester">
<str name="name">mySuggester</str>
<str name="buildOnCommit">true</str>
<str name="buildOnStartup">true</str>
</lst>
七、More Like This #
7.1 配置MLT #
xml
<requestHandler name="/mlt" class="solr.MoreLikeThisHandler">
<lst name="defaults">
<str name="mlt.fl">title,content</str>
<int name="mlt.mintf">1</int>
<int name="mlt.mindf">1</int>
<int name="mlt.minwl">3</int>
<int name="mlt.maxwl">15</int>
<int name="mlt.maxqt">20</int>
<int name="mlt.maxntp">2000</int>
<bool name="mlt.boost">true</bool>
</lst>
</requestHandler>
7.2 使用MLT #
bash
# 查找相似文档
curl "http://localhost:8983/solr/mycore/mlt?q=id:book-001&mlt.fl=title,content&mlt.count=5"
7.3 MLT参数 #
| 参数 | 说明 | 默认值 |
|---|---|---|
| mlt.fl | 相似字段 | 空 |
| mlt.mintf | 最小词频 | 2 |
| mlt.mindf | 最小文档频率 | 5 |
| mlt.minwl | 最小词长 | 0 |
| mlt.maxwl | 最大词长 | 0 |
| mlt.maxqt | 最大查询词数 | 25 |
| mlt.count | 返回数量 | 5 |
| mlt.boost | 是否加权 | false |
八、实战示例 #
8.1 完整搜索示例 #
bash
curl "http://localhost:8983/solr/products/select" \
-d "defType=edismax" \
-d "q=苹果手机" \
-d "qf=name^3 description^1" \
-d "hl=true" \
-d "hl.fl=name,description" \
-d "hl.simple.pre=<em>" \
-d "hl.simple.post=</em>" \
-d "hl.snippets=3" \
-d "spellcheck=true" \
-d "spellcheck.collate=true" \
-d "fl=id,name,price,score"
8.2 自动建议集成 #
bash
# 获取建议
curl "http://localhost:8983/solr/products/suggest" \
-d "suggest=true" \
-d "suggest.dictionary=mySuggester" \
-d "suggest.q=苹果"
8.3 相似推荐 #
bash
# 查找相似商品
curl "http://localhost:8983/solr/products/mlt" \
-d "q=id:product-001" \
-d "mlt.fl=name,description,category" \
-d "mlt.count=10"
九、总结 #
功能对比:
| 功能 | 说明 | 适用场景 |
|---|---|---|
| 高亮显示 | 标记关键词 | 搜索结果展示 |
| 拼写检查 | 纠正拼写 | 用户输入容错 |
| 自动建议 | 输入提示 | 搜索提示 |
| More Like This | 相似推荐 | 相关推荐 |
最佳实践:
- 使用Unified Highlighter
- 配置拼写检查提升体验
- 定期重建建议索引
- 合理设置高亮参数
下一步,让我们学习排序与分页!
最后更新:2026-03-27