Search code examples
solrsolrj

Is there any SolrJ documentation available for connecting with Solr 4.0 or ahead


I'm unable to connect to my Solr instance on Tomcat from SolrJ. I've been through the documentation shown at "http://wiki.apache.org/solr/Solrj" , but its pretty outdated - primarily because it has no reference on Solr cores. I'm wondering if somebody could point me to the latest documentation OR advise on how to connect SolrJ with Solr4.0 or ahead - although my instance is running just 1 core for now.

Here's my connection string: "localhost:8080/solr-example/collection1/". Do you know which jars to add along with solrj? Thats where the trouble might be. For examples, the SolrJ wiki references a jar called commons-codec-1.3.jar which is not to be found anywhere, in the solr 4.0 zip file.


Solution

  • Calling Solr from Java with SolrJ

    You can specify the Core directly in the URL.

    HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/my_core");
    
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(q);
    solrQuery.setStart(start);
    solrQuery.setRows(rows);
    
    QueryResponse response = server.query(solrQuery);
    

    HttpSolrServer is reusable for more queries.

    The communication between the Solr Server and SolrJ happens via HTTP with a custom binary format. Can you Browse the Solr Web Admin at http://localhost:8080/solr? Depending on your installation, you might need to adjust the port (8080 is default on Tomcat, jetty uses 8983).

    Also, did you deploy Solr with a generic name or did you include the Version? Than your URL would be http://localhost:8080/solr-4.2.1/my_core

    Dependencies

    These are the minimum dependencies you need for using SolrJ. Add these to your pom.xml, if you are using maven.

    <dependency>
      <artifactId>solr-solrj</artifactId>
      <groupId>org.apache.solr</groupId>
      <version>4.2.0</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.5.6</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.2</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    

    If you are not using maven, you need the following jars:

    • solr-solrj-4.2.0
    • zookeeper-3.4.5
    • commons-io-2.1
    • httpclient-4.2.3
    • httpcore-4.2.2
    • commons-codec-1.6
    • httpmime-4.2.3
    • wstx-asl-3.2.3
    • slf4j-simple-1.5.6
    • slf4j-api-1.7.2
    • commons-logging-1.1.1