Solr server request using SolrJ

I am trying to query my solr database based on the code provided in this question using SolrJ, but it continues to throw a null pointer exception.

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=5 in my browser, then it will return the correct values, but if I enter : http://localhost:8080/apache-solr-1.4.0/select/q=*:*&facet=true&rows=5 , then I get the same error as when I started in eclipse, error:

 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 will get this error:

 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 on what I did wrong?

Thanks heaps

EDIT

Here is my new solrQuery code based on this :

 @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 does not work :( 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 the code and deduced:

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

If I enter this in my browser, for example

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

then it again throws a null pointer exception. However, if I manually enter ? before q=*%3A*... then it works. So I assume my solr server needs this one ? (is it because it is so old?), is there a way to hardcode it into?

thanks

+4
source share
4 answers

Try using SolrQuery instead of ModifiableSolrParams. Perhaps this will help.

If you can't use SolrQuery for any reason, try using static names like "CommonParams.Q" rather than hardcoded ones like "? Q"

edited

I tested your problem and I think you are missing configurations on your application server.

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

I do not know about other application servers, but you can search a bit and see how you can do this in another AS.

+3
source

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

on the last solr 6.2.1 you can create a solr client, as shown below:

 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); 
+2
source

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

You may have made the wrong decision using such an ancient version of Solr.

You cannot check the Solr log to find out where the 500 comes from.

Instead of SolrQuery you use ModifiableSolrParams.

0
source

As in previous comments, try using SolrQuery. Here is a link that might help solrj example

I think you are missing response.getResults(); this will return a SolrDocumentList , which you can handle using Iterator, and check the results as:

 SolrDocumentList docs = response.getResults(); Iterator<SolrDocument> iter = docs.iterator(); while (iter.hasNext()) { // handle results } 
0
source

All Articles