全文搜索 #
一、全文搜索概述 #
1.1 什么是全文搜索 #
全文搜索是指在文档的全文内容中搜索关键词,主要特点:
- 支持分词
- 支持相关性排序
- 支持高亮显示
- 支持拼写检查
1.2 全文搜索流程 #
text
用户输入
↓
查询分析
↓
分词处理
↓
索引匹配
↓
评分排序
↓
返回结果
二、分词查询 #
2.1 基本分词查询 #
bash
# 分词查询
curl "http://localhost:8983/solr/mycore/select?q=content:Solr搜索引擎"
# 使用默认字段
curl "http://localhost:8983/solr/mycore/select?q=Solr搜索引擎&df=content"
2.2 DisMax分词查询 #
bash
# 使用DisMax
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=dismax" \
-d "q=Solr搜索引擎" \
-d "qf=title^2 content^1"
2.3 eDisMax分词查询 #
bash
# 使用eDisMax
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr搜索引擎" \
-d "qf=title^2 content^1" \
-d "pf=title^3" \
-d "mm=75%"
2.4 最小匹配(mm) #
bash
# 必须匹配所有词
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr搜索引擎教程" \
-d "qf=title content" \
-d "mm=100%"
# 必须匹配75%的词
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr搜索引擎教程" \
-d "qf=title content" \
-d "mm=75%"
# 必须匹配至少2个词
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr搜索引擎教程" \
-d "qf=title content" \
-d "mm=2"
三、短语查询 #
3.1 精确短语 #
bash
# 精确匹配短语
curl "http://localhost:8983/solr/mycore/select?q=content:\"Solr搜索引擎\""
3.2 短语邻近 #
bash
# 允许词间隔
curl "http://localhost:8983/solr/mycore/select?q=content:\"Solr 指南\"~5"
3.3 Phrase Boost #
bash
# 短语匹配加权
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr实战指南" \
-d "qf=title content" \
-d "pf=title^5 content^2"
3.4 PSlop参数 #
bash
# 设置短语slop
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr实战指南" \
-d "qf=title content" \
-d "pf=title^5" \
-d "ps=2"
四、模糊搜索 #
4.1 编辑距离模糊 #
bash
# 模糊匹配
curl "http://localhost:8983/solr/mycore/select?q=content:Solr~"
# 指定编辑距离
curl "http://localhost:8983/solr/mycore/select?q=content:Solr~1"
curl "http://localhost:8983/solr/mycore/select?q=content:Solr~2"
4.2 通配符模糊 #
bash
# 前缀匹配
curl "http://localhost:8983/solr/mycore/select?q=content:Sol*"
# 后缀匹配
curl "http://localhost:8983/solr/mycore/select?q=content:*r"
# 中间匹配
curl "http://localhost:8983/solr/mycore/select?q=content:S*r"
4.3 正则表达式 #
bash
# 正则匹配
curl "http://localhost:8983/solr/mycore/select?q=content:/[Ss]olr/"
五、同义词搜索 #
5.1 同义词配置 #
synonyms.txt
text
# 同义词配置
iphone,苹果手机,Apple手机
ipad,苹果平板,Apple平板
mbp,macbook pro
5.2 Schema配置 #
xml
<fieldType name="text_synonyms" class="solr.TextField">
<analyzer>
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.SynonymGraphFilterFactory"
synonyms="synonyms.txt"
ignoreCase="true"
expand="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
5.3 同义词扩展模式 #
expand=true(扩展模式)
text
iphone → iphone, 苹果手机, Apple手机
expand=false(替换模式)
text
iphone, 苹果手机, Apple手机 → iphone
5.4 使用同义词字段 #
bash
# 使用同义词字段搜索
curl "http://localhost:8983/solr/mycore/select?q=content_synonyms:苹果手机"
六、拼写检查 #
6.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>
6.2 使用拼写检查 #
bash
# 拼写检查
curl "http://localhost:8983/solr/mycore/spell?q=slor&spellcheck=true"
6.3 响应示例 #
json
{
"spellcheck": {
"suggestions": [
"slor",
{
"numFound": 1,
"startOffset": 0,
"endOffset": 4,
"suggestion": ["solr"]
}
],
"collations": [
"collation",
"solr"
]
}
}
七、自动建议 #
7.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>
7.2 构建建议索引 #
bash
# 构建建议索引
curl "http://localhost:8983/solr/mycore/suggest?suggest.build=true"
7.3 使用自动建议 #
bash
# 获取建议
curl "http://localhost:8983/solr/mycore/suggest?suggest=true&suggest.dictionary=mySuggester&suggest.q=Sol"
7.4 响应示例 #
json
{
"suggest": {
"mySuggester": {
"Sol": {
"numFound": 3,
"suggestions": [
{
"term": "Solr实战指南",
"weight": 100,
"payload": ""
},
{
"term": "SolrCloud集群",
"weight": 80,
"payload": ""
},
{
"term": "Solr性能优化",
"weight": 60,
"payload": ""
}
]
}
}
}
}
八、高亮显示 #
8.1 基本高亮 #
bash
# 开启高亮
curl "http://localhost:8983/solr/mycore/select" \
-d "q=content:Solr" \
-d "hl=true" \
-d "hl.fl=content"
8.2 高亮参数 #
| 参数 | 说明 | 默认值 |
|---|---|---|
| hl | 是否高亮 | false |
| hl.fl | 高亮字段 | 空 |
| hl.snippets | 片段数量 | 1 |
| hl.fragsize | 片段大小 | 100 |
| hl.simple.pre | 前缀 | <em> |
| hl.simple.post | 后缀 | </em> |
| hl.encoder | 编码方式 | 空 |
8.3 自定义高亮标签 #
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='highlight'>" \
-d "hl.simple.post=</span>"
8.4 高亮响应 #
json
{
"response": {
"docs": [
{
"id": "doc-001",
"content": "Solr是一个强大的搜索引擎"
}
]
},
"highlighting": {
"doc-001": {
"content": ["<em>Solr</em>是一个强大的搜索引擎"]
}
}
}
九、中文搜索 #
9.1 IK分词器 #
Schema配置
xml
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true"/>
</analyzer>
</fieldType>
9.2 中文分词查询 #
bash
# 中文分词查询
curl "http://localhost:8983/solr/mycore/select?q=content_ik:搜索引擎技术"
9.3 中文同义词 #
synonyms_cn.txt
text
# 中文同义词
搜索引擎,搜索技术,检索系统
手机,移动电话,智能机
十、实战示例 #
10.1 电商搜索 #
bash
curl "http://localhost:8983/solr/products/select" \
-d "defType=edismax" \
-d "q=苹果手机" \
-d "qf=name^3 description^1 brand^2" \
-d "pf=name^5" \
-d "mm=75%" \
-d "hl=true" \
-d "hl.fl=name,description" \
-d "hl.simple.pre=<em>" \
-d "hl.simple.post=</em>" \
-d "spellcheck=true" \
-d "fl=id,name,price,score"
10.2 文章搜索 #
bash
curl "http://localhost:8983/solr/articles/select" \
-d "defType=edismax" \
-d "q=Solr搜索引擎教程" \
-d "qf=title^3 content^1 tags^2" \
-d "pf=title^5" \
-d "hl=true" \
-d "hl.fl=title,content" \
-d "hl.snippets=3" \
-d "fl=id,title,author,score"
10.3 文档搜索 #
bash
curl "http://localhost:8983/solr/documents/select" \
-d "defType=edismax" \
-d "q=合同模板下载" \
-d "qf=title^3 content^1" \
-d "pf=title^5" \
-d "fq=type:pdf" \
-d "hl=true" \
-d "hl.fl=title,content" \
-d "fl=id,title,size,score"
十一、总结 #
全文搜索要点:
| 功能 | 说明 |
|---|---|
| 分词查询 | DisMax、eDisMax |
| 短语查询 | 精确短语、邻近短语 |
| 模糊搜索 | 编辑距离、通配符 |
| 同义词 | 扩展、替换模式 |
| 拼写检查 | 自动纠错 |
| 自动建议 | 输入提示 |
| 高亮显示 | 结果高亮 |
最佳实践:
- 选择合适的分词器
- 配置同义词提升搜索体验
- 使用拼写检查容错
- 开启高亮提升可读性
- 合理设置字段权重
下一步,让我们学习聚合统计!
最后更新:2026-03-27