我正在尝试通过Eclipse中的solrj查询solr。
我已经尝试了最新的solrj wiki示例:

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.ModifiableSolrParams;

import java.net.MalformedURLException;

public class SolrQuery2 {
  public static void main(String[] args) throws MalformedURLException, SolrServerException {
    SolrServer solr = new CommonsHttpSolrServer("http://localhost:8080/solr");

    // http://localhost:8080/solr/select/?q=outside
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("qt", "/select");
    params.set("q", "outside");

    QueryResponse response = solr.query(params);
    System.out.println("response = " + response);
  }
}
但是,无论如何我都无法克服此错误:
Exception in thread "main" java.lang.NoSuchMethodError:
    org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
接下来,我尝试了菜谱示例:
import java.util.Iterator;
import org.apache.solr.client.solrj.SolrQuery; //Error: The import org.apache.solr.client.solrj.SolrQuery conflicts with a type defined in the same file
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

public class SolrQuery {

      public static void main(String[] args) throws Exception {

          CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8080/solr");
          server.setParser(new XMLResponseParser());
          SolrQuery query = new SolrQuery();
          query.setQuery("document"); //Error: The method setQuery(String) is undefined for the type SolrQuery
          query.setStart(0); //Error: The method setStart(int) is undefined for the type SolrQuery
          query.setRows(10); //Error: The method setRows(int) is undefined for the type SolrQuery
          QueryResponse response = server.query(query); //Error: The method query(SolrParams) in the type SolrServer is not applicable for the arguments (SolrQuery)
          SolrDocumentList documents = response.getResults();
          Iterator<SolrDocument> itr = documents.iterator();
          System.out.println("DOCUMENTS");
          while(itr.hasNext()){
              SolrDocument doc = itr.next();
              System.out.println(doc.getFieldValue("id")+":"+doc.getFieldValue("content"));
          }

      }
    }
但是,由于我什至无法导入SolrQuery库,因此该示例的日期可能是当前api的日期。
有没有人提供一个快速的样例示例?
先感谢您。
PS。我正在使用tomcat7和solr 3.5运行Windows7。此时我要做的只是一个基本查询,然后将结果以某种列表,数组等形式返回。当我查询:firefox中的http://localhost:8080/solr/select/?q=outside时,结果恢复正常。

最佳答案

这是我如何在带有Eclipse的Windows7机器上运行Solrj(Solr 3.6)的方法:

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

  }
}

我必须下载其他jar(3.6的Solr之外)才能工作:httpcomponents-client-4.2-beta1

总共,我需要4个罐子才能使它工作:
  • apache-solr-solrj-3.6.0.jar
  • httpclient-4.2-beta1.jar
  • httpcore-4.2-beta1.jar
  • httpmime-4.2-beta1.jar

  • 我不确定我的解决方案在锅炉代码方面是否被认为是最佳实践,但是它解决了解决solrj w / eclipse的问题。

    08-26 08:54