0

hi im working on several sample programs for solrj and im having trouble determining if i connected it correcttly or not..this is my code

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.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;

import java.net.MalformedURLException;

public class SolrExample {
  public static void main(String[] args) throws MalformedURLException,    SolrServerException {
//SolrServer solr = new CommonsHttpSolrServer("localhost:8983/solr");
SolrServer server = new HttpSolrServer("localhost:8983/solr/");


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);
System.out.println("response = " + response);
  }
}

the output of the code above is: {time=219.0,sub1={time=125.0,sub1.1={time=0.0}}}

on the other hand if im using this:

     SolrServer solr = new CommonsHttpSolrServer("localhost:8983/solr");

it is said that is is depracated..

im confused because i can access the solr admin in my computer thru localhost but i cant in my program..

big thanks if anyone can help

y.reyes
  • 117
  • 1
  • 2
  • 8

2 Answers2

2
  • For Solrj 4.x you must use HttpSolrServer:
    SolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr");
    or "http://localhost:8983/solr/solrCoreName"

  • Bonus, for Solr Cloud, you can use CloudSolrServer with parameter the url to Apache ZooKeeper:
    SolrServer cloudSolrServer = new CloudSolrServer("htt p://localhost:2181");

Check Apache Solr wiki page for more informations: wiki.apache.org/solr/FrontPage

samgak
  • 23,944
  • 4
  • 60
  • 82
silviu.ct
  • 101
  • 1
  • 5
1

Shouldn't you specify the protocol on the URL:

SolrServer solr = new CommonsHttpSolrServer("http://localhost:8983/solr");

I did simple test and I get an error if I do not do so. I am however using Solr 3.4 in my project... I do not get the "deprecated" warning, according to the documentation it is indeed advised to use "HttpSolrServer" in later versions of solr (http://lucene.apache.org/solr/3_6_0/org/apache/solr/client/solrj/impl/CommonsHttpSolrServer.html).

emgsilva
  • 3,047
  • 1
  • 17
  • 16
  • i edited the sample program and used the url you stated: SolrServer solr = new CommonsHttpSolrServer("http://localhost:8983/solr"); but i still recieve the deprecated warning. im using solr 3.6.0 and also tried solr 4.2.1 im going to try solr 3.4..thanks – y.reyes Jul 23 '13 at 08:39
  • you need to specify "HTTP://", I do not see it in your code snippet. In Solr 3.4 CommonHttpSolrServer is not deprecated, but if you are starting using Solr you should go for a more recent version, and use the HttpSolrServer as suggested in the documentation. – emgsilva Jul 23 '13 at 08:43