2012-04-21 11 views
6

Sto cercando di interrogare solr tramite solrj in Eclipse. Ho provato l'ultimo solrj wiki esempio:Richiesta di Solr via Solrj: Nozioni di base

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

Tuttavia, non posso ottenere oltre questo errore, non importa quello che faccio:

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 

Avanti, ho provato l'esempio libro di cucina:

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

     } 
    } 

Tuttavia, quell'esempio potrebbe essere datato per l'API corrente poiché non posso nemmeno importare la libreria SolrQuery.

Qualcuno ha un esempio di piastra veloce che funziona?

Grazie in anticipo.

PS. Sto facendo funzionare windows7 con tomcat7 e solr 3.5. Tutto quello che sto cercando di fare a questo punto è una query di base e ottenere i risultati in una sorta di elenco, array, qualunque sia. Quando interrogo: http://localhost:8080/solr/select/?q=outside in firefox, i risultati tornano perfettamente.

risposta

6

Ecco come ho ottenuto Solrj (Solr 3.6) che lavorano sulla mia casella di Windows7 con 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); 

    } 
} 

ho dovuto scaricare vasi supplementari (al di fuori Solr di 3.6) per farlo funzionare: httpcomponents-client-4.2-beta1

In totale, avevo bisogno di 4 giare per farlo funzionare:

  • apache-solr-solrj-3.6.0.jar
  • httpclient-4,2-beta1.jar
  • httpcore-4,2-beta1.jar
  • httpmime-4,2-beta1.jar

Non sono sicuro che la mia soluzione sia considerata una best practice in termini di boilercode, ma risolve il problema di alzarmi su solrj w/eclipse.

+4

Ho usato esattamente l'esempio Java sopra, ma mi è stato richiesto di avere i seguenti file jar nel mio percorso di compilazione. 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

+0

+1 questo mi ha aiutato anche io. – Aaron

1

La classe org.slf4j.spi.LocationAwareLogger è fornita dal barattolo slf4j-api. Che versione stai usando? Solr 3.5 richiede la versione 1.6.1, ho il sospetto che stai usando una versione di eariler.

Se stai cercando un avvio rapido Solrj, ti consiglio di passare a Groovy. Può scaricare le dipendenze jar in fase di esecuzione utilizzando Grab annotations. Esempio:

+0

Mark, grazie per la risposta. Per sicurezza, ho scaricato Solr 3.6, configurato e caricato in alcuni documenti. Ho creato un nuovo proj e classe Eclipse, quindi caricato i nuovi jar (da 3.6) e ora la libreria: org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; ha una linea e dice: Il tipo CommonsHttpSolrServer è deprecato e non compilare (errori). L'id piuttosto si attacca a j2se poiché mi sento a mio agio con esso. Altre idee sono molto apprezzate. Grazie ancora. – Chris

+0

Modifica: Utilizzo: import org.apache.solr.client.solrj.SolrServer; invece di: org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; in 3.6 corregge il problema sopra (problema pubblicato originale). Tuttavia Risposta a QueryResponse = server.query (params); continua a restituire lo stesso errore sopra ... Solo fyi nel caso in cui qualcuno si imbatta anche in questo ... – Chris

+0

Quella classe è stata deprecata dal numero di Jira SOLR-2020. Secondo Javadocs è sostituito da "HttpSolrServer" ma questo introduce nuove dipendenze sulle librerie Apache Http Components (Vedi: http://wiki.apache.org/solr/ReleaseNote36) –