java 通过 Solrj 查询 Solr:基础知识

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10260237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:16:12  来源:igfitidea点击:

Querying Solr via Solrj: Basics

javasolrlucenesolrj

提问by Chris

I am trying to query solr via solrj in Eclipse. I have tried the latest solrj wikiexample:

我想在 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);
  }
}

However, I cant get past this error no matter what I do:

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

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

Next, I tried the cookbook example:

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

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

      }
    }

However, that example might be dated for the current api as I cant even import the SolrQuery library.

但是,该示例对于当前 api 可能已过时,因为我什至无法导入 SolrQuery 库。

Does anyone have a quick boilerplate example that works?

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

Thank you in advance.

先感谢您。

PS. I am running windows7 with tomcat7 and solr 3.5. All I am trying to do at this point is a basic query and get the results back in some kind of list, array, whatever. When I query: http://localhost:8080/solr/select/?q=outsidein firefox, the results come back just fine.

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

采纳答案by Chris

Here is how I got Solrj (Solr 3.6) working on my Windows7 box with eclipse:

这是我如何让 Solrj (Solr 3.6) 在我的 Windows7 机器上使用 eclipse 工作:

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

  }
} 

I had to download additional jars (outside Solr of 3.6) for this to work: httpcomponents-client-4.2-beta1

我必须下载额外的 jars(在 3.6 的 Solr 之外)才能工作:httpcomponents-client-4.2-beta1

In total, I needed 4 jars to get this working :

总的来说,我需要 4 个罐子才能让它工作:

  • apache-solr-solrj-3.6.0.jar
  • httpclient-4.2-beta1.jar
  • httpcore-4.2-beta1.jar
  • httpmime-4.2-beta1.jar
  • apache-solr-solrj-3.6.0.jar
  • httpclient-4.2-beta1.jar
  • httpcore-4.2-beta1.jar
  • httpmime-4.2-beta1.jar

Im not sure if my solution is considered a best practice in terms of boilercode, but it solves the issue of getting up on solrj w/ eclipse.

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

回答by Mark O'Connor

The org.slf4j.spi.LocationAwareLoggerclass is provided by the slf4j-api jar. What version are you running? Solr 3.5 requires version 1.6.1, I suspect you're using an eariler version.

org.slf4j.spi.LocationAwareLogger类是由SLF4J-API JAR提供。你运行的是什么版本?Solr 3.5 需要 1.6.1 版,我怀疑您使用的是早期版本。

If you're looking for a Solrj quick-start I'd recommend switching to Groovy. It can download the jar dependencies at run-time using Grab annotations. Example:

如果您正在寻找 Solrj 快速入门,我建议您切换到 Groovy。它可以在运行时使用Grab annotations下载 jar 依赖项。例子: