gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:00:01 25 4
gpt4 key购买 nike

我想用 solrj 获得所有结果,我向 Solr 添加了 10 个文档,我没有得到任何异常,但是如果我向 Solr 添加了超过 10 个文档Solr我遇到异常。我在 http://localhost:8983/solr/browse 中搜索,我为此得到了这个异常(exception) 第一页有10个文件,第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

最佳答案

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


}
}

关于java - 使用 Solrj 在 Solr 中获取所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com