通过Solrj查询Solr:基础知识

Chr*_*ris 6 java lucene solr solrj

我试图通过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);
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,无论我做什么,我都无法克服这个错误:

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
Run Code Online (Sandbox Code Playgroud)

接下来,我尝试了食谱示例:

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

      }
    }
Run Code Online (Sandbox Code Playgroud)

但是,该示例可能是当前api的日期,因为我甚至无法导入SolrQuery库.

有没有人有一个快速的样板示例?

先感谢您.

PS.我正在使用tomcat7和solr 3.5运行windows7.我在这一点上所做的只是一个基本的查询,并将结果返回到某种列表,数组,等等.当我查询:http://localhost:8080/solr/select/?q=outside在Firefox中,结果回来就好了.

Chr*_*ris 6

以下是我如何使用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);

  }
} 
Run Code Online (Sandbox Code Playgroud)

我必须下载额外的罐子(在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的问题.

  • 我完全使用了上面的Java示例,但我需要在构建路径中包含以下jar文件.commons-logging-1.1.1.jar,httpclient-4.2.1.jar,httpcore-4.2.1.jar,httpmime-4.2.1.jar,slf4j-api-1.6.4.jar,slf4j-simple-1.6. 4.jar,solr-solrj-3.6.0.jar (4认同)