Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Holden Karau committed May 15, 2012
1 parent dd88052 commit 6de668e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ libraryDependencies <++= (scalaVersion) { scalaVersion =>
"org.scala-tools.testing" %% "specs" % specsVersion % "test",
"org.elasticsearch" % "elasticsearch" % "0.19.2",
"org.scala-tools.testing" %% "scalacheck" % scalaCheckVersion % "test",
"org.scalaj" %% "scalaj-collection" % "1.2"
"org.scalaj" %% "scalaj-collection" % "1.2",
"org.testng" % "testng" % "6.3.1" % "test",
"org.hamcrest" % "hamcrest-core" % "1.3.RC2" % "test",
"org.hamcrest" % "hamcrest-library" % "1.3.RC2" % "test"
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.foursquare.elasticsearch.scorer;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentFactory;
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 java.util.Arrays;

import org.hamcrest.MatcherAssert.assertThat;
import org.hamcrest.Matchers.closeTo;
import org.hamcrest.Matchers.equalTo;

import scala.math;
/**
*/
class DistanceScoreMagicSearchScriptTests {

@Test
def simpleUrlScoreTest() {
val settings: Settings = ImmutableSettings.settingsBuilder()
.put("gateway.type", "none")
.put("index.number_of_shards", 1)
.build();
val node: Node = NodeBuilder.nodeBuilder().settings(settings).node();
val mapping: String = XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("properties").startObject("point").field("type", "geo_point").endObject().endObject()
.endObject().endObject().string();
node.client().admin().indices().prepareCreate("test").addMapping("type1", mapping).execute().actionGet();

node.client().prepareIndex("test", "type1", "1")
.setSource(XContentFactory.jsonBuilder().startObject()
.startObject("point").field("lat", 40.7143528).field("lon", -74.0059731).endObject()
.field("decayedPopularity1", 3)
.endObject())
.setRefresh(true)
.execute().actionGet();

val searchResponse: SearchResponse = node.client().prepareSearch("test")
.setQuery(QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery())
.script("distance_score_magic").lang("native").param("lat", 40.759011).param("lon", -73.9844722).param("weight1", 2000).param("weight2", 0.03))
.execute().actionGet();

assertThat(Arrays.toString(searchResponse.shardFailures().asInstanceOf[Array[Object]]), searchResponse.failedShards(), equalTo(0));
assertThat(searchResponse.hits().totalHits(), equalTo(1l));

// 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],
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]));

node.close();
}
}

0 comments on commit 6de668e

Please sign in to comment.