Querying Solr server using SolrJ

20,044

Solution 1

Try use the SolrQuery object instead of ModifiableSolrParams. Maybe it will help.

If you cant use SolrQuery for any reason, try using statics names, like "CommonParams.Q" instead of hardcoded ones like "?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

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

Solution 2

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

on the latest solr 6.2.1 you can create solr client like 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);
Share:
20,044
Pete
Author by

Pete

About me: Not much; a desk, chair and computer.

Updated on July 09, 2022

Comments

  • Pete
    Pete almost 2 years

    I'm trying to query my solr database based off the code provided in this question with SolrJ but it keeps throwing 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 returns the correct values however if I enter: http://localhost:8080/apache-solr-1.4.0/select/q=*:*&facet=true&rows=5 then I get the exact same error as when I run it in eclipse, the error is:

    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:

    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:

    @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:

    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?

    Thanks