Skip to content

Instantly share code, notes, and snippets.

@jaohaohsuan
Created June 19, 2016 03:34
Show Gist options
  • Save jaohaohsuan/9b9047e5e9e3fa3281776a4c365e5140 to your computer and use it in GitHub Desktop.
Save jaohaohsuan/9b9047e5e9e3fa3281776a4c365e5140 to your computer and use it in GitHub Desktop.
search directive sample
package com.inu.frontend.storedquery
import org.elasticsearch.action.search.{SearchRequestBuilder, SearchResponse}
import spray.routing._
import scala.concurrent.{ExecutionContext, Future}
import org.json4s._
import org.json4s.native.JsonMethods._
import shapeless.{::, HNil}
trait SearchLogsDirectives extends Directives {
implicit def client: org.elasticsearch.client.Client
implicit def executionContext: ExecutionContext
def prepareSearch(query: JValue): Directive1[SearchRequestBuilder] = {
import shapeless._
val queryString = compact(render(query))
parameter('size.as[Int] ? 10, 'from.as[Int] ? 0 ).hflatMap {
case size :: from :: HNil => {
provide(
client.prepareSearch("logs-*")
.setQuery(queryString)
.setSize(size).setFrom(from)
)
}
}
}
def search(query: JValue): Directive1[(String, SearchResponse)] = {
import com.inu.frontend.elasticsearch.ImplicitConversions._
import shapeless._
val queryString = compact(render(query))
parameter('size.as[Int] ? 10, 'from.as[Int] ? 0 ).hflatMap {
case size :: from :: HNil => {
val `logs-*/_search/`: Future[SearchResponse] = client.prepareSearch("logs-*")
.setQuery(queryString)
.setSize(size).setFrom(from)
.execute().future
onComplete(`logs-*/_search/`).flatMap{
case scala.util.Success(res) => provide((queryString,res))
case _ => reject
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment