【问题标题】:solrJ error in HttpSolrServer and SolrServerHttpSolrServer 和 SolrServer 中的 solrJ 错误
【发布时间】:2017-04-13 07:05:26
【问题描述】:

我已经在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 或设置集合名称。
  • @vinod 我是 solr 的新手,可以更具体一点吗?
  • 检查答案。
  • 这听起来像是一个 Eclipse 问题——您使用的是 Eclipse 吗?你是怎么编译的?您如何尝试运行该程序?你是如何将罐子添加到项目中的?你加了哪些罐子?
  • 是 .jar 文件的原因吗?我已经导入:commons-io-2.5,httpclient-4.4.1,httpcore-4.4.1,httpmime-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 solrj


【解决方案1】:

在服务器 URL 中添加您索引文档的集合/核心名称。

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

【讨论】:

  • sry,我刚试过你的代码,得到了同样的错误:线程“main”java.lang.Error中的异常:未解决的编译问题:构造函数HttpSolrServer(String)未定义方法setQuery(String ) 对于类型 SolrQuery 未定义 对于类型 HttpSolrServer 未定义方法 query(SolrQuery) 类型 java.util.Map$Entry 无法解析。它是从所需的 .class 文件中间接引用的
  • 你添加了 solrJ jar 文件吗?
  • 这些是我导入的文件:commons-io-2.5,httpclient-4.4.1,httpcore-4.4.1,httpmime-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
  • 编译前是否显示错误。 ?我猜你还没有下载 jars 并正确地添加到你的 java 项目类路径中。重新检查。
  • 是的,导入 org.apache.solr.client.solrj.impl.HttpSolrServer;并导入 org.apache.solr.client.solrj.SolrQuery;在红线中,但是我已经导入了 solr lib 中的所有 jar 文件,我很困惑
猜你喜欢
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
相关资源
最近更新 更多