Bogdan Senyshyn
Bogdan Senyshyn

Reputation: 33

How to convert GetResponse from elastic4s to specific case class?

I have set up client for elasticsearch using elastic4s which stores Person case class:

case class Person(id: String, name: String)

How can I get specific person by id and convert GetResponse to this case class? What I'm trying to do is:

client.execute{
   get(id).from(index, `type`).
}

which returns Future[Response[GetResponse]]

Upvotes: 0

Views: 298

Answers (1)

mavarazy
mavarazy

Reputation: 7735

I have not touched elastic4s in a while, but as far as I remember you need to provide a specific HitReader for response

implicit object PersonHitReader extends PersonReader[Character] {
  override def read(hit: Hit): Either[Throwable, Person] = {
    val source = hit.sourceAsMap
    Right(Person(source("id").toString, source("name").toString))
  }
}

https://github.com/sksamuel/elastic4s#hitreader-typeclass

Upvotes: 0

Related Questions