Skip to content

Commit

Permalink
Fix everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Holden Karau committed May 25, 2012
1 parent a7513ed commit 7739460
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/es-plugin.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
plugin=com.foursquare.elasticsearch.scorer
plugin=com.foursquare.elasticsearch.scorer.FourSquareScorePlugin
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class FourSquareScorePlugin extends AbstractPlugin {
override def description(): String = "foursquare plugin";

def onModule(module: ScriptModule): Unit = {
module.registerScript("distance_score_magic", classOf[CombinedDistanceDocumentScorerSearchScript#Factory]);
module.registerScript("distance_score_magic", classOf[ScoreFactory]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ case class CombinedDistanceDocumentScorerSearchScript(val lat: Double,
val weight1: Float,
val weight2: Float) extends AbstractFloatSearchScript {

class Factory 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);
}
}


override def runAsFloat(): Float = {
val myDoc: DocLookup = doc();
val point: GeoPointDocFieldData = myDoc.get("point").asInstanceOf[GeoPointDocFieldData];
Expand All @@ -41,3 +30,13 @@ case class CombinedDistanceDocumentScorerSearchScript(val lat: Double,
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.testng.annotations.Test;

import org.junit.Test
import org.junit._

import java.util.Arrays;

Expand Down Expand Up @@ -52,7 +54,7 @@ class DistanceScoreMagicSearchScriptTests {

// we rely here on score being 1 since we are using match_all
val distance: Double = GeoDistance.PLANE.calculate(40.7143528, -74.0059731, 40.759011, -73.9844722, DistanceUnit.KILOMETERS);
assertThat(searchResponse.hits().getAt(0).score().asInstanceOf[java.lang.Double],
assertThat(searchResponse.hits().getAt(0).score().toDouble.asInstanceOf[java.lang.Double],
closeTo(((1 + 2000 * math.pow(((1.0 * (math.pow(distance, 2.0))) + 1.0), -1.0)
+ 3 * 0.03).asInstanceOf[java.lang.Double]),
0.00001.asInstanceOf[java.lang.Double]));
Expand Down

0 comments on commit 7739460

Please sign in to comment.