Created
June 19, 2016 03:34
-
-
Save jaohaohsuan/9b9047e5e9e3fa3281776a4c365e5140 to your computer and use it in GitHub Desktop.
search directive sample
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.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