Question

I am using solr4.0 in jetty server. I want to query solr using solrj and expecting results to be formatted in XML. So i used HttpSolrServer (CloudSolrServer and LBHttpSolrServer does not provide support for setting parser) and i set parser to Xmlparser. Moreover i am also setting SolrQuery param wt=xml.But i am not able to get results in XML.Here is my test code

package solrjtest;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
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.SolrDocumentList;


class SolrjTest
{

    public static void main(String[] args) throws IOException, SolrServerException
    {
        SolrjTest solrj = new SolrjTest();
        solrj.query("hello");
    }

    public void query(String q) throws IOException, SolrServerException
    {
        CommonsHttpSolrServer server = null;
        String uuid = null;
        boolean flag = true;
        while (flag == true)
        {
            uuid = UUID.randomUUID().toString();
            File f = new File("D:/SearchResult/" + uuid + ".txt");
            if (!f.exists())
            {
               flag=false;
               f.createNewFile();
            }
        }
        try
        {
            server = new CommonsHttpSolrServer("http://skyfall:8983/solr/documents");
            server.setParser(new XMLResponseParser());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        SolrQuery query = new SolrQuery();
        query.setQuery(q);
        query.setParam("wt", "xml");
        FileWriter fw = new FileWriter("D:/SearchResult/" + uuid + ".txt");
        try
        {
            QueryResponse qr = server.query(query);
            SolrDocumentList sdl = qr.getResults();
            XMLResponseParser r = new XMLResponseParser();

            Object[] o = new Object[sdl.size()];
            o = sdl.toArray();
            for (int i = 0; i < o.length; i++)
            {
                System.out.println(o[i].toString());
                fw.write(o[i].toString() + "\n");
            }
            fw.flush();
            fw.close();
            System.out.println("finished");
        }
        catch (SolrServerException e)
        {
            e.printStackTrace();
        }
    }
}

Any idea whats going wrong here ?

Was it helpful?

Solution

With that setup, the Solr server at the machine skyfall does send the response in XML and the CommonsHttpSolrServer wrapper does correctly parse the XML. However, that does not change the internal representation in the QueryResponse, which is just a thin wrapper around the Solr class NamedList.

You can (mis)use the XMLResponseWriter to get an XML representation of the full QueryResponse:

private String toXML(SolrParams request, QueryResponse response) {
    XMLResponseWriter xmlWriter = new XMLResponseWriter();
    Writer w = new StringWriter();
    SolrQueryResponse sResponse = new SolrQueryResponse();
    sResponse.setAllValues(response.getResponse());
    try {
        xmlWriter.write(w, new LocalSolrQueryRequest(null, request), sResponse);
    } catch (IOException e) {
        throw new RuntimeException("Unable to convert Solr response into XML", e);
    }
    return w.toString();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top