Created
December 16, 2013 22:12
-
-
Save fcroiseaux/7995372 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 elastic | |
import play.api.libs.ws.{Response, SignatureCalculator, WS} | |
import play.api.libs.json._ | |
import play.api.libs.json.Json._ | |
import scala.concurrent._ | |
import ExecutionContext.Implicits.global | |
/** | |
* Created with IntelliJ IDEA. | |
* User: croiseaux | |
* Date: 23/10/12 | |
* Time: 23:13 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
class ElasticClient(val host: String = "localhost", val port: Int = 9200) { | |
val url = "http://" + host + ":" + port.toString | |
def postQuery(domain: String, query: JsValue) = WS.url(url + "/_all" + domain + "/_search/").post(query).map(resp => Json.parse(resp.body)) | |
def runQuery(domain: String, query: JsValue) = { | |
WS.url(url + "/_all" + domain + "/_search/").post(query).map(resp => Json.parse(resp.body)) | |
} | |
} | |
object QueryBuilder { | |
val facet_size = 999 | |
val no_data = 0 | |
def statistical(field: String): JsValue = Json.obj( | |
"statistical" -> Json.obj( | |
"field" -> field, | |
"size" -> facet_size | |
) | |
) | |
def termStat(pair: (String, String)): JsValue = Json.obj( | |
"terms_stats" -> Json.obj( | |
"key_field" -> pair._1, | |
"value_field" -> pair._2, | |
"all_terms" -> true, | |
"size" -> facet_size | |
) | |
) | |
def term(field: String): JsValue = Json.obj( | |
"terms" -> Json.obj( | |
"field" -> field, | |
"all_terms" -> true, | |
"size" -> facet_size | |
) | |
) | |
def dateHistogram(pair: (String, String), interval: String = "month"): JsValue = Json.obj( | |
"date_histogram" -> Json.obj( | |
"key_field" -> pair._1, | |
"value_field" -> pair._2, | |
"interval" -> interval, | |
"size" -> facet_size | |
) | |
) | |
def textQ(clause: Map[String, String]): JsValue = Json.obj( | |
"text" -> toJson(clause) | |
) | |
def dateRange(field: String, debut: String, fin: String): JsValue = Json.obj( | |
"range" -> Json.obj( | |
field -> Json.obj( | |
"from" -> debut, | |
"to" -> fin | |
) | |
) | |
) | |
def must(cond: Seq[JsValue]): JsValue = Json.obj( | |
"must" -> toJson(cond.toArray) | |
) | |
def bool(cond: JsValue): JsValue = Json.obj("bool" -> cond) | |
def matchAll(): JsValue = Json.obj( | |
"match_all" -> Json.obj() | |
) | |
def queryWithFacets(query: JsValue, facets: Map[String, JsValue]): JsValue = Json.obj( | |
"size" -> no_data, | |
"query" -> query, | |
"facets" -> facets | |
) | |
def sortFields(fields: Array[String]): JsValue = Json.toJson(fields) | |
def query(clause: JsValue): JsValue = Json.obj( | |
"size" -> 99999, | |
"query" -> clause | |
) | |
def query(clause: JsValue, sortBy: JsValue): JsValue = Json.obj( | |
"size" -> 99999, | |
"sort" -> sortBy, | |
"query" -> clause | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment