在不搜索的情况下从Solr中读取所有文档(仅在可能的情况下才为文档ID)

时间:2019-01-08 22:58:25

标签: solr solrj

我知道Solr是用于搜索的。

但是,我正在做一些基准测试,我想知道是否有一种方法可以检索每个已编制索引的文档的文档ID。

最好的选择是检索而不进行搜索(如果存在的话)。

我想替代方法是查询所有文档,但只要求提供文档ID。

我将使用SolrJ,因此SolrJ的操作将很有用

1 个答案:

答案 0 :(得分:1)

使用/export端点:Exporting result sets

它支持使用与常规搜索相同的fl参数(尽管使用SolrJ时仅搜索*:*的行为可能非常相似)。

在SolrJ中,您必须使用CloudSolrStream类来正确地传输结果(与搜索*:*时的常规行为相比)。

来自Joel Bernstein's example when introducing the feature

import org.apache.solr.client.solrj.io.*;
import java.util.*;

public class StreamingClient {

   public static void main(String args[]) throws IOException {
      String zkHost = args[0];
      String collection = args[1];

      Map props = new HashMap();
      props.put("q", "*:*");
      props.put("qt", "/export");
      props.put("sort", "fieldA asc");
      props.put("fl", "fieldA,fieldB,fieldC");

      CloudSolrStream cstream = new CloudSolrStream(zkHost, 
                                                    collection, 
                                                    props);
      try {

        cstream.open();
        while(true) {

          Tuple tuple = cstream.read();
          if(tuple.EOF) {
             break;
          }

          String fieldA = tuple.getString("fieldA");
          String fieldB = tuple.getString("fieldB");
          String fieldC = tuple.getString("fieldC");
          System.out.println(fieldA + ", " + fieldB + ", " + fieldC);
        }

      } finally {
       cstream.close();
      }
   }
}