使用solrj from java连接到solr时出错

vdgimpew  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(433)

在我的本地机器上,我安装了solrv8.4.1来做一些测试,并创建了一个核心,使用json文件添加了一些数据,并且可以连接到http://127.0.0.1:8983/solr/“。
我现在正尝试使用lotusdomino代理通过solrj(java)连接到示例。

solrServer = new HttpSolrServer("http://127.0.0.1:8983/solr/");
        solrServer.setSoTimeout(10000);
        solrServer.setConnectionTimeout(10000);
        solrServer.setDefaultMaxConnectionsPerHost(100);
        solrServer.setMaxTotalConnections(100);
        solrServer.setFollowRedirects(false);
        solrServer.setAllowCompression(true);
        solrServer.setMaxRetries(1);
       // solrServer.setParser(new XMLResponseParser());

        System.out.println(solrServer.getBaseURL());

        SolrQuery query = new SolrQuery();
        query.setQuery( "*:*" );
        try {
          System.out.println("queryyyy");
            QueryResponse rsp = solrServer.query( query );

            Iterator<SolrDocument> iter = rsp.getResults().iterator();

            while (iter.hasNext()) {
              SolrDocument resultDoc = iter.next();

              String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field
              System.out.println(id);
             // if (rsp.getHighlighting().get(id) != null) {
              //  List<String> highlightSnippets = rsp.getHighlighting().get(id).get("content");
             // }
            }

        } catch (SolrServerException e1) {
            System.out.println("error here");
            e1.printStackTrace();
        }

但是,当我尝试使用上面的代码建立连接时,出现以下错误。

org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://127.0.0.1:8983/solr/
    at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:562)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:210)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:206)
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:91)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
    at JavaAgent.init(Unknown Source)
    at JavaAgent.NotesMain(Unknown Source)
    at lotus.domino.AgentBase.runNotes(Unknown Source)
    at lotus.domino.NotesThread.run(Unknown Source)
Caused by: java.net.UnknownHostException:  :  
    at java.net.InetAddress.getAllByName0(InetAddress.java:1413)
    at java.net.InetAddress.getAllByName(InetAddress.java:1322)
    at java.net.InetAddress.getAllByName(InetAddress.java:1245)
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:260)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:160)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.executeMethod(HttpSolrServer.java:448)

它似乎无法通过此url连接到示例。
任何想法都将不胜感激。

vwhgwdsa

vwhgwdsa1#

我设法找出了我的问题所在。
我试图连接到solrv8.4.1版本,但使用的是来自版本4的solrjjar。当我将solrjjar更新到v8.4.1时,它开始工作了。
谢谢你的回复。

u4vypkhs

u4vypkhs2#

请在core中找到可用数据的屏幕截图。

下面是从核心获取数据的代码。

public void getResult() throws SolrServerException, IOException {

        final SolrClient client = getSolrClient();
        SolrQuery params = new SolrQuery();
        params.setQuery("*:*");
        try {
            QueryResponse queryResponse = client.query(params);
            Iterator<SolrDocument> iter = queryResponse.getResults().iterator();

            while (iter.hasNext()) {
                SolrDocument resultDoc = iter.next();
                String id = (String) resultDoc.getFieldValue("id");
                Collection<Object> manufacturerS = resultDoc.getFieldValues("manufacturer_s");
                String siteName = (String) resultDoc.getFieldValue("site_name");
                String title = (String) resultDoc.getFieldValue("title");

                System.out.println("Solr Response -> id :: " + id + " manufacturerS :: " + manufacturerS
                        + " siteName :: " + siteName + " title :: " + title);
            }

        } catch (SolrServerException e1) {
            e1.printStackTrace();
        }

    }

    private SolrClient getSolrClient() {
        final String solrUrl = "http://10.224.143.172:8983/solr/knowledge_combined";
        return new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
    }

代码的输出是:

相关问题