Thinkpad
Thinkpad

Reputation: 101

Error Connecting to Solr using Solrj from Java

On my local machine I have installed Solr v8.4.1 to do some testing and have created a core, added some data using json file and can connect to the Admin client on "http://127.0.0.1:8983/solr/".

I am now trying to connect to the instance via Solrj (Java) using a Lotus Domino agent.

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

However when I try and make a connection using the above code, I get the following error.

    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)

It appears that it is having trouble connecting to the instance via this URL.

Any ideas would be greatly appreciated.

Upvotes: 0

Views: 1358

Answers (2)

Thinkpad
Thinkpad

Reputation: 101

I managed to work out what my problem was.

I was trying to connect to a version of Solr v8.4.1 but was using the SolrJ jar from version 4. When I updated my SolrJ jar to v8.4.1 it started to work.

Thanks for the replies.

Upvotes: 1

Abhijit Bashetti
Abhijit Bashetti

Reputation: 8658

Please find the the screenshot of the data available in the core.

Solr Core

Here is the code that fetches the data from the 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();
    }

The output of the code is :

SOlr Code output

Upvotes: 0

Related Questions