java 使用 Solrj 在 Solr 中获取所有结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14020006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:47:10  来源:igfitidea点击:

Get All Result in Solr with Solrj

javasolrsolrj

提问by user527

I want to get all result with solrj, I add 10 document to Solr, I don't get any exception, but if I add more than 10 document to SolrI get exception. I search that, I get this exception for this, in http://localhost:8983/solr/browse10 document in first page,11th document go to second page. How I can get all result?

我想获得所有结果solrj,我将 10 个文档添加到Solr,我没有得到任何异常,但是如果我添加了 10 个以上的文档,Solr我得到了异常。我搜索了这个,我得到了这个例外,在第一页的http://localhost:8983/solr/browse10 文档中,第 11 个文档转到第二页。我怎样才能得到所有的结果?

String qry="*:*";
                CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr");
      QueryResponse rsp=server.query(new SolrQuery(qry));
      SolrDocumentList docs=rsp.getResults();  
                        for(int i=0;i<docs.getNumFound();i++){

                            System.out.println(docs.get(i));                    
    }

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 10, Size: 10

线程“AWT-EventQueue-0”中的异常java.lang.IndexOutOfBoundsException:索引:10,大小:10

回答by phantaux

    Integer start = 0;

    query.setStart(start);
    QueryResponse response = server.query(query);
    SolrDocumentList rs = response.getResults();
    long numFound = rs.getNumFound();
    int current = 0;
    while (current < numFound) {

        ListIterator<SolrDocument> iter = rs.listIterator();
        while (iter.hasNext()) {
            current++;

            System.out.println("************************************************************** " + current + "   " + numFound);
            SolrDocument doc = iter.next();
            Map<String, Collection<Object>> values = doc.getFieldValuesMap();

            Iterator<String> names = doc.getFieldNames().iterator();
            while (names.hasNext()) {
                String name = names.next();
                System.out.print(name);
                System.out.print(" = ");

                Collection<Object> vals = values.get(name);
                Iterator<Object> valsIter = vals.iterator();
                while (valsIter.hasNext()) {
                    Object obj = valsIter.next();
                    System.out.println(obj.toString());
                }
            }
        }
        query.setStart(current);
        response = server.query(query);
        rs = response.getResults();
         numFound = rs.getNumFound();


    }
}

回答by Chen Sheng-Lun

An easier way:

更简单的方法:

CloudSolrServer server = new CloudSolrServer(solrZKServerUrl);
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setRows(Integer.MAX_VALUE);
QueryResponse rsp;
rsp = server.query(query, METHOD.POST);
SolrDocumentList docs = rsp.getResults();
for (SolrDocument doc : docs) {
    Collection<String> fieldNames = doc.getFieldNames();
    for (String s: fieldNames) {
        System.out.println(doc.getFieldValue(s));
    }
}

回答by Jayendra

numFound gives you the total number of results that matched the Query.

numFound 为您提供与查询匹配的结果总数。

However, by default Solr will return only top 10 results which is controlled by parameter rows.
You are trying to iterate over numFound, However as the results returned are only 10 it fails.
You should use the rows parameter for Iteration.

但是,默认情况下,Solr 将仅返回由参数rows控制的前 10 个结果。
您正在尝试迭代 numFound,但是由于返回的结果只有 10 个,所以它失败了。
您应该使用行参数进行迭代。

For getting the next set of results, you would need to requery Solr with a different startparameter. This is to support pagination so that you don't have to pull all the results at one go which is a very heavy operation.

为了获得下一组结果,您需要使用不同的开始参数重新查询 Solr 。这是为了支持分页,这样您就不必一次拉出所有结果,这是一项非常繁重的操作。

回答by FrancescoM

If you refactor your code like this it will work

如果您像这样重构代码,它将起作用

String qry="*:*";
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setRows(Integer.MAX_VALUE); //Add me to avoid IndexOutOfBoundExc
CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr");
QueryResponse rsp=server.query(query);
SolrDocumentList docs=rsp.getResults();  
for(int i=0;i<docs.getNumFound();i++){
    System.out.println(docs.get(i));
            }

The answer to why it's quite simple.

原因很简单的答案。

The response is telling you that there are getNumFound()matching documents, but if you do not specify in your query how many of them the response must carry, this limit is automatically setted to 10,

响应告诉您有getNumFound()匹配文档,但如果您没有在查询中指定响应必须携带多少个文档,则此限制会自动设置为 10,

ending up fetching only the top 10 documents out of getNumFound() documents found

最终只从找到的 getNumFound() 文档中获取前 10 个文档

For this reason the docs list will have just 10 elements and trying to do the get of the i-th elementh with i > 9 (Eg 10) will take you to a

出于这个原因,文档列表将只有 10 个元素,并且尝试使用 i > 9(例如 10)获取第 i 个元素将带您到

java.lang.IndexOutOfBoundsException

java.lang.IndexOutOfBoundsException

just like you are experimenting.

就像你在试验一样。

P.S i suggest you to use the for iterator just like @Chen Sheng-Lun did.

PS 我建议你像@Chen Sheng-Lun 一样使用 for 迭代器。

P.P.S at first this drove me crazy too.

PPS 起初这也让我发疯。