通过Solrj查询Solr:基础知识 [英] Querying Solr via Solrj: Basics

查看:105
本文介绍了通过Solrj查询Solr:基础知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

      }
    }

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

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

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

Does anyone have a quick boilerplate example that works?

提前谢谢。

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

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=outside in firefox, the results come back just fine.

推荐答案

以下是我如何使用eclipse在我的Windows7盒子上运行Solrj(Solr 3.6):

Here is how I got Solrj (Solr 3.6) working on my Windows7 box with 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);

  }
} 

我必须下载额外的罐子(在3.6的Solr之外为此工作:httpcomponents-client-4.2-beta1

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

总共,我需要4个罐来实现这个功能:

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


  • 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

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

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.

这篇关于通过Solrj查询Solr:基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