Solrj客户端和XML响应

时间:2013-04-16 05:34:50

标签: solr solrj solrcloud

我在jetty服务器上使用solr4.0。我想使用solrj查询solr并期望结果以XML格式化。所以我使用了HttpSolrServer(CloudSolrServer和LBHttpSolrServer不支持设置解析器),我将解析器设置为Xmlparser。此外我还设置了SolrQuery param wt = xml.But我无法在XML中获得结果。这是我的测试代码

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

有什么想法在这里出错吗?

1 个答案:

答案 0 :(得分:1)

通过该设置,机器skyfall上的Solr服务器确实以XML格式发送响应,而CommonsHttpSolrServer包装器正确解析XML。但是,这不会改变QueryResponse中的内部表示,它只是Solr类NamedList的一个薄包装。

您可以(错误地)使用XMLResponseWriter来获取完整QueryResponse的XML表示:

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