6

I am trying to query solr via solrj in Eclipse. I have tried the latest solrj wiki example:

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

import java.net.MalformedURLException;

public class SolrQuery2 {
  public static void main(String[] args) throws MalformedURLException, SolrServerException {
    SolrServer solr = new CommonsHttpSolrServer("http://localhost:8080/solr");

    // http://localhost:8080/solr/select/?q=outside
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("qt", "/select");
    params.set("q", "outside");

    QueryResponse response = solr.query(params);
    System.out.println("response = " + response);
  }
}

However, I cant get past this error no matter what I do:

Exception in thread "main" java.lang.NoSuchMethodError: 
    org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V

Next, I tried the cookbook example:

import java.util.Iterator;
import org.apache.solr.client.solrj.SolrQuery; //Error: The import org.apache.solr.client.solrj.SolrQuery conflicts with a type defined in the same file
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

public class SolrQuery {
    
      public static void main(String[] args) throws Exception {

          CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8080/solr");
          server.setParser(new XMLResponseParser());
          SolrQuery query = new SolrQuery();
          query.setQuery("document"); //Error: The method setQuery(String) is undefined for the type SolrQuery
          query.setStart(0); //Error: The method setStart(int) is undefined for the type SolrQuery
          query.setRows(10); //Error: The method setRows(int) is undefined for the type SolrQuery
          QueryResponse response = server.query(query); //Error: The method query(SolrParams) in the type SolrServer is not applicable for the arguments (SolrQuery)
          SolrDocumentList documents = response.getResults();
          Iterator<SolrDocument> itr = documents.iterator();
          System.out.println("DOCUMENTS");
          while(itr.hasNext()){
              SolrDocument doc = itr.next();
              System.out.println(doc.getFieldValue("id")+":"+doc.getFieldValue("content"));
          }
          
      }
    }

However, that example might be dated for the current api as I cant even import the SolrQuery library.

Does anyone have a quick boilerplate example that works?

Thank you in advance.

PS. I am running windows7 with tomcat7 and solr 3.5. All I am trying to do at this point is a basic query and get the results back in some kind of list, array, whatever. When I query: http://localhost:8080/solr/select/?q=outside in firefox, the results come back just fine.

Community
  • 1
  • 1
Chris
  • 18,075
  • 15
  • 59
  • 77

2 Answers2

6

Here is how I got Solrj (Solr 3.6) working on my Windows7 box with eclipse:

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

public class SolrQuery {
  public static void main(String[] args) throws MalformedURLException, SolrServerException {
    SolrServer server = new HttpSolrServer("http://localhost:8080/solr");
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("q", "1");

            QueryResponse response = server.query(params);

            System.out.println("response = " + response);

  }
} 

I had to download additional jars (outside Solr of 3.6) for this to work: httpcomponents-client-4.2-beta1

In total, I needed 4 jars to get this working :

  • apache-solr-solrj-3.6.0.jar
  • httpclient-4.2-beta1.jar
  • httpcore-4.2-beta1.jar
  • httpmime-4.2-beta1.jar

Im not sure if my solution is considered a best practice in terms of boilercode, but it solves the issue of getting up on solrj w/ eclipse.

Aaron
  • 55,518
  • 11
  • 116
  • 132
Chris
  • 18,075
  • 15
  • 59
  • 77
  • 4
    I used exactly the Java example above, but I was required to have the following jar files in my build path. commons-logging-1.1.1.jar, httpclient-4.2.1.jar, httpcore-4.2.1.jar, httpmime-4.2.1.jar, slf4j-api-1.6.4.jar, slf4j-simple-1.6.4.jar, solr-solrj-3.6.0.jar – Adam Aug 09 '12 at 14:27
1

The org.slf4j.spi.LocationAwareLogger class is provided by the slf4j-api jar. What version are you running? Solr 3.5 requires version 1.6.1, I suspect you're using an eariler version.

If you're looking for a Solrj quick-start I'd recommend switching to Groovy. It can download the jar dependencies at run-time using Grab annotations. Example:

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Mark, thank you for your response. Just to be sure, I downloaded Solr 3.6, set it up and loaded in some documents. I created a new Eclipse proj & class then loaded the new jars (from 3.6), and now the library: org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; has a line through it and states: The type CommonsHttpSolrServer is deprecated and wont compile (errors out). Id rather stick with j2se since Im comfortable with it. Any more ideas are much appreciated. Thanks again. – Chris Apr 21 '12 at 18:55
  • Edit: Using: import org.apache.solr.client.solrj.SolrServer; instead of: org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; in 3.6 corrects issue above (original posted problem). However QueryResponse response = server.query(params); still fires back the same error above... Just fyi in case anyone runs into this as well... – Chris Apr 21 '12 at 20:29
  • That class has been deprectated by the Jira issue SOLR-2020. According to the Javadocs it is replaced by "HttpSolrServer" but this introduces new dependencies on the Apache Http Components libraries (See: http://wiki.apache.org/solr/ReleaseNote36) – Mark O'Connor Apr 21 '12 at 23:36
  • "org.apache.solr.client.solrj.SolrServer" is an abstract class... Not sure how this solved your issue as you cannot instantiate it – Mark O'Connor Apr 21 '12 at 23:43