Search code examples
solrsolrj

Get a response as xlsx using solrj in spring boot


In an spring boot application, I am using solrj to query on solr 7.4. I want the output as xlsx file(not csv).

As mentioned here, I have added the required jars into the solr server classpath. Now when i hit http://localhost:8983/solr/collection_name/select?q=*:*&wt=xlsx in browser, I get a proper response from solr to download the result as a xlsx file.

BUT

How can i achieve this using SOLRJ in a spring boot app?


For other formats, like wt=xml/json/csv, solr response is a string which i can return from controller and set the content info in header to send it as a file. But i didn't get much info on how can i achieve it in case of xlsx file.



Any help is appreciated.


Solution

  • Finally i figured out a way.
    As per the solr documentation, first copy the required jars into solr server classpath, then use the below code to get the response as xlsx file...

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("add solr query");
    solrQuery.addFilterQuery("add filter query");
    solrQuery.setParam("wt", "xlsx");
    
    QueryRequest request = new QueryRequest(solrQuery);
    request.setMethod(METHOD.POST);
    request.setResponseParser(new InputStreamResponseParser("xlsx"));
    
    SolrClient client = // create solrClient
    NamedList<Object> resp = client.request(request, collectionName);
    InputStream xlsxStream = (InputStream) resp.get("stream");
    OutputStream outStream = new FileOutputStream("/path/to/xlsx/file");
    IOUtils.copy(xlsxStream, output);