Basic Usage

Solrs supports different Future implementations, which affects the result type of AsyncSolrClient methods. For scala there’s support for the standard scala.concurrent.Future and for twitters com.twitter.util.Future. Which one is chosen is defined by the io.ino.solrs.future.FutureFactory that’s in scope when building the AsyncSolrClient (as shown below in the code samples).

For java there’s support for CompletableFuture/CompletionStage. Because java does not support higher kinded types there’s a separate class JavaAsyncSolrClient that allows to create new instances and to perform a request.

In the following it’s shown how to use JavaAsyncSolrClient/AsyncSolrClient:

Java
import io.ino.solrs.JavaAsyncSolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import java.util.concurrent.CompletionStage;

class HelloJava {

  JavaAsyncSolrClient solr = JavaAsyncSolrClient.create("http://localhost:8983/solr/collection1");
  CompletionStage<QueryResponse> response = solr.query(new SolrQuery("java"));
  response.thenAccept(r -> System.out.println("found " + r.getResults().getNumFound() + " docs"));

  // At EOL...
  solr.shutdown();

}
Full source at GitHub
Scala
import io.ino.solrs.AsyncSolrClient
import io.ino.solrs.future.ScalaFutureFactory.Implicit
import org.apache.solr.client.solrj.SolrQuery
import org.apache.solr.client.solrj.response.QueryResponse
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class HelloScala extends App {

  val solr = AsyncSolrClient("http://localhost:8983/solr/collection1")
  val response: Future[QueryResponse] = solr.query(new SolrQuery("scala"))
  response.foreach {
    qr => println(s"found ${qr.getResults.getNumFound} docs")
  }

  // Don't forget...
  solr.shutdown()

}
Full source at GitHub
Twitter
import io.ino.solrs.AsyncSolrClient
import io.ino.solrs.future.TwitterFutureFactory.Implicit
import org.apache.solr.client.solrj.SolrQuery
import org.apache.solr.client.solrj.response.QueryResponse
import com.twitter.util.Future

class HelloTwitter extends App {

  val solr = AsyncSolrClient("http://localhost:8983/solr")
  val response: Future[QueryResponse] = solr.query(new SolrQuery("scala"))
  response.onSuccess {
    qr => println(s"found ${qr.getResults.getNumFound} docs")
  }

  // Finally...
  solr.shutdown()

}
Full source at GitHub

The AsyncSolrClient can further be configured with an AsyncHttpClient instance and the response parser via the AsyncSolrClient.Builder (other configuration properties are described in greater detail in the following sections):

Java
import io.ino.solrs.JavaAsyncSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.asynchttpclient.DefaultAsyncHttpClient;

class HelloJava {

  JavaAsyncSolrClient solr = JavaAsyncSolrClient.builder("http://localhost:8983/solr/collection1")
    .withHttpClient(new DefaultAsyncHttpClient())
    .withResponseParser(new XMLResponseParser())
    .build();

  // ...

}
Full source at GitHub
Scala
import io.ino.solrs.AsyncSolrClient
import io.ino.solrs.future.ScalaFutureFactory.Implicit
import org.apache.solr.client.solrj.impl.XMLResponseParser
import org.asynchttpclient.DefaultAsyncHttpClient

class HelloScala extends App {

  val solr = AsyncSolrClient.Builder("http://localhost:8983/solr/collection1")
    .withHttpClient(new DefaultAsyncHttpClient())
    .withResponseParser(new XMLResponseParser())
    .build

  // ...

}
Full source at GitHub
The source code for this page can be found here.