基础查询 #
一、查询参数 #
1.1 核心参数 #
| 参数 | 说明 | 示例 |
|---|---|---|
| q | 主查询语句 | q=title:Solr |
| fq | 过滤查询 | fq=category:tech |
| fl | 返回字段 | fl=id,title,price |
| sort | 排序 | sort=price desc |
| start | 起始位置 | start=0 |
| rows | 返回行数 | rows=10 |
| wt | 响应格式 | wt=json |
| df | 默认字段 | df=text |
1.2 基本查询示例 #
bash
# 查询所有文档
curl "http://localhost:8983/solr/mycore/select?q=*:*"
# 字段查询
curl "http://localhost:8983/solr/mycore/select?q=title:Solr"
# 多参数查询
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&fl=id,title&rows=10"
二、查询操作符 #
2.1 布尔操作符 #
AND(与)
bash
# 必须同时满足两个条件
curl "http://localhost:8983/solr/mycore/select?q=title:Solr AND author:张三"
OR(或)
bash
# 满足任一条件
curl "http://localhost:8983/solr/mycore/select?q=title:Solr OR title:Elasticsearch"
NOT(非)
bash
# 排除条件
curl "http://localhost:8983/solr/mycore/select?q=title:Solr NOT title:Elasticsearch"
组合使用
bash
# 复杂布尔查询
curl "http://localhost:8983/solr/mycore/select?q=(title:Solr OR title:Elasticsearch) AND category:tech"
2.2 必须与排除 #
+(必须包含)
bash
# 必须包含Solr
curl "http://localhost:8983/solr/mycore/select?q=+title:Solr author:张三"
-(排除)
bash
# 排除Elasticsearch
curl "http://localhost:8983/solr/mycore/select?q=title:Solr -title:Elasticsearch"
2.3 操作符优先级 #
text
NOT > AND > OR
bash
# 使用括号明确优先级
curl "http://localhost:8983/solr/mycore/select?q=(title:Solr OR title:Elasticsearch) AND category:tech"
三、范围查询 #
3.1 数值范围 #
bash
# 闭区间 [min TO max]
curl "http://localhost:8983/solr/mycore/select?q=price:[0 TO 100]"
# 开区间 {min TO max}
curl "http://localhost:8983/solr/mycore/select?q=price:{0 TO 100}"
# 半开区间
curl "http://localhost:8983/solr/mycore/select?q=price:[0 TO 100}"
curl "http://localhost:8983/solr/mycore/select?q=price:{0 TO 100]"
3.2 日期范围 #
bash
# 日期范围
curl "http://localhost:8983/solr/mycore/select?q=publish_date:[2026-01-01T00:00:00Z TO 2026-12-31T23:59:59Z]"
# 使用NOW
curl "http://localhost:8983/solr/mycore/select?q=publish_date:[NOW-7DAYS TO NOW]"
curl "http://localhost:8983/solr/mycore/select?q=publish_date:[NOW-1MONTH TO NOW]"
3.3 无限范围 #
bash
# 小于等于
curl "http://localhost:8983/solr/mycore/select?q=price:[* TO 100]"
# 大于等于
curl "http://localhost:8983/solr/mycore/select?q=price:[100 TO *]"
# 所有日期
curl "http://localhost:8983/solr/mycore/select?q=publish_date:[* TO *]"
四、通配符查询 #
4.1 星号通配符 #
bash
# 匹配任意字符(0个或多个)
curl "http://localhost:8983/solr/mycore/select?q=title:sol*"
# 匹配Solr、SolrCloud、SolrJ等
curl "http://localhost:8983/solr/mycore/select?q=title:sol*"
4.2 问号通配符 #
bash
# 匹配单个字符
curl "http://localhost:8983/solr/mycore/select?q=title:sol?"
# 匹配solr、sola、solb等
curl "http://localhost:8983/solr/mycore/select?q=title:sol?"
4.3 混合使用 #
bash
# 混合通配符
curl "http://localhost:8983/solr/mycore/select?q=title:s*l?"
# 注意:通配符查询性能较低
五、模糊查询 #
5.1 编辑距离模糊 #
bash
# 模糊匹配(默认编辑距离2)
curl "http://localhost:8983/solr/mycore/select?q=title:solr~"
# 指定编辑距离
curl "http://localhost:8983/solr/mycore/select?q=title:solr~1"
curl "http://localhost:8983/solr/mycore/select?q=title:solr~2"
5.2 编辑距离说明 #
| 编辑距离 | 说明 |
|---|---|
| 0 | 精确匹配 |
| 1 | 允许1个字符差异 |
| 2 | 允许2个字符差异 |
5.3 使用场景 #
- 拼写错误纠正
- 近似匹配
- 用户输入容错
六、短语查询 #
6.1 精确短语 #
bash
# 精确匹配短语
curl "http://localhost:8983/solr/mycore/select?q=title:\"Solr实战指南\""
6.2 短语邻近 #
bash
# 允许单词间隔(最多5个词)
curl "http://localhost:8983/solr/mycore/select?q=title:\"Solr 指南\"~5"
6.3 短语查询特点 #
- 双引号包裹
- 单词顺序必须一致
- 邻近查询允许间隔
七、正则表达式查询 #
7.1 基本语法 #
bash
# 正则表达式查询
curl "http://localhost:8983/solr/mycore/select?q=title:/[Ss]olr/"
7.2 常用正则 #
bash
# 以Solr开头
curl "http://localhost:8983/solr/mycore/select?q=title:/Solr.*/"
# 包含数字
curl "http://localhost:8983/solr/mycore/select?q=id:/book-[0-9]+/"
# 邮箱格式
curl "http://localhost:8983/solr/mycore/select?q=email:/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/"
八、过滤查询(fq) #
8.1 fq与q的区别 #
| 特性 | q | fq |
|---|---|---|
| 影响评分 | 是 | 否 |
| 缓存 | 不缓存 | 缓存 |
| 用途 | 主要查询 | 过滤条件 |
8.2 使用fq #
bash
# 单个过滤条件
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&fq=category:tech"
# 多个过滤条件(AND关系)
curl "http://localhost:8983/solr/mycore/select?q=*:*&fq=category:tech&fq=price:[0 TO 100]"
# 多个过滤条件(OR关系)
curl "http://localhost:8983/solr/mycore/select?q=*:*&fq=category:tech OR category:book"
8.3 fq缓存标签 #
bash
# 为fq添加标签
curl "http://localhost:8983/solr/mycore/select?q=*:*&fq={!tag=catTag}category:tech&facet=true&facet.field={!ex=catTag}category"
九、字段列表(fl) #
9.1 指定返回字段 #
bash
# 返回指定字段
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=id,title,price"
# 返回所有字段
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=*"
# 排除字段
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=*,_version_"
9.2 字段别名 #
bash
# 使用别名
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=id,title:书名,price:价格"
9.3 函数字段 #
bash
# 使用函数
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=id,price,discount:product(price,0.8)"
十、排序(sort) #
10.1 基本排序 #
bash
# 单字段排序
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=price desc"
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=price asc"
# 多字段排序
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=price desc,title asc"
10.2 评分排序 #
bash
# 按相关度排序(默认)
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&sort=score desc"
# 评分与其他字段组合
curl "http://localhost:8983/solr/mycore/select?q=title:Solr&sort=score desc,price asc"
10.3 函数排序 #
bash
# 使用函数排序
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=product(price,popularity) desc"
十一、分页 #
11.1 基础分页 #
bash
# 第一页(0-9)
curl "http://localhost:8983/solr/mycore/select?q=*:*&start=0&rows=10"
# 第二页(10-19)
curl "http://localhost:8983/solr/mycore/select?q=*:*&start=10&rows=10"
# 第三页(20-29)
curl "http://localhost:8983/solr/mycore/select?q=*:*&start=20&rows=10"
11.2 深度分页限制 #
基础分页在深度分页时性能下降:
bash
# 深度分页性能差
curl "http://localhost:8983/solr/mycore/select?q=*:*&start=100000&rows=10"
11.3 游标分页 #
bash
# 初始请求
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=id asc&rows=10&cursorMark=*"
# 使用返回的nextCursorMark继续
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=id asc&rows=10&cursorMark=AoE/BjQ0MTY1NQ=="
十二、查询解析器 #
12.1 Lucene解析器(默认) #
bash
# 标准Lucene语法
curl "http://localhost:8983/solr/mycore/select?q=title:Solr AND price:[0 TO 100]"
12.2 DisMax解析器 #
bash
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=dismax" \
-d "q=Solr guide" \
-d "qf=title^2 content^1"
12.3 eDisMax解析器 #
bash
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=edismax" \
-d "q=Solr guide" \
-d "qf=title^2 content^1" \
-d "pf=title^3" \
-d "mm=75%"
十三、实战示例 #
13.1 电商搜索 #
bash
curl "http://localhost:8983/solr/products/select" \
-d "defType=edismax" \
-d "q=iphone" \
-d "qf=name^3 description^1 brand^2" \
-d "fq=category:手机" \
-d "fq=price:[3000 TO 10000]" \
-d "fl=id,name,price,brand,image" \
-d "sort=score desc,sales desc" \
-d "start=0" \
-d "rows=20"
13.2 文档搜索 #
bash
curl "http://localhost:8983/solr/documents/select" \
-d "q=合同模板" \
-d "qf=title^3 content^1" \
-d "fq=type:pdf" \
-d "fl=id,title,author,date" \
-d "sort=date desc" \
-d "rows=10"
13.3 日志搜索 #
bash
curl "http://localhost:8983/solr/logs/select" \
-d "q=error" \
-d "fq=level:ERROR" \
-d "fq=timestamp:[NOW-1DAY TO NOW]" \
-d "fl=id,message,timestamp,level,host" \
-d "sort=timestamp desc" \
-d "rows=100"
十四、总结 #
基础查询要点:
| 类别 | 要点 |
|---|---|
| 参数 | q、fq、fl、sort、start、rows |
| 操作符 | AND、OR、NOT、+、- |
| 范围 | [min TO max]、 |
| 通配符 | *、? |
| 模糊 | ~ |
| 短语 | “”、~n |
最佳实践:
- 使用fq进行过滤
- 限制返回字段
- 合理使用分页
- 选择合适的查询解析器
下一步,让我们学习复合查询!
最后更新:2026-03-27