Apache Lucene open-source search software
SOLR-2114: fix parsing error in hsin function
Grant Ingersoll77f39ebc0f6f
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 7f68d8aebd5..adffaaf5ed4 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -466,6 +466,8 @@ Bug Fixesprevent an exception in one facet command from affecting anotherfacet command. (yonik)+* SOLR-2114: Fixed parsing error in hsin function. The function signature has changed slightly. (gsingers)+Other Changes----------------------diff --git a/solr/src/java/org/apache/solr/search/ValueSourceParser.java b/solr/src/java/org/apache/solr/search/ValueSourceParser.javaindex 92509f938df..3a5f71ab6b9 100755--- a/solr/src/java/org/apache/solr/search/ValueSourceParser.java+++ b/solr/src/java/org/apache/solr/search/ValueSourceParser.java@@ -241,12 +241,17 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {public ValueSource parse(FunctionQParser fp) throws ParseException {double radius = fp.parseDouble();+ //SOLR-2114, make the convert flag required, since the parser doesn't support much in the way of lookahead or the ability to convert a String into a ValueSource+ boolean convert = Boolean.parseBoolean(fp.parseArg());+MultiValueSource pv1;MultiValueSource pv2;ValueSource one = fp.parseValueSource();ValueSource two = fp.parseValueSource();if (fp.hasMoreArguments()) {++List<ValueSource> s1 = new ArrayList<ValueSource>();s1.add(one);s1.add(two);@@ -267,10 +272,7 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {"Input must either be 2 MultiValueSources, or there must be 4 ValueSources");}}- boolean convert = false;- if (fp.hasMoreArguments()){- convert = Boolean.parseBoolean(fp.parseArg());- }+return new HaversineFunction(pv1, pv2, radius, convert);}});diff --git a/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java b/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.javaindex 74364467cb2..c74eb33f557 100644--- a/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java+++ b/solr/src/test/org/apache/solr/search/function/distance/DistanceFunctionTest.java@@ -44,16 +44,18 @@ public class DistanceFunctionTest extends SolrTestCaseJ4 {assertU(adoc("id", "4", "x_td", String.valueOf(Math.PI / 4), "y_td", String.valueOf(Math.PI / 4), "gh_s", GeoHashUtils.encode(32.7693246, -81.9289094)));assertU(adoc("id", "5", "x_td", "45.0", "y_td", "45.0","gh_s", GeoHashUtils.encode(32.7693246, -81.9289094)));- assertU(adoc("id", "6", "point_hash", "32.5, -79.0"));- assertU(adoc("id", "7", "point_hash", "32.6, -78.0"));+ assertU(adoc("id", "6", "point_hash", "32.5, -79.0", "point", "32.5, -79.0"));+ assertU(adoc("id", "7", "point_hash", "32.6, -78.0", "point", "32.6, -78.0"));assertU(commit());//Get the haversine distance between the point 0,0 and the docs above assuming a radius of 1- assertQ(req("fl", "*,score", "q", "{!func}hsin(1, x_td, y_td, 0, 0)", "fq", "id:1"), "//float[@name='score']='0.0'");- assertQ(req("fl", "*,score", "q", "{!func}hsin(1, x_td, y_td, 0, 0)", "fq", "id:2"), "//float[@name='score']='" + (float) (Math.PI / 2) + "'");- assertQ(req("fl", "*,score", "q", "{!func}hsin(1, x_td, y_td, 0, 0)", "fq", "id:3"), "//float[@name='score']='" + (float) (Math.PI / 2) + "'");- assertQ(req("fl", "*,score", "q", "{!func}hsin(1, x_td, y_td, 0, 0)", "fq", "id:4"), "//float[@name='score']='1.0471976'");- assertQ(req("fl", "*,score", "q", "{!func}hsin(1, x_td, y_td, 0, 0, true)", "fq", "id:5"), "//float[@name='score']='1.0471976'");-+ assertQ(req("fl", "*,score", "q", "{!func}hsin(1, false, x_td, y_td, 0, 0)", "fq", "id:1"), "//float[@name='score']='0.0'");+ assertQ(req("fl", "*,score", "q", "{!func}hsin(1, false, x_td, y_td, 0, 0)", "fq", "id:2"), "//float[@name='score']='" + (float) (Math.PI / 2) + "'");+ assertQ(req("fl", "*,score", "q", "{!func}hsin(1, false, x_td, y_td, 0, 0)", "fq", "id:3"), "//float[@name='score']='" + (float) (Math.PI / 2) + "'");+ assertQ(req("fl", "*,score", "q", "{!func}hsin(1, false, x_td, y_td, 0, 0)", "fq", "id:4"), "//float[@name='score']='1.0471976'");+ assertQ(req("fl", "*,score", "q", "{!func}hsin(1, true, x_td, y_td, 0, 0)", "fq", "id:5"), "//float[@name='score']='1.0471976'");+ //SOLR-2114+ assertQ(req("fl", "*,score", "q", "{!func}hsin(6371.009, true, point, vector(0, 0))", "fq", "id:6"), "//float[@name='score']='8977.814'");+//Geo Hash Haversine//Can verify here: http://www.movable-type.co.uk/scripts/latlong.html, but they use a slightly different radius for the earth, so just be closeassertQ(req("fl", "*,score", "q", "{!func}ghhsin(" + DistanceUtils.EARTH_MEAN_RADIUS_KM + ", gh_s, \"" + GeoHashUtils.encode(32, -79) +@@ -67,6 +69,7 @@ public class DistanceFunctionTest extends SolrTestCaseJ4 {assertQ(req("fl", "*,score", "q", "{!func}ghhsin(" + DistanceUtils.EARTH_MEAN_RADIUS_KM + ", gh_s, geohash(32, -79))", "fq", "id:1"), "//float[@name='score']='122.171875'");+}@Test