Solr基础语法 #
一、REST API概述 #
1.1 API基础 #
Solr提供RESTful API,所有操作通过HTTP请求完成:
text
http://hostname:port/solr/{core}/{handler}?{parameters}
组成部分:
hostname:Solr服务器地址port:端口号(默认8983)core:Core名称handler:请求处理器parameters:请求参数
1.2 HTTP方法 #
| 方法 | 用途 |
|---|---|
| GET | 查询操作 |
| POST | 创建/更新操作 |
| DELETE | 删除操作 |
1.3 内容格式 #
Solr支持多种数据格式:
| 格式 | Content-Type | 说明 |
|---|---|---|
| JSON | application/json | 推荐格式 |
| XML | application/xml | 传统格式 |
| CSV | text/csv | 批量导入 |
| JSONL | application/x-ndjson | 流式JSON |
二、查询语法 #
2.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 |
2.2 基础查询示例 #
查询所有文档
bash
curl "http://localhost:8983/solr/mycore/select?q=*:*"
字段查询
bash
# 精确匹配
curl "http://localhost:8983/solr/mycore/select?q=title:Solr"
# 多字段查询
curl "http://localhost:8983/solr/mycore/select?q=title:Solr AND author:张三"
分页查询
bash
curl "http://localhost:8983/solr/mycore/select?q=*:*&start=0&rows=20"
指定返回字段
bash
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=id,title,price"
2.3 查询操作符 #
| 操作符 | 说明 | 示例 |
|---|---|---|
| AND | 与 | title:Solr AND author:张三 |
| OR | 或 | title:Solr OR title:Elasticsearch |
| NOT | 非 | title:Solr NOT title:Elasticsearch |
| + | 必须包含 | +title:Solr author:张三 |
| - | 排除 | title:Solr -title:Elasticsearch |
2.4 范围查询 #
bash
# 数值范围
curl "http://localhost:8983/solr/mycore/select?q=price:[0 TO 100]"
# 日期范围
curl "http://localhost:8983/solr/mycore/select?q=publish_date:[2026-01-01T00:00:00Z TO 2026-12-31T23:59:59Z]"
# 开区间
curl "http://localhost:8983/solr/mycore/select?q=price:{0 TO 100}"
2.5 通配符查询 #
bash
# 任意字符(单个)
curl "http://localhost:8983/solr/mycore/select?q=title:sol?"
# 任意字符(多个)
curl "http://localhost:8983/solr/mycore/select?q=title:sol*"
# 混合使用
curl "http://localhost:8983/solr/mycore/select?q=title:s*l?"
2.6 模糊查询 #
bash
# 模糊匹配(编辑距离2)
curl "http://localhost:8983/solr/mycore/select?q=title:solr~"
# 指定编辑距离
curl "http://localhost:8983/solr/mycore/select?q=title:solr~1"
2.7 短语查询 #
bash
# 精确短语
curl "http://localhost:8983/solr/mycore/select?q=title:\"Solr实战指南\""
# 短语邻近查询
curl "http://localhost:8983/solr/mycore/select?q=title:\"Solr 指南\"~5"
三、过滤查询(fq) #
3.1 fq与q的区别 #
| 特性 | q | fq |
|---|---|---|
| 影响评分 | 是 | 否 |
| 缓存 | 不缓存 | 缓存 |
| 用途 | 主要查询条件 | 过滤条件 |
3.2 使用示例 #
bash
# 单个过滤条件
curl "http://localhost:8983/solr/mycore/select?q=title:solr&fq=category:tech"
# 多个过滤条件(AND)
curl "http://localhost:8983/solr/mycore/select?q=title:solr&fq=category:tech&fq=price:[0 TO 100]"
# 多个过滤条件(OR)
curl "http://localhost:8983/solr/mycore/select?q=title:solr&fq=category:tech OR category:book"
3.3 过滤查询标签 #
bash
# 为fq添加标签
curl "http://localhost:8983/solr/mycore/select?q=*:*&fq={!tag=catTag}category:tech&facet=true&facet.field={!ex=catTag}category"
四、排序 #
4.1 基础排序 #
bash
# 单字段排序
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=price desc"
# 多字段排序
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=price desc, title asc"
4.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"
4.3 函数排序 #
bash
# 使用函数排序
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=product(price,popularity) desc"
五、分页 #
5.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"
5.2 深度分页(Cursor) #
对于深度分页,使用Cursor更高效:
bash
# 初始请求
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=id asc&rows=10&cursorMark=*"
# 响应包含nextCursorMark
{
"response": {
"docs": [...]
},
"nextCursorMark": "AoE/BjQ0MTY1NQ=="
}
# 下一页请求
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=id asc&rows=10&cursorMark=AoE/BjQ0MTY1NQ=="
六、响应格式 #
6.1 JSON响应 #
bash
curl "http://localhost:8983/solr/mycore/select?q=*:*&wt=json"
响应结构:
json
{
"responseHeader": {
"status": 0,
"QTime": 5,
"params": {
"q": "*:*",
"wt": "json"
}
},
"response": {
"numFound": 100,
"start": 0,
"docs": [
{
"id": "book-001",
"title": "Solr实战指南",
"author": "张三",
"price": 99.0
}
]
}
}
6.2 XML响应 #
bash
curl "http://localhost:8983/solr/mycore/select?q=*:*&wt=xml"
响应结构:
xml
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">5</int>
</lst>
<result name="response" numFound="100" start="0">
<doc>
<str name="id">book-001</str>
<str name="title">Solr实战指南</str>
<str name="author">张三</str>
<float name="price">99.0</float>
</doc>
</result>
</response>
6.3 响应字段说明 #
| 字段 | 说明 |
|---|---|
| status | 状态码(0表示成功) |
| QTime | 查询耗时(毫秒) |
| numFound | 匹配文档总数 |
| start | 起始位置 |
| docs | 文档列表 |
七、文档操作API #
7.1 添加文档 #
JSON格式
bash
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
-H "Content-Type: application/json" \
-d '{
"id": "book-001",
"title": "Solr实战指南",
"author": "张三",
"price": 99.0
}'
批量添加
bash
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
-H "Content-Type: application/json" \
-d '[
{"id": "book-001", "title": "Solr实战指南"},
{"id": "book-002", "title": "Elasticsearch权威指南"}
]'
7.2 提交更改 #
bash
# 软提交(近实时可见)
curl -X POST "http://localhost:8983/solr/mycore/update?softCommit=true"
# 硬提交(持久化到磁盘)
curl -X POST "http://localhost:8983/solr/mycore/update?commit=true"
7.3 删除文档 #
按ID删除
bash
curl -X POST "http://localhost:8983/solr/mycore/update" \
-H "Content-Type: application/json" \
-d '{"delete": {"id": "book-001"}}'
按查询删除
bash
curl -X POST "http://localhost:8983/solr/mycore/update" \
-H "Content-Type: application/json" \
-d '{"delete": {"query": "category:tech"}}'
删除所有文档
bash
curl -X POST "http://localhost:8983/solr/mycore/update" \
-H "Content-Type: application/json" \
-d '{"delete": {"query": "*:*"}}'
八、常用查询参数详解 #
8.1 查询参数 #
| 参数 | 说明 | 示例 |
|---|---|---|
| q | 主查询 | q=title:solr |
| q.op | 默认操作符 | q.op=AND |
| df | 默认字段 | df=text |
| qf | 查询字段(Dismax) | qf=title^2 content |
| mm | 最小匹配 | mm=75% |
8.2 结果参数 #
| 参数 | 说明 | 示例 |
|---|---|---|
| fl | 返回字段 | fl=id,title |
| start | 起始位置 | start=0 |
| rows | 返回行数 | rows=10 |
| sort | 排序 | sort=price desc |
| cursorMark | 游标标记 | cursorMark=* |
8.3 高级参数 #
| 参数 | 说明 | 示例 |
|---|---|---|
| debugQuery | 调试信息 | debugQuery=true |
| explainOther | 解释其他 | explainOther=id:book-001 |
| timeAllowed | 超时时间 | timeAllowed=1000 |
| segmentTerminateEarly | 提前终止 | segmentTerminateEarly=true |
九、本地参数 #
9.1 语法格式 #
text
{!param1=value1 param2=value2}query
9.2 使用示例 #
bash
# 指定查询解析器
curl "http://localhost:8983/solr/mycore/select?q={!dismax qf=title}solr"
# 指定字段
curl "http://localhost:8983/solr/mycore/select?q={!field f=title}solr"
# 使用术语查询
curl "http://localhost:8983/solr/mycore/select?q={!term f=title}solr"
十、查询解析器 #
10.1 常用解析器 #
| 解析器 | 说明 |
|---|---|
| lucene | 默认解析器 |
| dismax | DisMax查询 |
| edismax | 扩展DisMax |
| complexphrase | 复杂短语 |
| surround | 邻近查询 |
10.2 DisMax解析器 #
bash
curl "http://localhost:8983/solr/mycore/select" \
-d "defType=dismax" \
-d "q=solr guide" \
-d "qf=title^2 content^1" \
-d "mm=100%"
10.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%"
十一、实战示例 #
11.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 "rows=20"
11.2 文档搜索 #
bash
curl "http://localhost:8983/solr/documents/select" \
-d "defType=edismax" \
-d "q=合同模板" \
-d "qf=title^3 content^1 tags^2" \
-d "hl=true" \
-d "hl.fl=title,content" \
-d "hl.simple.pre=<em>" \
-d "hl.simple.post=</em>" \
-d "fl=id,title,author,date"
11.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、cursorMark |
| 操作符 | AND、OR、NOT、+、- |
| 通配符 | *、? |
| 模糊查询 | ~ |
| 范围查询 | [a TO b]、 |
学习建议:
- 熟练掌握基础查询语法
- 理解q和fq的区别
- 学会使用eDisMax解析器
- 掌握分页和排序技巧
下一步,让我们学习索引管理!
最后更新:2026-03-27