Search code examples
javasolrsolrj

Get All Result in Solr with Solrj


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/browse 10 document in first page,11th document go to second page. How I can get all result?

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


Solution

  •     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();
    
    
        }
    }