复合查询 #
一、Boolean查询 #
1.1 基本语法 #
Boolean查询使用bool查询组合多个条件:
bash
# bool查询结构
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool must='title:Solr' must='category:tech'}"
1.2 Boolean子句 #
| 子句 | 说明 |
|---|---|
| must | 必须匹配,影响评分 |
| should | 可选匹配,影响评分 |
| must_not | 必须不匹配,不影响评分 |
| filter | 必须匹配,不影响评分 |
1.3 must子句 #
bash
# 必须同时满足两个条件
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool must='title:Solr' must='author:张三'}"
1.4 should子句 #
bash
# 满足任一条件即可
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool should='title:Solr' should='title:Elasticsearch'}"
# 至少匹配一个should
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool minimum_number_should_match=1 should='title:Solr' should='title:Elasticsearch'}"
1.5 must_not子句 #
bash
# 排除条件
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool must='title:Solr' must_not='category:test'}"
1.6 filter子句 #
bash
# 过滤条件(不影响评分)
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool must='title:Solr' filter='price:[0 TO 100]'}"
1.7 组合使用 #
bash
# 复杂Boolean查询
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool \
must='title:Solr' \
should='author:张三' \
should='author:李四' \
must_not='category:test' \
filter='price:[0 TO 200]'}"
二、Boost查询 #
2.1 字段权重 #
bash
# 提高title字段的权重
curl "http://localhost:8983/solr/mycore/select?q=title:Solr^2 content:Solr"
2.2 查询权重 #
bash
# 提高整个查询的权重
curl "http://localhost:8983/solr/mycore/select?q=(title:Solr)^2 OR content:Solr"
2.3 DisMax权重 #
bash
# 使用DisMax的qf参数设置权重
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=dismax" \
-d "q=Solr" \
-d "qf=title^3 content^1 author^2"
2.4 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"
2.5 Boost函数 #
bash
# 使用函数boost
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr" \
-d "qf=title content" \
-d "boost=product(popularity,0.5)"
三、嵌套查询 #
3.1 嵌套Boolean #
bash
# 嵌套Boolean查询
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool \
must='{!bool should=\"title:Solr\" should=\"title:Elasticsearch\"}' \
must='category:tech'}"
3.2 嵌套范围查询 #
bash
# 嵌套范围查询
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool \
must='title:Solr' \
filter='{!range f=price}0 TO 100'}"
3.3 复杂嵌套 #
bash
# 复杂嵌套查询
curl "http://localhost:8983/solr/mycore/select" \
-d "q={!bool \
must='{!bool must=\"title:Solr\" filter=\"category:tech\"}' \
should='{!bool should=\"author:张三\" should=\"author:李四\"}' \
must_not='status:deleted'}"
四、条件组合 #
4.1 AND组合 #
bash
# 多条件AND
curl "http://localhost:8983/solr/mycore/select?q=title:Solr AND author:张三 AND category:tech"
4.2 OR组合 #
bash
# 多条件OR
curl "http://localhost:8983/solr/mycore/select?q=title:Solr OR title:Elasticsearch OR title:Lucene"
4.3 NOT组合 #
bash
# NOT组合
curl "http://localhost:8983/solr/mycore/select?q=title:Solr AND NOT category:test"
4.4 复杂组合 #
bash
# 复杂条件组合
curl "http://localhost:8983/solr/mycore/select?q=(title:Solr OR title:Elasticsearch) AND (author:张三 OR author:李四) AND NOT category:test"
五、本地参数 #
5.1 基本语法 #
text
{!param1=value1 param2=value2}query
5.2 指定查询类型 #
bash
# 使用dismax
curl "http://localhost:8983/solr/mycore/select?q={!dismax qf='title content'}Solr"
# 使用edismax
curl "http://localhost:8983/solr/mycore/select?q={!edismax qf='title^2 content'}Solr"
5.3 指定字段 #
bash
# 指定查询字段
curl "http://localhost:8983/solr/mycore/select?q={!field f=title}Solr"
# 使用term查询
curl "http://localhost:8983/solr/mycore/select?q={!term f=title}Solr"
5.4 范围查询 #
bash
# 使用range查询
curl "http://localhost:8983/solr/mycore/select?q={!range f=price}0 TO 100"
六、函数查询 #
6.1 基本函数 #
bash
# 使用函数
curl "http://localhost:8983/solr/mycore/select?q=_val_:product(price,popularity)"
6.2 数学函数 #
| 函数 | 说明 |
|---|---|
| sum(a,b) | 求和 |
| product(a,b) | 乘积 |
| div(a,b) | 除法 |
| sub(a,b) | 减法 |
| abs(x) | 绝对值 |
| log(x) | 对数 |
| sqrt(x) | 平方根 |
6.3 日期函数 #
bash
# 日期函数
curl "http://localhost:8983/solr/mycore/select?q=_val_:ms(NOW,publish_date)"
6.4 条件函数 #
bash
# if函数
curl "http://localhost:8983/solr/mycore/select?q=_val_:if(gt(popularity,100),1,0)"
七、评分控制 #
7.1 constant_score #
bash
# 固定评分
curl "http://localhost:8983/solr/mycore/select?q={!constant_score}title:Solr"
7.2 评分函数 #
bash
# 使用评分函数
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr" \
-d "qf=title content" \
-d "bf=product(popularity,0.1)"
7.3 评分模式 #
bash
# 指定评分模式
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr" \
-d "qf=title content" \
-d "tie=0.1"
八、实战示例 #
8.1 商品搜索 #
bash
curl "http://localhost:8983/solr/products/select" \
-d "defType=edismax" \
-d "q=iphone" \
-d "qf=name^3 description^1 brand^2" \
-d "pf=name^5" \
-d "fq={!bool filter='price:[3000 TO 10000]'}" \
-d "fq=category:手机" \
-d "boost=product(popularity,0.1)" \
-d "fl=id,name,price,brand,score" \
-d "sort=score desc,sales desc" \
-d "rows=20"
8.2 文档搜索 #
bash
curl "http://localhost:8983/solr/documents/select" \
-d "defType=edismax" \
-d "q=合同模板" \
-d "qf=title^3 content^1 tags^2" \
-d "pf=title^5" \
-d "fq={!bool \
must='type:pdf' \
filter='size:[0 TO 10485760]'}" \
-d "fl=id,title,author,score" \
-d "sort=score desc,date desc"
8.3 用户搜索 #
bash
curl "http://localhost:8983/solr/users/select" \
-d "defType=edismax" \
-d "q=张三" \
-d "qf=name^3 email^1 phone^1" \
-d "fq={!bool \
should='status:active' \
should='status:pending'}" \
-d "fl=id,name,email,phone,score" \
-d "sort=score desc"
8.4 日志搜索 #
bash
curl "http://localhost:8983/solr/logs/select" \
-d "defType=lucene" \
-d "q={!bool \
must='message:error' \
filter='level:ERROR' \
filter='{!range f=timestamp}NOW-1DAY TO NOW'}" \
-d "fl=id,message,timestamp,level,host" \
-d "sort=timestamp desc" \
-d "rows=100"
九、性能优化 #
9.1 使用filter代替must #
bash
# 不推荐(影响评分)
curl "http://localhost:8983/solr/mycore/select?q={!bool must='title:Solr' must='category:tech'}"
# 推荐(不影响评分)
curl "http://localhost:8983/solr/mycore/select?q={!bool must='title:Solr' filter='category:tech'}"
9.2 使用fq缓存 #
bash
# 使用fq缓存过滤条件
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&fq=category:tech&fq=price:[0 TO 100]"
9.3 避免复杂嵌套 #
bash
# 简化查询结构
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&fq=category:tech&fq=price:[0 TO 100]"
十、调试技巧 #
10.1 开启调试 #
bash
# 开启调试信息
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&debugQuery=true"
10.2 查看评分解释 #
bash
# 查看评分解释
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&debugQuery=true&fl=*,score"
10.3 分析查询性能 #
bash
# 查看查询耗时
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&debugQuery=true&debug=timing"
十一、总结 #
复合查询要点:
| 类型 | 说明 |
|---|---|
| Boolean | must、should、must_not、filter |
| Boost | 字段权重、查询权重 |
| 嵌套 | 多层Boolean嵌套 |
| 函数 | 数学函数、日期函数、条件函数 |
最佳实践:
- 使用filter代替must进行过滤
- 合理设置字段权重
- 避免过度嵌套
- 使用fq缓存
- 开启调试分析查询
下一步,让我们学习全文搜索!
最后更新:2026-03-27