2017-04-15 74 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(); 

后它终于可以运行了!