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
-
source
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(); }
- Scala
-
source
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[Future] = 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() }
-
source
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[Future] = 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() }
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
-
source
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(); // ... }
- Scala
-
source
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 // ... }