文档查询 #
一、查询概述 #
1.1 查询类型 #
| 类型 | 说明 | 特点 |
|---|---|---|
| 实时获取 | 按ID获取文档 | 近实时,不走索引 |
| 搜索查询 | 按条件搜索 | 走索引,支持复杂查询 |
| 实时搜索 | 搜索未提交文档 | NRT搜索 |
1.2 查询处理器 #
| 处理器 | 路径 | 说明 |
|---|---|---|
| /select | 搜索查询 | 主要查询入口 |
| /get | 实时获取 | 按ID获取文档 |
| /browse | 浏览查询 | 带界面 |
| /export | 导出查询 | 大量数据导出 |
二、实时获取(/get) #
2.1 单文档获取 #
bash
# 基本获取
curl "http://localhost:8983/solr/mycore/get?id=book-001"
# 指定返回字段
curl "http://localhost:8983/solr/mycore/get?id=book-001&fl=id,title,price"
2.2 响应格式 #
json
{
"doc": {
"id": "book-001",
"title": "Solr实战指南",
"author": "张三",
"price": 99.0,
"category": ["技术", "搜索"],
"_version_": 1234567890
}
}
2.3 多文档获取 #
bash
# 获取多个文档
curl "http://localhost:8983/solr/mycore/get?ids=book-001,book-002,book-003"
# 指定返回字段
curl "http://localhost:8983/solr/mycore/get?ids=book-001,book-002&fl=id,title,price"
2.4 多文档响应 #
json
{
"response": {
"numFound": 2,
"docs": [
{
"id": "book-001",
"title": "Solr实战指南",
"price": 99.0
},
{
"id": "book-002",
"title": "Elasticsearch权威指南",
"price": 128.0
}
]
}
}
2.5 文档不存在 #
bash
# 获取不存在的文档
curl "http://localhost:8983/solr/mycore/get?id=not-exist"
# 响应
{
"doc": null
}
三、实时获取特点 #
3.1 近实时性 #
/get处理器可以获取未提交的文档:
bash
# 索引文档(未提交)
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
-H "Content-Type: application/json" \
-d '{"id": "book-new", "title": "新书"}'
# 立即可获取(近实时)
curl "http://localhost:8983/solr/mycore/get?id=book-new"
3.2 配置要求 #
solrconfig.xml配置:
xml
<requestHandler name="/get" class="solr.RealTimeGetHandler">
<lst name="defaults">
<str name="omitHeader">true</str>
</lst>
</requestHandler>
<updateHandler class="solr.DirectUpdateHandler2">
<updateLog>
<str name="dir">${solr.ulog.dir:}</str>
</updateLog>
</updateHandler>
3.3 与/select区别 #
| 特性 | /get | /select |
|---|---|---|
| 查询方式 | 按ID直接获取 | 走索引搜索 |
| 实时性 | 近实时 | 需要提交 |
| 性能 | 高 | 中 |
| 功能 | 简单 | 复杂查询 |
四、搜索查询(/select) #
4.1 基本查询 #
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 AND author:张三"
4.2 查询参数 #
bash
curl "http://localhost:8983/solr/mycore/select" \
-d "q=title:Solr" \
-d "fl=id,title,price" \
-d "sort=price desc" \
-d "start=0" \
-d "rows=10"
4.3 过滤查询 #
bash
# 使用fq过滤
curl "http://localhost:8983/solr/mycore/select" \
-d "q=*:*" \
-d "fq=category:tech" \
-d "fq=price:[0 TO 100]"
五、文档存在检查 #
5.1 使用/get #
bash
# 检查文档是否存在
response=$(curl -s "http://localhost:8983/solr/mycore/get?id=book-001")
if echo "$response" | jq -e '.doc != null' > /dev/null; then
echo "文档存在"
else
echo "文档不存在"
fi
5.2 使用/select #
bash
# 查询文档数量
curl "http://localhost:8983/solr/mycore/select?q=id:book-001&rows=0"
# 响应
{
"response": {
"numFound": 1,
"docs": []
}
}
5.3 批量存在检查 #
bash
# 检查多个文档
curl "http://localhost:8983/solr/mycore/select?q=id:(book-001 OR book-002 OR book-003)&fl=id&rows=10"
六、实时搜索 #
6.1 软提交后搜索 #
bash
# 索引文档并软提交
curl -X POST "http://localhost:8983/solr/mycore/update?softCommit=true" \
-H "Content-Type: application/json" \
-d '{"id": "book-new", "title": "新书"}'
# 立即可搜索
curl "http://localhost:8983/solr/mycore/select?q=id:book-new"
6.2 自动软提交配置 #
xml
<updateHandler class="solr.DirectUpdateHandler2">
<autoSoftCommit>
<maxTime>1000</maxTime>
</autoSoftCommit>
</updateHandler>
七、导出查询(/export) #
7.1 基本导出 #
bash
# 导出大量数据
curl "http://localhost:8983/solr/mycore/export?q=*:*&fl=id,title,price&sort=id asc"
7.2 导出特点 #
- 支持导出大量数据
- 流式输出
- 需要指定排序
- 字段必须启用DocValues
7.3 配置要求 #
xml
<requestHandler name="/export" class="solr.SearchHandler">
<lst name="invariants">
<str name="rq">{!xport}</str>
<str name="sort">id asc</str>
</lst>
</requestHandler>
八、查询参数详解 #
8.1 基础参数 #
| 参数 | 说明 | 示例 |
|---|---|---|
| q | 查询语句 | q=title:Solr |
| fq | 过滤查询 | fq=category:tech |
| fl | 返回字段 | fl=id,title |
| start | 起始位置 | start=0 |
| rows | 返回行数 | rows=10 |
| sort | 排序 | sort=price desc |
| wt | 响应格式 | wt=json |
8.2 高级参数 #
| 参数 | 说明 | 示例 |
|---|---|---|
| debugQuery | 调试信息 | debugQuery=true |
| explainOther | 解释其他 | explainOther=id:book-001 |
| timeAllowed | 超时时间 | timeAllowed=1000 |
| omitHeader | 省略头部 | omitHeader=true |
| indent | 格式化输出 | indent=true |
8.3 响应格式 #
bash
# JSON格式
curl "http://localhost:8983/solr/mycore/select?q=*:*&wt=json"
# XML格式
curl "http://localhost:8983/solr/mycore/select?q=*:*&wt=xml"
# CSV格式
curl "http://localhost:8983/solr/mycore/select?q=*:*&wt=csv"
九、查询响应结构 #
9.1 标准响应 #
json
{
"responseHeader": {
"status": 0,
"QTime": 5,
"params": {
"q": "title:Solr",
"fl": "id,title,price"
}
},
"response": {
"numFound": 100,
"start": 0,
"docs": [
{
"id": "book-001",
"title": "Solr实战指南",
"price": 99.0
}
]
}
}
9.2 字段说明 #
| 字段 | 说明 |
|---|---|
| status | 状态码(0成功) |
| QTime | 查询耗时(毫秒) |
| numFound | 匹配文档总数 |
| start | 起始位置 |
| docs | 文档列表 |
十、查询优化 #
10.1 使用fq缓存 #
bash
# fq会被缓存,适合过滤条件
curl "http://localhost:8983/solr/mycore/select" \
-d "q=title:Solr" \
-d "fq=category:tech"
10.2 限制返回字段 #
bash
# 只返回需要的字段
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=id,title"
10.3 使用DocValues #
bash
# 排序字段使用DocValues
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=price desc"
十一、实战示例 #
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/articles/select" \
-d "q=Solr教程" \
-d "qf=title^3 content^1 tags^2" \
-d "fl=id,title,author,publish_date" \
-d "sort=publish_date desc" \
-d "rows=10"
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"
11.4 用户查询 #
bash
# 用户搜索
curl "http://localhost:8983/solr/users/select" \
-d "q=张三" \
-d "qf=name^3 email^1 phone^1" \
-d "fl=id,name,email,phone" \
-d "rows=10"
十二、常见问题 #
12.1 查询超时 #
bash
# 设置超时时间
curl "http://localhost:8983/solr/mycore/select?q=*:*&timeAllowed=5000"
12.2 结果过多 #
bash
# 限制返回数量
curl "http://localhost:8983/solr/mycore/select?q=*:*&rows=100"
# 使用游标分页
curl "http://localhost:8983/solr/mycore/select?q=*:*&sort=id asc&cursorMark=*&rows=100"
12.3 字段不存在 #
bash
# 使用fl参数指定字段,不存在的字段会被忽略
curl "http://localhost:8983/solr/mycore/select?q=*:*&fl=id,title,nonexistent"
十三、总结 #
查询方式对比:
| 方式 | 适用场景 | 实时性 | 性能 |
|---|---|---|---|
| /get | 按ID获取 | 近实时 | 高 |
| /select | 条件搜索 | 需提交 | 中 |
| /export | 大量导出 | 需提交 | 中 |
最佳实践:
- 按ID获取使用/get
- 条件搜索使用/select
- 大量导出使用/export
- 合理使用fq缓存
- 限制返回字段
下一步,让我们学习查询功能!
最后更新:2026-03-27