How to convert SolrQuery (SOLRJ) to URL?

When using SOLRJ, I would like to know how to convert a SolrQuery object to its URL representation with SOLR query syntax. I tried using the .toString () method, but it does not return the correct representation of the request. Is there any other way to do this?

+6
source share
2 answers

I recommend ClientUtils.toQueryString on this issue.

@Test public void solrQueryToURL() { SolrQuery tmpQuery = new SolrQuery("some query"); Assert.assertEquals("?q=some+query", ClientUtils.toQueryString(tmpQuery, false)); } 

In the HttpSolrServer source code , you can see that for this reason it is used by the Solrj code.

 public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { // ... other code left out if( SolrRequest.METHOD.GET == request.getMethod() ) { if( streams != null ) { throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!" ); } method = new HttpGet( baseUrl + path + ClientUtils.toQueryString( params, false ) ); // ... other code left out } 
+7
source

SolrJ (verified version 6.6.0):

 @Test public void solrQueryToURL() { SolrQuery query = new SolrQuery("query"); Assert.assertEquals("?q=query", query.toQueryString()); } 
+1
source

All Articles