Solr Request via Solrj: Basics

I am trying to request solr via solrj in Eclipse. I tried the last 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 can not get through 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 

Then 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, this example may be deprecated for the current api, since I cannot even import the SolrQuery library.

Does anyone have a quick template example that works?

Thanks in advance.

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

+6
source share
2 answers

This is how I got Solrj (Solr 3.6) working on my Windows7 mailbox 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 banks (except Solr 3.6) for this to work: httpcomponents-client-4.2-beta1

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

  • Apache-Solr-solrj-3.6.0.jar
  • HttpClient-4,2-beta1.jar
  • httpcore-4,2-beta1.jar
  • httpmime-4,2-beta1.jar

I'm not sure if my solution is considered best practice regarding boilercode, but it solves the problem of getting up to solrj w / eclipse.

+6
source

The org.slf4j.spi.LocationAwareLogger class is provided by the slf4j-api bank. Which version are you using? Solr 3.5 requires version 1.6.1, I suspect you are using the headphone version.

If you are looking for a quick start to Solrj, I would recommend switching to Groovy. It can load jar dependencies at runtime using Capture Annotations . Example:

+1
source

Source: https://habr.com/ru/post/913796/


All Articles