Skip to content

Instantly share code, notes, and snippets.

@ktoso
Created December 24, 2011 21:44
Show Gist options
  • Save ktoso/1518424 to your computer and use it in GitHub Desktop.
Save ktoso/1518424 to your computer and use it in GitHub Desktop.
ScalasticSearch - v0.0.1 (simple Scala DSL for http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html )
val query = ScalasticSearch.boolQuery(
cond (
must(
'nickname -> "Superman", // is equivalent to...
term('nickname -> "Superman"), // this
term("nickname" -> "Superman") // or this
),
should(
'nickname -> "Superman", // is equivalent to...
term('nickname -> "Superman"), // this
term("nickname" -> "Superman") // or this
),
mustNot(
'nickname -> "Superman", // is equivalent to...
term('nickname -> "Superman"), // this
term("nickname" -> "Superman") // or this
)
),
boost = 1
)
query.execute()
package pl.project13
package elastic
import ScalasticSearch._
import ScalasticImplicits._
import java.lang.String
import collection.Seq
object ElasticDemo extends App {
def main(args: Array[String]) {
val query = ScalasticSearch.boolQuery(
cond (
must(
'nickname -> "Superman",
term('nickname -> "Superman"),
term("nickname" -> "Superman")
),
should(
'nickname -> "Superman",
term('nickname -> "Superman"),
term("nickname" -> "Superman")
),
mustNot(
'nickname -> "Superman",
term('nickname -> "Superman"),
term("nickname" -> "Superman")
)
),
boost = 1
)
query.execute()
}
}
class ScalasticSearch(params: ScalasticSearchConds, boost: Int) {
def execute(){
// todo, implement me
}
}
case class ScalasticSearchConds(conditions: Seq[ScalasticQuery])
object ScalasticSearch {
// query entry points
def boolQuery(conf: => ScalasticSearchConds,
boost: Int = 1) = new ScalasticSearch(conf, boost)
// cond
def cond(conditions: Seq[ScalasticQuery]*): ScalasticSearchConds = {
new ScalasticSearchConds(conditions.flatten)
}
// must
def must(confs: ScalasticTerm*): Seq[ScalasticMust] = for (conf <- confs)
yield new ScalasticMust(conf.key, conf.value)
// should
def should(confs: (ScalasticTerm)*): Seq[ScalasticShould] = for (conf <- confs)
yield new ScalasticShould(conf.key, conf.value)
// must
def mustNot(confs: (ScalasticTerm)*): Seq[ScalasticMustNot] = for (conf <- confs)
yield new ScalasticMustNot(conf.key, conf.value)
def mustNot(confs: => (List[(Symbol, String)])) = for (conf <- confs)
yield new ScalasticMustNot(conf._1, conf._2)
// terms etc
def term(conf: (Symbol, String)): ScalasticTerm = term(conf._1.name, conf._2)
def term(conf: (String, String)): ScalasticTerm = term(conf._1, conf._2)
def term(field: String, value: String): ScalasticTerm = ScalasticTerm(conf._1, conf._2)
}
object ScalasticImplicits {
implicit def tuple2must(tuple: (Symbol, String)) = tuple2must(tuple._1.name, tuple._2)
implicit def tuple2must(tuple: (String, String)) = new ScalasticMust(tuple._1, tuple._2)
implicit def tuple2mustNot(tuple: (Symbol, String)) = tuple2mustNot(tuple._1.name, tuple._2)
implicit def tuple2mustNot(tuple: (String, String)) = new ScalasticMustNot(tuple._1, tuple._2)
implicit def tuple2should(tuple: (Symbol, String)) = tuple2should(tuple._1.name, tuple._2)
implicit def tuple2should(tuple: (String, String)) = new ScalasticShould(tuple._1, tuple._2)
implicit def tuple2term(tuple: (Symbol, String)) = term(tuple._1, tuple._2)
implicit def symbol2string(sym: Symbol): String = sym.name
}
// ---------------------------------
class ScalasticMust(field: String, value: String) extends ScalasticQuery {
def term(cond: (String, String)) = new ScalasticMust(cond._1, cond._2)
def ~(conf: => ScalasticMust): List[ScalasticMust] = this :: conf :: Nil
}
object ScalasticMust { }
class ScalasticShould(field: String, value: String) extends ScalasticQuery {
def term(cond: (String, String)) = new ScalasticShould(cond._1, cond._2)
def ~(conf: => ScalasticShould): List[ScalasticShould] = this :: conf :: Nil
}
object ScalasticShould { }
class ScalasticMustNot(field: String, value: String) extends ScalasticQuery {
def term(cond: (String, String)) = new ScalasticMustNot(cond._1, cond._2)
def ~(conf: => ScalasticMustNot): List[ScalasticMustNot] = this :: conf :: Nil
}
object ScalasticMustNot { }
class ScalasticQuery() {
}
object ScalasticQuery
case class ScalasticTerm(key: String, value: String)
case class ScalasticRange(key: String, value: String)
{
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"must_not" : {
"range" : {
"age" : { "from" : 10, "to" : 20 }
}
},
"should" : [
{
"term" : { "tag" : "wow" }
},
{
"term" : { "tag" : "elasticsearch" }
}
],
"minimum_number_should_match" : 1,
"boost" : 1.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment