From 77f39ebc0f6faead945c9f31f364dc51d5dfd435 Mon Sep 17 00:00:00 2001 From: Grant Ingersoll Date: Wed, 8 Sep 2010 22:29:32 +0000 Subject: [PATCH] SOLR-2114: fix parsing error in hsin function git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@995267 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 ++ .../apache/solr/search/ValueSourceParser.java | 10 ++++++---- .../distance/DistanceFunctionTest.java | 19 +++++++++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 7f68d8aebd5..adffaaf5ed4 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -466,6 +466,8 @@ Bug Fixes prevent an exception in one facet command from affecting another facet 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.java index 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 s1 = new ArrayList(); 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.java index 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 close assertQ(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