8

While using SOLRJ I would like to know how can I convert SolrQuery object to its URL representation with SOLR query syntax. I tried to use .toString() method but it doesnt return proper query representation. Is there some other way how to do it?

cheffe
  • 9,345
  • 2
  • 46
  • 57
David Marko
  • 2,477
  • 3
  • 27
  • 58

2 Answers2

8

I recommend ClientUtils.toQueryString for this matter.

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

Within the source code of HttpSolrServer you can see that this is used by the Solrj code itself for this reason.

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

  }
cheffe
  • 9,345
  • 2
  • 46
  • 57
1

SolrJ (tested version 6.6.0 ) it is:

@Test
public void solrQueryToURL() {
  SolrQuery query = new SolrQuery("query");
  Assert.assertEquals("?q=query", query.toQueryString());
}
Babu
  • 4,324
  • 6
  • 41
  • 60