我想通過Eclipse中的solrj查詢solr。 我已經嘗試了最新solrj wiki例如:通過Solrj查詢Solr:基礎知識
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);
}
}
但是,我不能讓過去的這個錯誤,無論我做什麼:
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
接下來,我嘗試了食譜例如:
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"));
}
}
}
但是,這個例子可能會針對當前api過期,因爲我甚至不能導入SolrQuery庫。
有沒有人有一個快速樣板工程?
預先感謝您。
PS。我使用tomcat7和solr 3.5運行windows7。我現在想要做的只是一個基本的查詢,並將結果返回到某種列表,數組等等。當我在Firefox中查詢:http://localhost:8080/solr/select/?q=outside
時,結果回來就好了。
我完全使用了上面的Java示例,但我需要在我的構建路徑中具有以下jar文件。 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 2012-08-09 14:27:53
+1這也幫助了我。 – Aaron 2012-08-17 19:01:06