Skip to content

Instantly share code, notes, and snippets.

@huntc
Created March 10, 2016 22:42
Show Gist options
  • Save huntc/ff3d37ddc024cf22b856 to your computer and use it in GitHub Desktop.
Save huntc/ff3d37ddc024cf22b856 to your computer and use it in GitHub Desktop.
Sample of using Scala with the Java DSL of Lagom
package com.lightbend.conductr.eslite
import javax.inject.Named
import akka.NotUsed
import akka.actor.ActorRef
import akka.pattern.ask
import com.fasterxml.jackson.databind.node.ObjectNode
import com.google.inject.Inject
import com.lightbend.conductr.eslite.dto._
import com.lightbend.lagom.javadsl.api.ServiceCall
import scala.collection.JavaConverters._
import scala.compat.java8.FutureConverters._
import scala.compat.java8.OptionConverters._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import com.lightbend.conductr.eslite.QueryParser._
import akka.util.Timeout
import scala.concurrent.duration._
class EsliteServiceImpl @Inject() (@Named("BundleIdSourceCache") bundleIdSourceCache: ActorRef) extends EsliteService {
implicit val timeout = akka.util.Timeout(2.seconds)
override def bulkIndex(): ServiceCall[NotUsed, BulkRequest, BulkReply] = { (id, request) =>
bundleIdSourceCache ! BundleIdSourceCache.Append(request.getBulkLineItems.asScala.map(bli => {
val item = BulkLineItem.builder().source(bli).build()
item.getBundleId -> item.getSource
}).toSeq)
Future.successful(BulkReply.builder().build()).toJava
}
override def search(): ServiceCall[SearchQuery, NotUsed, SearchReply] = { (id, request) =>
val parsed = ParsedSearchQuery(id)
QueryProcessor.process(parsed, bundleIdSourceCache).map { results =>
val hits = results.map { o =>
Hit.builder().source(o).build()
}
SearchReply.builder().hits(Hits.builder().addHits(hits.toArray: _*).build()).build
}.toJava
}
}
case class ParsedSearchQuery(expr: Expr, size: Int, sort: String)
object ParsedSearchQuery {
def apply(query: SearchQuery): ParsedSearchQuery = {
val expr = QueryParser.parse(query.getQ)
ParsedSearchQuery(expr, query.getSize, query.getSort)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment