how to get started with Elastic Search using scala client

14,511

Solution 1

The Elastic4s project contains, near the top of the readme, a simple example on how to use the driver. This example is a complete Scala program that you can execute.

import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._

object Test extends App {

  val client = ElasticClient.local

  // await is a helper method to make this operation sync instead of async
  // You would normally avoid doing this in a real program as it will block
  client.execute { index into "bands/artists" fields "name"->"coldplay" }.await

  val resp = client.execute { search in "bands/artists" query "coldplay" }.await
  println(resp)

}

If this is too complicated, then that is not because the Scala client is too complicated, but because you don't yet understand enough about Elasticsearch or Scala. The Scala client you are looking at is a typical DSL so it uses some Scala tricks that make it nice to use as a client, but not necessarily easy to to understand under the covers.

Here are some good links to understanding Elasticsearch:

Before you use any of the Scala drivers, you should at least understand the basic concepts of an index/type, the query DSL, and what a node is in Elasticsearch. It might also be helpful to look at the JSON that you can send with the HTTP interface as that is a bit easier to see what is going on, because the Elasticsearch docs can be heavy going at first.

Solution 2

Simple elastic search client

  <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.5.0</version>
  </dependency>
   <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.5.0</version>
    </dependency>

Scala code to ES with basic auth:

import org.apache.http.HttpHost
import org.apache.http.auth.{AuthScope, Credentials, UsernamePasswordCredentials}
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions
import org.elasticsearch.client._
import org.apache.http.client.CredentialsProvider
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder

  val  credentials = new UsernamePasswordCredentials("<username>", "<password>");
    val credentialsProvider:CredentialsProvider  = new BasicCredentialsProvider
    credentialsProvider.setCredentials(AuthScope.ANY, credentials)

        val client = RestClient.builder(new HttpHost("<host>", 9200,"https")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
          override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
        }).build

val request = new Request(
      "GET",     
      /_cat/aliases?format=JSON )
    val response = client.performRequest(request);

 println("Response:"+response.getEntity.getContent)

client.close
Share:
14,511
swaheed
Author by

swaheed

Updated on June 16, 2022

Comments