如何使用 SolrJ 获取 solr 服务器中所有核心的列表

发布于 2024-11-19 14:09:57 字数 197 浏览 3 评论 0原文

我们使用 Solr 进行搜索,并将数据分片到多个核心。我们每周有一个核心的数据,因此我们每周都会动态创建和删除核心。

如何查询 solr 服务器以获取其所有核心的列表? JavaDoc 说我可以使用 coreAdminHandler.getCoreContainer().getCoreNames() ,但我不确定如何构建 coreAdminHandler 对象。

We are using Solr for our searches, and sharding the data across several cores. We have one core per week of data, so we are dynamically creating and deleting cores each week.

How can I query a solr server for a list of all its cores? The JavaDoc says I can use coreAdminHandler.getCoreContainer().getCoreNames(), but I'm not sure how to build a coreAdminHandler object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2024-11-26 14:09:57

对 http://localhost:8983/solr/admin/cores?action=STATUS 的请求(当然替换您自己的主机/端口)将返回所有核心。

A request to http://localhost:8983/solr/admin/cores?action=STATUS (replace your own host/port of course) will return all cores.

三月梨花 2024-11-26 14:09:57

按照您的要求使用 SolrJ ,这是我的做法:

// Solr server instance
CommonsHttpSolrServer solrServer = ...;

// Request core list
CoreAdminRequest request = new CoreAdminRequest();
request.setAction(CoreAdminAction.STATUS);
CoreAdminResponse cores = request.process(solrServer);

// List of the cores
List<String> coreList = new ArrayList<String>();
for (int i = 0; i < cores.getCoreStatus().size(); i++) {
    coreList.add(cores.getCoreStatus().getName(i));
}

Using SolrJ as you asked, here is how I did:

// Solr server instance
CommonsHttpSolrServer solrServer = ...;

// Request core list
CoreAdminRequest request = new CoreAdminRequest();
request.setAction(CoreAdminAction.STATUS);
CoreAdminResponse cores = request.process(solrServer);

// List of the cores
List<String> coreList = new ArrayList<String>();
for (int i = 0; i < cores.getCoreStatus().size(); i++) {
    coreList.add(cores.getCoreStatus().getName(i));
}
街道布景 2024-11-26 14:09:57

只需对上面的代码示例添加更新,因为自 Solr 4 以来已弃用了一些位。以下代码适用于 Solr 6.1.0。

package <...>.<...>.<...>;    

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;    

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.response.CoreAdminResponse;
import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction;    

public class GetCores {
    static String SOLR_URL = "http://...:8983/solr/";    

    public static void getCores() {
        System.out.println("Building Solr server instance");
        HttpSolrClient solrClient=new HttpSolrClient.Builder(SOLR_URL).build();    

        System.out.println("Requesting core list"); 
        CoreAdminRequest request = new CoreAdminRequest();
        request.setAction(CoreAdminAction.STATUS);
        CoreAdminResponse cores=null;

        try {
            cores = request.process(solrClient);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(" Listing cores");
        List<String> coreList = new ArrayList<String>();
        for (int i = 0; i < cores.getCoreStatus().size(); i++) {
            coreList.add(cores.getCoreStatus().getName(i));
        }

        System.out.println(coreList.get(0)+" is the first core");    
    }       
}

Just adding an update to the code sample above as several bits have been deprecated since Solr 4. The following code works on Solr 6.1.0.

package <...>.<...>.<...>;    

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;    

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.response.CoreAdminResponse;
import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction;    

public class GetCores {
    static String SOLR_URL = "http://...:8983/solr/";    

    public static void getCores() {
        System.out.println("Building Solr server instance");
        HttpSolrClient solrClient=new HttpSolrClient.Builder(SOLR_URL).build();    

        System.out.println("Requesting core list"); 
        CoreAdminRequest request = new CoreAdminRequest();
        request.setAction(CoreAdminAction.STATUS);
        CoreAdminResponse cores=null;

        try {
            cores = request.process(solrClient);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(" Listing cores");
        List<String> coreList = new ArrayList<String>();
        for (int i = 0; i < cores.getCoreStatus().size(); i++) {
            coreList.add(cores.getCoreStatus().getName(i));
        }

        System.out.println(coreList.get(0)+" is the first core");    
    }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文