java 使用 SolrJ 查询 Solr 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13944567/
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 14:31:49  来源:igfitidea点击:

Querying Solr server using SolrJ

javasolrsolrj

提问by Pete

I'm trying to query my solr database based off the code provided in this questionwith SolrJ but it keeps throwing a null pointer exception.

我正在尝试根据此问题中提供的代码使用 SolrJ查询我的 solr 数据库,但它不断抛出空指针异常。

My code is:

我的代码是:

@PUT
@Produces(MediaType.TEXT_PLAIN)
public String returnText(String url) throws MalformedURLException, SolrServerException{
    SolrServer server = new HttpSolrServer("http://localhost:8080/apache-solr-1.4.0");

    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("?q", "*:*");
    params.set("facet", true);
    params.set("rows", 5);

    QueryResponse response = server.query(params);
    System.out.println(response);
    return "success";
}

and if I run this url: http://localhost:8080/apache-solr-1.4.0/select/?q=*:*&facet=true&rows=5in my browser then it returns the correct values however if I enter: http://localhost:8080/apache-solr-1.4.0/select/q=*:*&facet=true&rows=5then I get the exact same error as when I run it in eclipse, the error is:

如果我http://localhost:8080/apache-solr-1.4.0/select/?q=*:*&facet=true&rows=5在我的浏览器中运行这个 url:然后它返回正确的值但是如果我输入:http://localhost:8080/apache-solr-1.4.0/select/q=*:*&facet=true&rows=5然后我得到与在 eclipse 中运行它时完全相同的错误,错误是:

