Solr/SolrJ:如何在不创建巨大 ArrayList 的情况下迭代结果
有没有一种方法可以迭代 Solrj 响应,以便在迭代期间增量获取结果,而不是返回巨大的内存中 ArrayList?
或者我们是否必须诉诸于此:
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
int fetchSize = 1000;
query.setRows(fetchSize);
QueryResponse rsp = server.query(query);
long offset = 0;
long totalResults = rsp.getResults().getNumFound();
while (offset < totalResults)
{
query.setStart((int) offset); // requires an int? wtf?
query.setRows(fetchSize);
for (SolrDocument doc : server.query(query).getResults())
{
log.info((String) doc.getFieldValue("title"));
}
offset += fetchSize;
}
当我谈论这个主题时,为什么 SolrQuery.setStart()
需要一个 integer
,当 SolrDocumentList.getStart ()/getNumFound()
返回long
?
Is there a way to iterate over a Solrj response such that the results are fetched incrementally during iteration, rather than returning a giant in-memory ArrayList
?
Or do we have to resort to this:
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
int fetchSize = 1000;
query.setRows(fetchSize);
QueryResponse rsp = server.query(query);
long offset = 0;
long totalResults = rsp.getResults().getNumFound();
while (offset < totalResults)
{
query.setStart((int) offset); // requires an int? wtf?
query.setRows(fetchSize);
for (SolrDocument doc : server.query(query).getResults())
{
log.info((String) doc.getFieldValue("title"));
}
offset += fetchSize;
}
And while I'm on the topic, why does SolrQuery.setStart()
require an integer
, when SolrDocumentList.getStart()/getNumFound()
return long
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Caffeine 的原因是 Solr 旨在为您提供前 X 个搜索结果。期望您将返回一个“合理”的数字。如果 Solr 必须深入查看搜索结果(数千个),那么您就违背了 Solr 的设计目的。它会起作用,但查询响应将呈指数级减慢,并且您必须深入搜索结果越慢。
Solr 中正在进行一些工作来提高此用例的效率,但我最近没有看到任何进展。
The reason, Caffeine, is that Solr is designed to give you the top X search results. The expectation is that you will have a "reasonable" number to return. If Solr has to look deep into the search results (into the thousands), you're rubbing against the grain for what Solr was designed for. It will work but the query response will get exponentially slower and slower the deeper into the search results you have to go.
There is some ongoing work in Solr to make this use-case more efficient but I've seen no progress on it lately.
该代码看起来是正确的。您还可以将其包装在迭代器中,以便您的客户端代码不必了解有关底层分页的任何信息。
关于
SolrQuery.setStart()
需要一个 Integer,它看起来确实很奇怪,我认为你是对的,它也应该是一个 long 。尝试询问 solr-user 或 lucene-dev 邮件列表。That code looks correct. You could also wrap it in an Iterator so that your client code doesn't have to know anything about the underlying paging.
About
SolrQuery.setStart()
requiring an Integer, it certainly looks odd, I think you're right and it should be a long as well. Try asking on the solr-user or lucene-dev mailing lists.