Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transform SolrQuery(SOLRJ) to URL?

Tags:

java

solr

solrj

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?

like image 402
David Marko Avatar asked Mar 11 '13 14:03

David Marko


People also ask

What is SolrJ in Solr?

SolrJ is an API that makes it easy for applications written in Java (or any language based on the JVM) to talk to Solr. SolrJ hides a lot of the details of connecting to Solr and allows your application to interact with Solr with simple high-level methods. SolrJ supports most Solr APIs, and is highly configurable.

What is SolrClient?

SolrClient's are the main workhorses at the core of SolrJ. They handle the work of connecting to and communicating with Solr, and are where most of the user configuration happens.

How can I recover data from Solr?

You can index this data under the core named sample_Solr using the post command. Following is the Java program to add documents to Apache Solr index. Save this code in a file with named RetrievingData. java.


1 Answers

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

  }
like image 197
cheffe Avatar answered Sep 28 '22 13:09

cheffe