Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor scoring functions for elastic.

  • Loading branch information...
commit 89160072a50e9edbf4354c32b911eec29515927d 1 parent 1d7cf75
Adam Alix adamalix authored holdenk committed
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 <
350 350 this.copy(boostFields=f(meta)::boostFields)
351 351 }
352 352
353   - def customScore(scriptName: String, params: Map[String, Any]) (implicit ev: ST =:= NoScoreModifiers):
  353 + def customScore(script: String, params: Map[String, Any]) (implicit ev: ST =:= NoScoreModifiers):
354 354 QueryBuilder[M, Ord, Lim, MM, Y, H, Q, MinFacetCount, FacetLimit, NativeScoreScript] = {
355   - this.copy(customScoreScript = Some((scriptName, params)))
  355 + this.copy(customScoreScript = Some((script, params)))
356 356 }
357 357
358 358 //Print out some debugging information.
58 src/main/scala/com/foursquare/slashem/Schema.scala
@@ -624,17 +624,12 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
624 624 case _ => filteredQuery(baseQuery,combineFilters(qb.filters.map(_.elasticFilter(qb.queryFields))))
625 625 }
626 626 //Apply any custom scoring rules (aka emulating Solr's bq/bf)
627   - val scoredQuery = qb.customScoreScript match {
628   - case Some((script, params)) => {
629   - scoreWithScript(fq, script, params)
630   - }
631   - case None => {
632   - val boostedQuery = qb.boostFields match {
633   - case Nil => fq
634   - case _ => boostFields(fq, qb.boostFields)
635   - }
636   - boostedQuery
  627 + val scoredQuery = qb.boostFields match {
  628 + case Nil => qb.customScoreScript match {
  629 + case Some((script, params)) => scoreWithScript(fq, script, params)
  630 + case None => fq
637 631 }
  632 + case _ => scoreFields(fq, qb.boostFields)
638 633 }
639 634 scoredQuery
640 635 }
@@ -649,35 +644,44 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
649 644 }
650 645 case _ => q
651 646 }
652   - }
653   - )
  647 + })
654 648 facetQueries
655 649 }
656   - def boostFields(query: ElasticQueryBuilder, boostFields: List[ScoreBoost]): ElasticQueryBuilder = {
657   - val boostedQuery = new CustomScoreQueryBuilder(query)
658   - val boostedQuerys = boostFields.map(_.elasticBoost)
659   - val params = boostedQuerys.flatMap(_._1)
660   - val scriptSrc = boostedQuerys.map(_._2).mkString(" + ")
  650 +
  651 + /**
  652 + * Custom score the fields which have scoreboosts
  653 + */
  654 + def scoreFields(query: ElasticQueryBuilder, fieldsToScore: List[ScoreBoost]): ElasticQueryBuilder = {
  655 + val scoredFields = fieldsToScore.map(_.elasticBoost)
  656 + val params = scoredFields.flatMap(_._1)
  657 + val scriptSrc = scoredFields.map(_._2).mkString(" + ")
661 658 val paramNames = (1 to params.length).map("p"+_)
662 659 val script = scriptSrc.format(paramNames:_*)
663   - val keyedParams = paramNames zip params
664   - keyedParams.foreach(p => {boostedQuery.param(p._1,p._2)})
  660 + val namesAndParams = paramNames.zip(params).toMap
665 661 val scoreScript = "_score * (1 +"+ script + " )"
666   - boostedQuery.script(scoreScript)
667   - }
668   - def combineFilters(filters: List[ElasticFilterBuilder]): ElasticFilterBuilder = {
669   - new AndFilterBuilder(filters:_*)
  662 + scoreWithScript(query, scoreScript, namesAndParams, false)
670 663 }
671 664
672   - def scoreWithScript(query: ElasticQueryBuilder, scriptName: String,
673   - namesAndParams: Map[String, Any]): ElasticQueryBuilder = {
674   - val customScoreQuery = new CustomScoreQueryBuilder(query)
675   - customScoreQuery.script(scriptName).lang("native")
  665 + /**
  666 + * Add the provided script and its params to the query and build a
  667 + * CustomScoreQuery with it.
  668 + */
  669 + def scoreWithScript(query: ElasticQueryBuilder, script: String,
  670 + namesAndParams: Map[String, Any], native: Boolean = true): ElasticQueryBuilder = {
  671 + val customScoreQuery = new CustomScoreQueryBuilder(query).script(script)
  672 + native match {
  673 + case true => customScoreQuery.lang("native")
  674 + case false => customScoreQuery.lang("mvel")
  675 + }
676 676 for ((name, param) <- namesAndParams) {
677 677 customScoreQuery.param(name, param)
678 678 }
679 679 customScoreQuery
680 680 }
  681 +
  682 + def combineFilters(filters: List[ElasticFilterBuilder]): ElasticFilterBuilder = {
  683 + new AndFilterBuilder(filters:_*)
  684 + }
681 685 }
682 686
683 687 trait SolrSchema[M <: Record[M]] extends SlashemSchema[M] {

0 comments on commit 8916007

Please sign in to comment.
Something went wrong with that request. Please try again.