我在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
我该如何解决?
答案 0 :(得分:0)
在服务器URL中添加集合/核心名称,即索引文档的位置。
SolrServer server = 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));
}
}
}