Skip to content

Instantly share code, notes, and snippets.

@fancellu
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancellu/278413d554c37ce7c19c to your computer and use it in GitHub Desktop.
Save fancellu/278413d554c37ce7c19c to your computer and use it in GitHub Desktop.
Example of using ES elastic4s with CaseMapper macros
package es
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.concurrent.duration._
object e4s1 extends App {
val client = ElasticClient.remote("localhost",9300)
val outF=client execute {
search in "automobiles"->"transactions" query { term("price"->80000) } sourceInclude("color", "make","sold")
}
val out = Await.result(outF, 10 seconds)
import com.felstar.caseMapper.CaseMapper._
case class Transaction(sold:String, color:String, make:String)
val hits=(out.getHits.hits)
import scala.collection.JavaConversions._
val sources=hits.map(_.getSource)
val scalaMaps=sources.map(mapAsScalaMap)
scalaMaps.foreach(println)
scalaMaps.map(fromMap[Transaction](_)).foreach(println)
}
Map(sold -> 2014-01-01, color -> red, make -> bmw)
Transaction(2014-01-01,red,bmw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment