Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor scoring functions for elastic.

  • Loading branch information...
commit 89160072a50e9edbf4354c32b911eec29515927d 1 parent 1d7cf75
@adamalix adamalix authored committed
View
4 src/main/scala/com/foursquare/slashem/QueryBuilder.scala
@@ -350,9 +350,9 @@ case class QueryBuilder[M <: Record[M], Ord, Lim, MM <: MinimumMatchType, Y, H <
this.copy(boostFields=f(meta)::boostFields)
}
- def customScore(scriptName: String, params: Map[String, Any]) (implicit ev: ST =:= NoScoreModifiers):
+ def customScore(script: String, params: Map[String, Any]) (implicit ev: ST =:= NoScoreModifiers):
QueryBuilder[M, Ord, Lim, MM, Y, H, Q, MinFacetCount, FacetLimit, NativeScoreScript] = {
- this.copy(customScoreScript = Some((scriptName, params)))
+ this.copy(customScoreScript = Some((script, params)))
}
//Print out some debugging information.
View
58 src/main/scala/com/foursquare/slashem/Schema.scala
@@ -624,17 +624,12 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
case _ => filteredQuery(baseQuery,combineFilters(qb.filters.map(_.elasticFilter(qb.queryFields))))
}
//Apply any custom scoring rules (aka emulating Solr's bq/bf)
- val scoredQuery = qb.customScoreScript match {
- case Some((script, params)) => {
- scoreWithScript(fq, script, params)
- }
- case None => {
- val boostedQuery = qb.boostFields match {
- case Nil => fq
- case _ => boostFields(fq, qb.boostFields)
- }
- boostedQuery
+ val scoredQuery = qb.boostFields match {
+ case Nil => qb.customScoreScript match {
+ case Some((script, params)) => scoreWithScript(fq, script, params)
+ case None => fq
}
+ case _ => scoreFields(fq, qb.boostFields)
}
scoredQuery
}
@@ -649,35 +644,44 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
}
case _ => q
}
- }
- )
+ })
facetQueries
}
- def boostFields(query: ElasticQueryBuilder, boostFields: List[ScoreBoost]): ElasticQueryBuilder = {
- val boostedQuery = new CustomScoreQueryBuilder(query)
- val boostedQuerys = boostFields.map(_.elasticBoost)
- val params = boostedQuerys.flatMap(_._1)
- val scriptSrc = boostedQuerys.map(_._2).mkString(" + ")
+
+ /**
+ * Custom score the fields which have scoreboosts
+ */
+ def scoreFields(query: ElasticQueryBuilder, fieldsToScore: List[ScoreBoost]): ElasticQueryBuilder = {
+ val scoredFields = fieldsToScore.map(_.elasticBoost)
+ val params = scoredFields.flatMap(_._1)
+ val scriptSrc = scoredFields.map(_._2).mkString(" + ")
val paramNames = (1 to params.length).map("p"+_)
val script = scriptSrc.format(paramNames:_*)
- val keyedParams = paramNames zip params
- keyedParams.foreach(p => {boostedQuery.param(p._1,p._2)})
+ val namesAndParams = paramNames.zip(params).toMap
val scoreScript = "_score * (1 +"+ script + " )"
- boostedQuery.script(scoreScript)
- }
- def combineFilters(filters: List[ElasticFilterBuilder]): ElasticFilterBuilder = {
- new AndFilterBuilder(filters:_*)
+ scoreWithScript(query, scoreScript, namesAndParams, false)
}
- def scoreWithScript(query: ElasticQueryBuilder, scriptName: String,
- namesAndParams: Map[String, Any]): ElasticQueryBuilder = {
- val customScoreQuery = new CustomScoreQueryBuilder(query)
- customScoreQuery.script(scriptName).lang("native")
+ /**
+ * Add the provided script and its params to the query and build a
+ * CustomScoreQuery with it.
+ */
+ def scoreWithScript(query: ElasticQueryBuilder, script: String,
+ namesAndParams: Map[String, Any], native: Boolean = true): ElasticQueryBuilder = {
+ val customScoreQuery = new CustomScoreQueryBuilder(query).script(script)
+ native match {
+ case true => customScoreQuery.lang("native")
+ case false => customScoreQuery.lang("mvel")
+ }
for ((name, param) <- namesAndParams) {
customScoreQuery.param(name, param)
}
customScoreQuery
}
+
+ def combineFilters(filters: List[ElasticFilterBuilder]): ElasticFilterBuilder = {
+ new AndFilterBuilder(filters:_*)
+ }
}
trait SolrSchema[M <: Record[M]] extends SlashemSchema[M] {
Please sign in to comment.
Something went wrong with that request. Please try again.