【问题标题】:SolrJ - Asynchronously indexing documents with ContentStreamUpdateRequestSolrJ - 使用 ContentStreamUpdateRequest 异步索引文档
【发布时间】:2014-08-27 17:15:04
【问题描述】:

我正在使用 SolrJ API 4.8 将丰富的文档索引到 solr。但我想异步索引这些文档。我制作的函数同步发送文档,但我不知道如何更改它以使其异步。有什么想法吗?

功能:

public Boolean indexDocument(HttpSolrServer server, String PathFile, InputReader external)
{  

        ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");

        try {
                up.addFile(new File(PathFile), "text");
        } catch (IOException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;
        }

        up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);

        try {
                server.request(up);
        } catch (SolrServerException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;

        } catch (IOException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;   
        }
        return true;
}

Solr 服务器:4.8 版

【问题讨论】:

    标签: asynchronous solr indexing request solrj


    【解决方案1】:

    听起来你可能想看看使用 ExecutorService 和 FutureTask 来做到这一点:

    private static HttpSolrServer server;
    private static int threadPoolSize = 4;  //Set this to something appropiate for your environment
    
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
        ArrayList<FutureTask<Boolean>> taskList = new ArrayList<FutureTask<Boolean>>();
        ArrayList<String> paths = new ArrayList<String>();
        //Initialize your list of paths here
    
        for (String path : paths) {
            FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new IndexDocumentTask(path));
            taskList.add(futureTask);
            executor.execute(futureTask);
        }
    
        for (int i = 0; i < taskList.size(); i++) {
            FutureTask<Boolean> futureTask = taskList.get(i);
    
            try {
                System.out.println("Index Task " + i + (futureTask.get() ? " finished successfully." : " encountered an error."));
            } catch (ExecutionException e) {
                System.out.println("An Execution Exception occurred with Index Task " + i);
            } catch (InterruptedException e) {
                System.out.println("An Interrupted Exception occurred with Index Task " + i);
            }
        }
    
        executor.shutdown();
    }
    
    static class IndexDocumentTask implements Callable<Boolean> {
    
        private String pathFile;
    
        public IndexDocumentTask(String pathFile) {
            this.pathFile = pathFile;
        }
    
        @Override
        public Boolean call() {
            return indexDocument(pathFile);
        }
    
        public Boolean indexDocument(String pathFile) {
            ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
    
            try {
                up.addFile(new File(pathFile), "text");
            } catch (IOException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;
            }
    
            up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    
            try {
                server.request(up);
            } catch (SolrServerException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;
    
            } catch (IOException e) {
                Logger.getLogger(ANOIndexer.class.getName()).log(Level.SEVERE, null, e);
                return false;
            }
            return true;
        }
    }
    

    这是未经测试的代码,所以我不确定像这样调用server.request(up) 是否是线程安全的。我认为只使用一个 HttpSolrServer 实例更简洁,但您也可以在每个任务中创建新的 HttpSolrServer 实例。

    如果您愿意,您可以扩充 IndexDocumentTask 以实现 Callable&lt;Tuple&lt;String, Boolean&gt;&gt;,这样您就可以检索要索引的文档的文件名,以及索引是否成功。

    尽管我不认为一次向 Solr 服务器发送多个请求应该是个问题,但您可能希望限制您的请求,以免使 Solr 服务器过载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-27
      • 2011-06-30
      • 2013-02-08
      • 1970-01-01
      • 2016-05-03
      • 2018-08-27
      • 1970-01-01
      • 2019-12-15
      相关资源
      最近更新 更多