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