Search code examples
scalaelastic4s

Elastic4s client bulk operation error


I am trying to use scala elstic4s client to index new documents into my elasticsearch cluster but I am having a compilation problem with the types. Following the documentation and the examples found in the web, the syntax looks like:

Client instantiation:

val settings = ImmutableSettings.settingsBuilder().put("cluster.name", Configuration.elasticsearchClusterName).build()    
val uri = ElasticsearchClientUri("elasticsearch://" + Configuration.elasticsearchUri)
val client = ElasticClient.remote(settings, uri)

I am trying to write it like:

def writeToElasticsearch(bulkList: List[EventMessage]) {
   val ops = for (message <- bulkList) yield index into indexDcp ttl 7.days.toMillis doc StringDocumentSource(message.toJSon()) 
   client.execute(bulk(ops: _*)).await
 }    

I am getting a compilation error in the bulk operation saying:

Multiple markers at this line
- type mismatch; found : List[com.sksamuel.elastic4s.IndexDefinition] required: 
 Seq[Int]

Can anyone tell me how I can convert the types to make this work? Thank you!


Solution

  • I'm not sure why you are getting errors, possibly something in the bits you've missed out of your code samples, but this version of your code compiles fine for me.

    object Test extends App {
    
      val client = ElasticClient.local
      val indexDcp = "myindex/mytype"
    
      import scala.concurrent.duration._
      import ElasticDsl._
    
      def writeToElasticsearch(bulkList: List[EventMessage]) {
        val ops = for (message <- bulkList) yield { index into indexDcp ttl 7.days.toMillis doc StringDocumentSource(message.toJSon()) }
        client.execute(bulk(ops: _*)).await
      }
    
      trait EventMessage {
        def toJSon(): String
      }
    }