Solr概念:搜索功能与查询语法

搜索是Solr的核心功能,掌握搜索原理和查询语法是构建高效搜索应用的关键。

搜索处理流程

搜索组件架构

1
2
3
4
5
6
7
8
9
10
11
12
13
用户查询

请求处理器 (Request Handler)

查询解析器 (Query Parser)

查询执行引擎

结果排序和过滤

响应格式化 (Response Writer)

返回结果给用户

核心搜索组件

1. 请求处理器(Request Handler)

定义搜索查询的处理逻辑:

1
2
3
4
5
6
7
8
<!-- solrconfig.xml中的请求处理器配置 -->
<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
<str name="echoParams">explicit</str>
<int name="rows">10</int>
<str name="df">text</str>
</lst>
</requestHandler>

2. 查询解析器(Query Parser)

将查询字符串转换为可执行的查询对象:

  • Standard/Lucene Parser:精确控制,支持完整Lucene语法
  • DisMax Parser:用户友好,容错性好
  • Extended DisMax (eDisMax):功能增强的DisMax

查询解析器详解

1. Standard Query Parser

最基础的查询解析器,支持完整的Lucene查询语法:

基本语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 单词搜索
q=solr

# 字段搜索
q=title:搜索引擎

# 短语搜索
q="apache solr"

# 通配符搜索
q=solr*
q=?olr

# 模糊搜索
q=solr~0.8

# 接近搜索
q="apache solr"~2

布尔操作符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# AND操作
q=apache AND solr
q=apache && solr

# OR操作
q=apache OR elasticsearch
q=apache || elasticsearch

# NOT操作
q=apache NOT elasticsearch
q=apache -elasticsearch

# 必须包含(+)和必须不包含(-)
q=+apache +solr -elasticsearch

# 分组
q=(apache OR solr) AND search

字段查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 指定字段搜索
q=title:搜索 AND content:Solr

# 多字段查询
q=title:(apache solr) OR content:(search engine)

# 范围查询
q=price:[10 TO 100] # 包含边界
q=price:{10 TO 100} # 不包含边界
q=date:[2024-01-01T00:00:00Z TO NOW]

# 数值范围
q=rating:[4 TO *] # 4分以上
q=price:[* TO 100] # 100以下

2. DisMax Query Parser

设计为用户友好的查询解析器:

1
2
3
4
5
6
7
8
9
10
11
# 基本配置
curl "http://localhost:8983/solr/mycollection/select" -d '
{
"query": "apache solr",
"params": {
"defType": "dismax",
"qf": "title^2.0 content^1.0 tags^1.5",
"pf": "title^3.0",
"mm": "75%"
}
}'

关键参数说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# qf (Query Fields): 搜索字段及权重
qf=title^2.0 content^1.0 author^0.5

# pf (Phrase Fields): 短语搜索字段
pf=title^3.0 content^1.5

# mm (Minimum Match): 最少匹配词条数或百分比
mm=2 # 至少匹配2个词
mm=75% # 至少匹配75%的词

# bf (Boost Functions): 函数加权
bf=recip(ms(NOW,publish_date),3.16e-11,1,1)

# bq (Boost Query): 查询加权
bq=category:hot^2.0

3. Extended DisMax (eDisMax)

功能最强大的查询解析器:

1
2
3
4
5
6
7
8
9
10
11
12
# 支持完整的查询语法
curl "http://localhost:8983/solr/mycollection/select" -d '
{
"query": "title:(apache OR solr) AND -elasticsearch",
"params": {
"defType": "edismax",
"qf": "title^2.0 content^1.0",
"pf": "title^3.0",
"mm": "50%",
"tie": "0.1"
}
}'

eDisMax特有功能

1
2
3
4
5
6
7
8
9
10
11
12
# 用户查询字段(uf):允许用户指定搜索字段
uf=title content tags

# 短语距离(ps):短语中词条的最大距离
ps=2

# 二元短语倾斜(ps2)和三元短语倾斜(ps3)
ps2=1
ps3=2

# 联结参数(tie):控制多字段匹配的分数计算
tie=0.1

