2017-04-15 118 views
0

我嘗試使用solr 6.5.0來連接java。我加入了以下.jar文件到圖書館:solr連接問題java

commons-io-2.5 
httpclient-4.4.1 
httpcore-4.4.1 
httpmine-4.4.1 
jcl-over-slf4j-1.7.7 
noggit-0.6 
slf4j-api-1.7.7 
stax2-api-3.1.4 
woodstox-core-asl-4.4.1 
zookeeper-3.4.6 
solr-solrj-6.5.0 

但是當我嘗試使用下面的代碼來連接Solr的:

import org.apache.http.impl.bootstrap.HttpServer; 
import org.apache.solr.client.solrj.SolrQuery; 
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.SolrDocumentList; 

public class SolrQuery { 

    public static void main(String[] args) throws SolrServerException { 
    HttpSolrServer solr = new HttpServer("http://localhost:8983/solr/collection1"); 
    SolrQuery query = new SolrQuery(); 
    query.setQuery("*"); 
    QueryResponse response = solr.query(query); 
    SolrDocumentList results = response.getResults(); 
    for (int i = 0; i < results.size(); ++i) { 
     System.out.println(results.get(i)); 
    } 
    } 
} 

之前,我編譯它,我得到了在一個錯誤:

import org.apache.solr.client.solrj.impl.HttpSolrServer; 
import org.apache.solr.client.solrj.SolrQuery; 
HttpSolrServer solr = new HttpServer("http://localhost:8983/solr/collection1"); 

任何人都可以幫助我如何解決它?

+0

你會得到什麼錯誤? – MatsLindh

+0

我不能編譯代碼,當我運行的代碼,它表示:HttpSolrServer不能解析爲一個類型 \t構造的HttpServer(字符串)是未定義 \t方法setQuery(字符串)是未定義的類型SolrQuery –

回答

1

您的問題中的一段代碼是針對舊版本的Solr編寫的。 5.0。你會發現很多來源和例子都是針對舊的Solr版本編寫的,但在大多數情況下,你所要做的就是用新的SolrClient(現在是正確的)類更改舊的SolrServer類。

兩者都是您要使用的Solr實例的表示形式。

閱讀的Solr Documentation - Using SolrJ

我熱烈建議不適合你的類已經存在的類同名使用(在你的例子類名爲SolrQuery)。

捕獲Solr查詢的所有字符串是*:*這意味着:搜索任何匹配的所有可用字段。所以更改語句query.setQuery到:

query.setQuery("*:*"); 

我想你正在使用的Solr客戶端的獨立實例是這樣,因爲你已經知道,正確的方法爲實例SolrClient是:

String urlString = "http://localhost:8983/solr/gettingstarted"; 
SolrClient solr = new HttpSolrClient.Builder(urlString).build(); 

,這是一個更簡單的方法,我建議在所有返回的文檔進行迭代:

for (SolrDocument doc : response.getResults()) { 
    System.out.println(doc); 
} 

看一看SolrDocument C的文檔解釋如何使用它並正確地讀取字段值。

1

我創辦我需要導入未在其名爲slf4j-simple-1.7.25的/ DIST庫包含一個.jar文件,也

HttpSolrServer solr = new HttpServer("http://localhost:8983/solr/gettingstarted"); 
     SolrQuery query = new SolrQuery(); 

需要改變的

String urlString = "http://localhost:8983/solr/gettingstarted"; 
    SolrClient solr = new HttpSolrClient.Builder(urlString).build(); 

後它終於可以運行了!