Solr/SolrJ:如何在不创建巨大 ArrayList 的情况下迭代结果

发布于 2024-10-18 07:46:49 字数 843 浏览 0 评论 0原文

有没有一种方法可以迭代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-10-25 07:46:50

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.

乙白 2024-10-25 07:46:49

该代码看起来是正确的。您还可以将其包装在迭代器中,以便您的客户端代码不必了解有关底层分页的任何信息。

关于 SolrQuery.setStart() 需要一个 Integer,它看起来确实很奇怪,我认为你是对的,它也应该是一个 long 。尝试询问 solr-userlucene-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文