查询参数详解

基本查询参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# q: 主查询
q=apache solr

# fq: 过滤查询(不影响评分)
fq=category:technology
fq=price:[10 TO 100]
fq=publish_date:[2024-01-01T00:00:00Z TO NOW]

# fl: 返回字段列表
fl=id,title,author,score

# rows: 返回结果数量
rows=20

# start: 结果起始位置(分页)
start=0

# sort: 排序
sort=score desc, publish_date desc

高级查询参数

1
2
3
4
5
6
7
8
9
10
11
12
13
# wt: 响应格式
wt=json # JSON格式(默认)
wt=xml # XML格式
wt=csv # CSV格式

# indent: 格式化输出
indent=true

# debugQuery: 调试信息
debugQuery=true

# timeAllowed: 查询超时时间(毫秒)
timeAllowed=5000

高级搜索功能

1. 分面搜索(Faceting)

提供搜索结果的分类统计:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 字段分面
curl "http://localhost:8983/solr/mycollection/select" -d '
{
"query": "*:*",
"facet": {
"categories": {
"type": "terms",
"field": "category",
"limit": 10
},
"price_ranges": {
"type": "range",
"field": "price",
"start": 0,
"end": 1000,
"gap": 100
}
}
}'

2. 高亮显示(Highlighting)

突出显示搜索词在结果中的位置:

1
2
3
4
5
6
7
8
9
10
11
curl "http://localhost:8983/solr/mycollection/select" -d '
{
"query": "apache solr",
"params": {
"hl": "true",
"hl.fl": "title,content",
"hl.simple.pre": "<mark>",
"hl.simple.post": "</mark>",
"hl.fragsize": 200
}
}'

3. 搜索建议(Suggester)

提供查询自动完成和拼写纠正:

1
2
3
4
5
# 自动完成
curl "http://localhost:8983/solr/mycollection/suggest?suggest=true&suggest.dictionary=mySuggester&suggest.q=ap"

# 拼写检查
curl "http://localhost:8983/solr/mycollection/spell?q=apachy&spellcheck=true&spellcheck.build=true"

4. 更多相似(More Like This)

基于文档相似性生成相关查询:

1
curl "http://localhost:8983/solr/mycollection/mlt?q=id:doc1&mlt.fl=title,content&mlt.count=5"

5. 结果聚类(Clustering)

根据发现的相似性对结果进行分组:

1
curl "http://localhost:8983/solr/mycollection/clustering?q=*:*&clustering=true&clustering.engine=lingo"

查询优化技巧

1. 查询性能优化

使用过滤查询

1
2
3
4
5
# 好的做法:使用fq进行过滤(可缓存)
q=apache solr&fq=category:technology&fq=year:2024

# 不好的做法:在主查询中包含过滤条件
q=apache solr AND category:technology AND year:2024

限制返回字段

1
2
3
4
5
# 只返回需要的字段
fl=id,title,score

# 避免返回大文本字段
fl=id,title,summary,-content

合理设置结果数量

1
2
3
4
5
# 避免请求过多结果
rows=20 # 而不是rows=1000

# 使用深度分页游标
cursorMark=* # 用于大结果集分页

2. 相关性优化

字段权重设置

1
2
3
4
5
# DisMax中设置字段权重
qf=title^3.0 content^1.0 tags^2.0 author^0.5

# 根据业务重要性调整权重
qf=product_name^5.0 description^1.0 brand^2.0

函数查询增强

1
2
3
4
5
6
7
8
# 基于时间的衰减函数
bf=recip(ms(NOW,publish_date),3.16e-11,1,1)

# 基于流行度的增强
bf=log(popularity)

# 组合多个因子
bf=product(popularity,recip(ms(NOW,date),3.16e-11,1,1))

3. 缓存优化

过滤查询缓存

1
2
<!-- solrconfig.xml中配置过滤缓存 -->
<filterCache size="512" initialSize="512" autowarmCount="128"/>

查询结果缓存

1
<queryResultCache size="512" initialSize="512" autowarmCount="32"/>

