HttpSolrServer 和 SolrServer 中的 solrJ 错误

solrJ error in HttpSolrServer and SolrServer

我已经在java库中添加了.jar文件,并尝试连接到solr,代码如下:

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

  }
} 

但是当我尝试 运行 程序时,出现错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    SolrServer cannot be resolved to a type
    HttpSolrServer cannot be resolved to a type
    The type org.apache.solr.common.params.SolrParams cannot be resolved. It is indirectly referenced from required .class files

我该如何解决?

在服务器 URL 添加 collection/core 名称,您在其中索引文档。

SolrServer 服务器 = new HttpSolrServer("http://localhost:8080/solr/collection");

示例Java代码供您参考。

* 给出索引到 solr 中的所有文档。将 * 更改为您要搜索的关键字字符串。

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 SearchSolr {

    public static void main(String[] args) throws SolrServerException {
        HttpSolrServer solr = new HttpSolrServer("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));
        }   
    }   
}