Solr配置:实时获取功能详解与配置指南

Solr配置:实时获取功能详解与配置指南

对于索引更新要可见(可搜索),必须执行某种提交来重新打开搜索器到索引的新时间点视图。

实时获取功能允许通过unique-key检索任何文档的最新版本,而无需重新打开搜索器的相关成本。这在将Solr用作NoSQL数据存储而不仅仅是搜索索引时特别有用。

实时获取的工作原理

实时获取依赖于更新日志功能,该功能默认启用,可以在solrconfig.xml中配置:

1
2
3
<updateLog>
<str name="dir">${solr.ulog.dir:}</str>
</updateLog>

GET处理器配置

实时获取请求可以使用/get处理器执行,该处理器在Solr中隐式存在,等效于以下配置:

1
2
3
4
5
<requestHandler name="/get" class="solr.RealTimeGetHandler">
<lst name="defaults">
<str name="omitHeader">true</str>
</lst>
</requestHandler>

基本使用示例

1. 索引未提交的文档

如果你使用bin/solr start -e techproducts示例命令启动Solr,可以索引一个新文档而不提交它:

1
2
curl 'http://localhost:8983/solr/techproducts/update/json?commitWithin=10000000' \
-H 'Content-type:application/json' -d '[{"id":"mydoc","name":"realtime-get test!"}]'

2. 常规搜索无法找到

如果搜索此文档,应该找不到它:

1
http://localhost:8983/solr/techproducts/query?q=id:mydoc

响应:

1
2
3
{"response":
{"numFound":0,"start":0,"docs":[]}
}

3. 使用实时获取检索

但是,如果使用在/get公开的实时获取处理器,可以检索文档:

V1 API方式

1
http://localhost:8983/solr/techproducts/get?id=mydoc

响应:

1
2
3
4
5
6
{"doc": {
"id": "mydoc",
"name": "realtime-get test!",
"_version_": 1487137811571146752
}
}

V2 API方式

1
http://localhost:8983/api/collections/techproducts/get?id=mydoc

响应:

1
2
3
4
5
6
{"doc": {
"id": "mydoc",
"name": "realtime-get test!",
"_version_": 1487137811571146752
}
}

批量获取文档

可以通过ids参数和逗号分隔的id列表,或使用多个id参数来同时指定多个文档。如果指定多个id或使用ids参数,响应将模拟正常查询响应,使现有客户端更容易解析。

批量获取示例

V1 API方式

1
2
3
http://localhost:8983/solr/techproducts/get?ids=mydoc,IW-02
# 或者
http://localhost:8983/solr/techproducts/get?id=mydoc&id=IW-02

V2 API方式

1
2
3
http://localhost:8983/api/collections/techproducts/get?ids=mydoc,IW-02
# 或者
http://localhost:8983/api/collections/techproducts/get?id=mydoc&id=IW-02

响应:

1
2
3
4
5
6
7
8
9
10
11
12
{"response":
{"numFound":2,"start":0,"docs":
[ { "id":"mydoc",
"name":"realtime-get test!",
"_version_":1487137811571146752},
{
"id":"IW-02",
"name":"iPod & iPod Mini USB 2.0 Cable"
}
]
}
}

结合过滤查询使用

实时获取请求还可以与过滤查询结合使用,通过fq参数指定:

V1 API方式

1
http://localhost:8983/solr/techproducts/get?id=mydoc&id=IW-02&fq=name:realtime-get

V2 API方式

1
http://localhost:8983/api/collections/techproducts/get?id=mydoc&id=IW-02&fq=name:realtime-get

响应:

1
2
3
4
5
6
7
8
{"response":
{"numFound":1,"start":0,"docs":
[ { "id":"mydoc",
"name":"realtime-get test!",
"_version_":1487137811571146752}
]
}
}

SolrCloud中的重要警告

⚠️ 重要提醒:如果你使用SolrCloud,不要禁用/get处理器的实时获取处理器。这样做会导致任何leader选举在相关分片的所有副本中引起完全同步。

同样,在没有此处理器的情况下,副本恢复也将始终从leader获取完整索引,因为部分同步将不可能实现。

配置最佳实践

1. 更新日志配置

  • 确保更新日志功能已启用
  • 合理配置更新日志目录
  • 监控更新日志大小和性能

2. 处理器配置

  • 保持默认的/get处理器配置
  • 根据需要调整omitHeader等参数
  • 在SolrCloud环境中不要禁用此处理器

3. 使用场景

  • NoSQL存储:将Solr用作文档存储时的理想选择
  • 实时应用:需要立即访问最新更新的应用
  • 混合查询:结合实时获取和常规搜索的应用

4. 性能考虑

  • 实时获取避免了重新打开搜索器的开销
  • 适合频繁更新、立即读取的场景
  • 监控更新日志对磁盘空间的影响

应用场景示例

1. 用户配置文件更新

1
2
3
4
5
6
7
# 更新用户配置文件
curl -X POST 'http://localhost:8983/solr/users/update/json?commitWithin=30000' \
-H 'Content-type:application/json' \
-d '[{"id":"user123","profile":{"name":"John","email":"john@example.com"}}]'

# 立即获取更新的配置文件
http://localhost:8983/solr/users/get?id=user123

2. 实时统计更新

1
2
3
4
5
6
7
# 更新计数器
curl -X POST 'http://localhost:8983/solr/stats/update/json?commitWithin=60000' \
-H 'Content-type:application/json' \
-d '[{"id":"counter1","value":{"inc":1}}]'

# 立即读取最新值
http://localhost:8983/solr/stats/get?id=counter1

总结

Solr的实时获取功能为需要立即访问最新数据的应用提供了强大的支持。通过合理配置和使用此功能,可以将Solr有效地用作NoSQL数据存储,同时保持其强大的搜索能力。在SolrCloud环境中,务必保持实时获取处理器的启用状态,以确保集群的正常运行和数据一致性。

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