19/12/2012 1:09:01 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.apache.solr.client.solrj.SolrServerException: Server at http://localhost:8080/apache-solr-1.4.0 returned non ok status:500, message:null  java.lang.NullPointerException    at java.io.StringReader.<init>(StringReader.java:33)    at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:197)    at org.apache.solr.search.LuceneQParser.parse(LuceneQParserPlugin.java:78)  at org.apache.solr.search.QParser.getQuery(QParser.java:131)    at org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:89)     at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:174)    at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:131)    at org.apache.solr.core.SolrCore.execute(SolrCore.java:1316)    at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:338)  at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:241)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)   at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)   at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)   at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)   at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)  at java.lang.Thread.run(Thread.java:619)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:328)
    at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:211)
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:89)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
    at geoportal.webservice.download.returnText(download.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    .......

If I change my code to params.set("q","*:*");then I get this error:

如果我将代码更改为,params.set("q","*:*");则会收到此错误:

19/12/2012 1:13:30 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.apache.solr.client.solrj.SolrServerException: Error executing query
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:95)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
    at geoportal.webservice.download.returnText(download.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      .......

Any ideas as to what I've done wrong?

关于我做错了什么的任何想法?

Thanks heaps

谢谢堆

EDIT

编辑

Here is my new code for the solrQuery based off this:

这是我基于的 solrQuery 新代码:

@PUT
@Produces(MediaType.TEXT_PLAIN)
public String returnText(String url) throws MalformedURLException, SolrServerException{
    SolrServer server = new HttpSolrServer("http://localhost:8080/apache-solr-1.4.0");
    SolrQuery query = new SolrQuery();

    query.setQuery("*:*");
    query.setFacet(true);
    query.set("wt", "json");
    query.setRows(5);
    query.setStart(0);

    QueryResponse response = server.query(query);

    System.out.println(response);
    return "success";
}

But it still isn't working :( Console output:

但它仍然无法正常工作:( 控制台输出:

19/12/2012 2:24:33 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.apache.solr.client.solrj.SolrServerException: Error executing query
    at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:95)
    at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
    at geoportal.webservice.download.returnText(download.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          ......

EDIT

编辑

I put a System.out.println(query.toString());in my code and it outputted:

System.out.println(query.toString());在我的代码中放入了一个,它输出了:

q=*%3A*&facet=true&wt=json&rows=5&start=0

If I enter this into my browser like

如果我将其输入到我的浏览器中

http://localhost:8080/apache-solr-1.4.0/select/q=*%3A*&facet=true&wt=json&rows=5&start=0

then it throws a null pointer exception again. However if I manually enter a ?in front of q=*%3A*...then it works. So I assume that my solr server needs this ?(is this because it's so old?), is there any way to hard code one in?

然后它再次抛出空指针异常。但是,如果我?在前面手动输入 aq=*%3A*...那么它就可以工作。所以我假设我的 solr 服务器需要这个?(这是因为它太旧了吗?),有没有办法硬编码一个?

Thanks

谢谢

采纳答案by Felipe Fonseca

Try use the SolrQueryobject instead of ModifiableSolrParams. Maybe it will help.

尝试使用SolrQuery对象而不是 ModifiableSolrParams。也许它会有所帮助。

If you cant use SolrQuery for any reason, try using statics names, like "CommonParams.Q" instead of hardcoded ones like "?q"

如果您因任何原因无法使用 SolrQuery,请尝试使用静态名称,例如“CommonParams.Q”,而不是像“?q”这样的硬编码名称

EDITED

已编辑

I tested your problem and I think you are lacking configurations at your Application Server.

我测试了您的问题,我认为您的应用程序服务器缺少配置。

Are you using JBoss 7.1? You need to add a line to .standalone.sh or standalone.bat telling where solr is. For example, in Jboss 7.1, in default configurations, you have to add \"-Dsolr.solr.home=$SOLR_HOME/example/solr\" \ to the standalone.sh

您使用的是 JBoss 7.1 吗?您需要在 .standalone.sh 或 standalone.bat 中添加一行,告诉 solr 在哪里。例如,在 Jboss 7.1 中,在默认配置中,您必须将 \"-Dsolr.solr.home=$SOLR_HOME/example/solr\" \ 添加到 standalone.sh

I dont know about others Application Servers, but you can search a little and see how you can do that in another AS.

我不了解其他应用程序服务器,但您可以稍微搜索一下,看看如何在另一个 AS 中执行此操作。

回答by Deepak Shrivastava

Try using SolrQuery, you have option to set everything like query, filter, facet, start, rows etc. Below is a sample code

尝试使用 SolrQuery,您可以选择设置所有内容,例如查询、过滤器、构面、开始、行等。下面是示例代码

on the latest solr 6.2.1 you can create solr client like below:

在最新的 solr 6.2.1 上,您可以创建如下所示的 solr 客户端:

SolrClient solr = new HttpSolrClient.Builder("<url>").build();

SolrQuery query = new SolrQuery();
query.setQuery("collector:" + input);
query.addFilterQuery("(doc_type:" + entity + ")");
query.addSort("lastUpdatedAt", ORDER.desc);
query.setFacet(true);
query.setStart(pagenumber);
query.setRows(pagesize);
QueryResponse response = solr.query(query);

回答by Mohammad Osama

As the previous comments suggests try using SolrQuery. Here is the link that may help solrj example

正如之前的评论建议尝试使用 SolrQuery。这是可能有助于solrj 示例的链接

I think you're missing response.getResults();this will return a SolrDocumentListwhich you can handle using an Iterator and loop through the results like:

我认为您错过了response.getResults();这将返回一个SolrDocumentList您可以使用 Iterator 处理并循环访问结果的方法,例如:

SolrDocumentList docs = response.getResults();

Iterator<SolrDocument> iter = docs.iterator();
while (iter.hasNext())
{
// handle results
}

回答by bmargulies

You might not have the same version of Solr and SolrJ.

您可能没有相同版本的 Solr 和 SolrJ。

You might be making a bad decision by using such an ancient version of Solr at all.

使用如此古老的 Solr 版本可能会做出错误的决定。

You are failing to check the Solr log to find out where the 500 is coming from.

您未能检查 Solr 日志以找出 500 的来源。

You are using ModifiableSolrParams instead SolrQuery.

您使用的是 ModifiableSolrParams 而不是 SolrQuery。