trouble in solr connect java

有些话、适合烂在心里 提交于 2019-12-24 06:29:14

问题


I try to use solr 6.5.0 to connect java . I have added following .jar files to the library:

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

but when i try use following code to connect the 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));
    }   
  }   
}

before i compile it, I got an error in the:

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

Can anyone help me how to solve it?


回答1:


The piece of code in your question was written for an old version of Solr before ver. 5.0. You'll find many sources and example around written for old Solr versions, but in most of the cases all you have to do is change the old SolrServer class with the new SolrClient (and now correct) class.

Both were the representations of the Solr instances you want to use.

Read the Solr Documentation - Using SolrJ

I warmly suggest to not use for your classes the same name of an already existing class (in your example your class is named SolrQuery).

The catch all string for Solr queries is *:* which means: search any match for all available fields. So change the statement query.setQuery into:

query.setQuery("*:*"); 

I suppose you're using a Solr client for a standalone instance so, as you're already aware, the correct way to instance a SolrClient is:

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

And this is an easier way I suggest to iterate through all returned document:

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

Have a look at the documentation of SolrDocument class that explain how to use it and correctly read field values.




回答2:


I founded that i need to import a .jar file which is not contain in the /dist library which named slf4j-simple-1.7.25 , and also

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

need to change to the

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

after that it finally can run already!!!



来源:https://stackoverflow.com/questions/43425136/trouble-in-solr-connect-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!