Is there any good example for ElasticSearch 5.1.1 Scala API using sksamuel/elastic4s or anything else?

922 views Asked by At

I am not able to find a good example for ElasticSearch 5.1.1 using Scala in sksamuel/elastic4s. Documentation is not very helpful and none of the site have any good example for it. Even a simple example to create Index, Put data and search will be helpful.

1

There are 1 answers

0
sksamuel On BEST ANSWER

The elastic4s readme has all the examples you will need to get started. Admittedly, it is light on advanced use cases, but for simple examples there are plenty.

For example, read the quick start guide.

import com.sksamuel.elastic4s.TcpClient
import com.sksamuel.elastic4s.ElasticDsl._

object Test extends App {

  // Here we create an instance of the TCP client
  val client = TcpClient.transport(ElasticsearchClientUri(host, port))

  // await is a helper method to make this operation synchronous instead of async
  // You would normally avoid doing this in a real program as it will block your thread
  client.execute { 
    indexInto("bands" / "artists") fields ("name" -> "coldplay") refresh(RefreshPolicy.IMMEDIATE)
  }.await

  // now we can search for the document we just indexed
  val resp = client.execute { 
    search("bands" / "artists") query "coldplay" 
  }.await

  println(resp)
}