Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from foursquare/custom-scoring
Custom scoring support
- Loading branch information
Showing
7 changed files
with
239 additions
and
129 deletions.
There are no files selected for viewing
180 changes: 97 additions & 83 deletions
180
src/main/scala/com/foursquare/slashem/QueryBuilder.scala
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
plugin=com.foursquare.elasticsearch.scorer.FourSquareScorePlugin |
17 changes: 17 additions & 0 deletions
17
src/test/scala/com/foursquare/elasticsearch/scorer/FoursquareScorePlugin.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.foursquare.elasticsearch.scorer; | ||
|
||
import org.elasticsearch.plugins.AbstractPlugin; | ||
import org.elasticsearch.script.ScriptModule; | ||
|
||
/** | ||
* Provides a fast* score script for our primary use case | ||
*/ | ||
class FourSquareScorePlugin extends AbstractPlugin { | ||
override def name(): String = "foursquare"; | ||
|
||
override def description(): String = "foursquare plugin"; | ||
|
||
def onModule(module: ScriptModule): Unit = { | ||
module.registerScript("distance_score_magic", classOf[ScoreFactory]); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ala/com/foursquare/elasticsearch/scorer/script/CombinedDistanceDocumentScorerScript.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.foursquare.elasticsearch.scorer; | ||
|
||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.elasticsearch.index.field.data.NumericDocFieldData; | ||
import org.elasticsearch.index.mapper.geo.GeoPointDocFieldData; | ||
import org.elasticsearch.script.AbstractFloatSearchScript; | ||
import org.elasticsearch.script.ExecutableScript; | ||
import org.elasticsearch.script.NativeScriptFactory; | ||
import org.elasticsearch.search.lookup.DocLookup; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Note: assumes that the point field is point | ||
*/ | ||
case class CombinedDistanceDocumentScorerSearchScript(val lat: Double, | ||
val lon: Double, | ||
val weight1: Float, | ||
val weight2: Float) extends AbstractFloatSearchScript { | ||
|
||
override def runAsFloat(): Float = { | ||
val myDoc: DocLookup = doc(); | ||
val point: GeoPointDocFieldData = myDoc.get("point").asInstanceOf[GeoPointDocFieldData]; | ||
val popularity: Double = myDoc.numeric("decayedPopularity1").asInstanceOf[NumericDocFieldData[_]].getDoubleValue() | ||
// up to you to remove score from here or not..., also, possibly, add more weights options | ||
val myScore: Float = (score() * | ||
(1 + weight1 * math.pow(((1.0 * (math.pow(point.distanceInKm(lat, lon), 2.0))) + 1.0), -1.0) | ||
+ popularity * weight2)).toFloat; | ||
myScore | ||
} | ||
} | ||
|
||
class ScoreFactory extends NativeScriptFactory { | ||
def newScript(@Nullable params: Map[String, Object]): ExecutableScript = { | ||
val lat: Double = if (params == null) 1 else XContentMapValues.nodeDoubleValue(params.get("lat"), 0); | ||
val lon: Double = if (params == null) 1 else XContentMapValues.nodeDoubleValue(params.get("lon"), 0); | ||
val weight1: Float = if(params == null) 1 else XContentMapValues.nodeFloatValue(params.get("weight1"), 5000.0f); | ||
val weight2: Float = if(params == null) 1 else XContentMapValues.nodeFloatValue(params.get("weight2"), 0.05f); | ||
return new CombinedDistanceDocumentScorerSearchScript(lat, lon, weight1, weight2); | ||
} | ||
} |
Oops, something went wrong.