Connect a Java project to Apache Solr 4.10.3

0

I have Apache Solr 4.10.3 running a collection called "Collection1". I also have a project in Maven at the moment completely clean and ready to put together. The first thing I want to do is connect to Solr to get indexed data from Collection1. On the internet there is a lot of data on how to connect to the database but, either they are not very clear, or they do not correspond to the version, or they make a connection without pointing to a specific collection:

import java.net.MalformedURLException;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;

public class Connection {
    private static HttpSolrServer server;

    public static HttpSolrServer init() throws MalformedURLException{
        server = new HttpSolrServer("http://localhost:8983/solr/collection1/");
        return server;
    }

}

Any ideas on how to connect Solr? Greetings.

Update: I found a code that can work, that adds the parameters of the query by means of setters, the problem is that I do not recognize the throws command to implement the exception:

import java.net.MalformedURLException;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;

public class Connection throws MalformedURLException, SolrServerException {
SolrServer server = new HttpSolrServer("localhost:8983/solr/Collection1");


ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/spellCheckCompRH");
params.set("q", "epod");
params.set("spellcheck", "on");
params.set("spellcheck.build", "true");

QueryResponse response = server.query(params);


}
    
asked by Darius 03.02.2017 в 21:14
source

1 answer

0

Solved, to connect:

import java.net.MalformedURLException;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;

public class Connection {
public static HttpSolrServer server;
public static void init() throws MalformedURLException, SolrServerException{
    server = new HttpSolrServer("http://localhost:8983/solr/collection1/");
    server.setParser(new XMLResponseParser());
    }

}
    
answered by 06.02.2017 / 22:04
source