Created
March 10, 2016 22:42
-
-
Save huntc/ff3d37ddc024cf22b856 to your computer and use it in GitHub Desktop.
Sample of using Scala with the Java DSL of Lagom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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