Permalink
Browse files

Add support for using params in geo queries with ES.

* lat/long will likely vary for most queries, move it out into
  a seperate param so the compilation of the script can be cached
  by es.
* Bump version to 0.9.10
  • Loading branch information...
1 parent f202139 commit efead0303adb5a055d6d166820a47a07d7e62418 Holden Karau committed Apr 24, 2012
View
@@ -1,6 +1,6 @@
name := "slashem"
-version := "0.9.9b"
+version := "0.9.10"
organization := "com.foursquare"
@@ -332,7 +332,7 @@ object Ast {
}
/** @inheritdoc */
def elasticBoost(): Pair[List[String],String] = {
- val distanceInKm = Pair(List(name,lat,lng).map(_.toString),"doc['%s'].distanceInKm(%s,%s)")
+ val distanceInKm = Pair(List(lat,lng).map(_.toString),"doc['"+name+"'].distanceInKm(%s,%s)")
distType match {
case "square" => Pair(distanceInKm._1,"pow(%s,2.0)".format(distanceInKm._2))
case _ => distanceInKm
@@ -345,8 +345,9 @@ object Ast {
def boost: String = "recip(%s,%d,%d,%d)".format(query.boost, x, y, z)
/** @inheritdoc */
def elasticBoost(): Pair[List[String],String] = {
- Pair(Nil,
- "%d.0*pow(((%d.0*(%s))+%d.0),-1.0)".format(y, x, query.elasticBoost(), z))
+ val subquery= query.elasticBoost()
+ Pair(subquery._1,
+ "%d.0*pow(((%d.0*(%s))+%d.0),-1.0)".format(y, x, subquery._2, z))
}
}
@@ -490,14 +490,14 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
case Some(Pair(sort,dir)) => {
val (params,scriptSrc) = sort.elasticBoost()
val paramNames = (1 to params.length).map("p"+_)
- val script = scriptSrc.format(paramNames)
- val keyedParams = params zip paramNames
+ val script = scriptSrc.format(paramNames:_*)
+ val keyedParams = paramNames zip params
val sortOrder = dir match {
case "asc" => SortOrder.ASC
case "desc" => SortOrder.DESC
}
val sortBuilder = new ScriptSortBuilder(script,"number").order(sortOrder)
- keyedParams.foreach(p => sortBuilder.param(p._1,p._2))
+ keyedParams.foreach(p => {sortBuilder.param(p._1,p._2)})
baseRequest.addSort(sortBuilder)
}
case _ => baseRequest
@@ -617,7 +617,15 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
}
def boostFields(query: ElasticQueryBuilder, boostFields: List[ScoreBoost]): ElasticQueryBuilder = {
val boostedQuery = new CustomScoreQueryBuilder(query)
- val scoreScript = "_score * (1 +"+(boostFields.map(_.elasticBoost).mkString(" + ") + " )")
+ val boostedQuerys = boostFields.map(_.elasticBoost)
+ val params = boostedQuerys.flatMap(_._1)
+ val scriptSrc = boostedQuerys.map(_._2).mkString(" + ")
+ val paramNames = (1 to params.length).map("p"+_)
+ println("using param names"+paramNames+" from params "+params+"on a query string of "+scriptSrc)
+ val script = scriptSrc.format(paramNames:_*)
+ val keyedParams = paramNames zip params
+ keyedParams.foreach(p => {boostedQuery.param(p._1,p._2)})
+ val scoreScript = "_score * (1 +"+ script + " )"
boostedQuery.script(scoreScript)
}
def combineFilters(filters: List[ElasticFilterBuilder]): ElasticFilterBuilder = {

0 comments on commit efead03

Please sign in to comment.