I am a newbie to Solr. I want to use Java to connect to my solr core and get the results back in XML format. By referring the official document, I am able to get the results in binary form. Below is my code:
public static void main(String[] args) throws SolrServerException, IOException {
String urlString = "http://localhost:8983/solr/index1/";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
QueryResponse response = solr.query(query);
System.out.println(response.toString());
}
I also tried to research on how to get response. I found this link which says "If you want to get the raw xml response, just pick up any java HTTP Client, build the request and send it to Solr. You'll get a nice XML String.." solr response in xml format I coded the below code, but it is not giving me response
public static void main(String[] args) throws ClientProtocolException, IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8983/solr/index1/select?q=*:*&wt=xml");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
System.out.println(response1);
}
}
Output:
HttpResponseProxy{HTTP/1.1 200 OK [Content-Type: application/xml; charset=UTF-8, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: application/xml; charset=UTF-8,Chunked: true]}}
On the official site https://lucene.apache.org/solr/guide/6_6/using-solrj.html , it is mentioned to use
solr.setParser(new XMLResponseParser());
to get XML response, but I am not sure how to use it since any example is not given. Any help is appreciated.
Edit: As mentioned in John's comment, I have modified my code as:
System.out.println(EntityUtils.toString(response1.getEntity()));
But in the output, I can see some javascript which is followed by the XML output:
Not tried but it should work,
You need to initialize org.apache.solr.client.solrj.SolrClient
which represents the Solr instance you want to use as follows.
import org.apache.solr.client.solrj.impl.XMLResponseParser;
String serverURL = "http://localhost:8983/solr/<core_name>";
SolrClient solr = new HttpSolrClient.Builder(serverURL).build();
solr.setParser(new XMLResponseParser());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.