实际应用示例

1. 电商产品搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
curl "http://localhost:8983/solr/products/select" -d '
{
"query": "笔记本电脑",
"params": {
"defType": "edismax",
"qf": "product_name^3.0 description^1.0 brand^2.0 category^1.5",
"pf": "product_name^5.0",
"mm": "75%"
},
"filter": [
"in_stock:true",
"price:[500 TO 5000]",
"category:electronics"
],
"facet": {
"brands": {
"type": "terms",
"field": "brand",
"limit": 10
},
"price_ranges": {
"type": "range",
"field": "price",
"start": 0,
"end": 10000,
"gap": 1000
}
},
"params": {
"hl": "true",
"hl.fl": "product_name,description",
"sort": "score desc, popularity desc"
}
}'

2. 内容管理搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
curl "http://localhost:8983/solr/articles/select" -d '
{
"query": "人工智能 机器学习",
"params": {
"defType": "edismax",
"qf": "title^4.0 content^1.0 tags^2.0 author^0.5",
"pf": "title^6.0 content^1.5",
"mm": "50%",
"bf": "recip(ms(NOW,publish_date),3.16e-11,1,1)"
},
"filter": [
"status:published",
"publish_date:[2023-01-01T00:00:00Z TO NOW]"
],
"facet": {
"authors": {
"type": "terms",
"field": "author",
"limit": 5
},
"categories": {
"type": "terms",
"field": "category",
"limit": 10
},
"by_month": {
"type": "date_histogram",
"field": "publish_date",
"interval": "+1MONTH"
}
}
}'

3. 地理位置搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
curl "http://localhost:8983/solr/locations/select" -d '
{
"query": "咖啡店",
"params": {
"defType": "edismax",
"qf": "name^2.0 description^1.0 tags^1.5"
},
"filter": [
"{!geofilt pt=39.9042,116.4074 sfield=location d=5}",
"rating:[4 TO *]"
],
"params": {
"sort": "geodist() asc, rating desc",
"fl": "name,rating,distance:geodist()"
}
}'

调试和分析

1. 查询调试

1
2
# 启用调试信息
curl "http://localhost:8983/solr/mycollection/select?q=apache&debugQuery=true"

调试信息包括:

  • 查询解析结果
  • 评分详情
  • 时间统计
  • 其他诊断信息

2. 查询分析

1
2
3
4
5
# 分析字段处理
curl "http://localhost:8983/solr/mycollection/analysis/field?analysis.fieldname=title&analysis.fieldvalue=Apache Solr搜索"

# 查看查询计划
curl "http://localhost:8983/solr/mycollection/select?q=apache&debug=query"

3. 性能监控

1
2
3
4
5
# 查询性能统计
curl "http://localhost:8983/solr/admin/metrics?group=core&prefix=QUERY"

# 缓存统计
curl "http://localhost:8983/solr/admin/mbeans?cat=CACHE&stats=true"

最佳实践

1. 查询设计

  • 明确需求:理解用户搜索意图和业务需求
  • 选对解析器:根据场景选择合适的查询解析器
  • 优化字段权重:基于业务重要性设置字段权重
  • 合理使用过滤:使用fq进行不影响评分的过滤

2. 性能优化

  • 缓存策略:合理配置各类缓存
  • 查询复杂度:避免过于复杂的查询
  • 结果限制:合理设置返回结果数量
  • 监控分析:持续监控查询性能

3. 用户体验

  • 容错处理:处理拼写错误和模糊查询
  • 搜索建议:提供自动完成和相关建议
  • 结果展示:合理使用高亮和摘要
  • 响应速度:优化查询响应时间

总结

Solr搜索功能的关键要点:

  • 理解组件:掌握请求处理器、查询解析器的作用
  • 熟练语法:掌握不同查询解析器的语法和特性
  • 善用功能:利用分面、高亮等高级功能提升用户体验
  • 持续优化:通过监控和调试不断优化搜索性能

通过深入理解这些概念,您将能够构建功能强大、性能优异的搜索应用。

下一步学习

© 2025 Solr Community of China All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero