aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-08-17 13:36:00 +0000
committerArne Juul <arnej@verizonmedia.com>2020-08-17 13:36:00 +0000
commit15de69eaf9c67b76f2a7e31fe32563ee2a1602b5 (patch)
tree756619d55e95138fd8991078300e03c5049ef529
parent529694a88d48270298171fdcb87d1439f183202b (diff)
Reapply "Arnej/add json geo format"
This reverts commit 6a3b6c0b2caef69146f0a796cef36574c3055417.
-rw-r--r--searchlib/src/tests/common/location/geo_location_test.cpp278
-rw-r--r--searchlib/src/vespa/searchlib/common/geo_location_parser.cpp86
-rw-r--r--searchlib/src/vespa/searchlib/common/geo_location_parser.h4
-rw-r--r--searchlib/src/vespa/searchlib/query/tree/location.cpp36
-rw-r--r--searchlib/src/vespa/searchlib/query/tree/location.h1
5 files changed, 333 insertions, 72 deletions
diff --git a/searchlib/src/tests/common/location/geo_location_test.cpp b/searchlib/src/tests/common/location/geo_location_test.cpp
index f564740fc7e..eec2411f2af 100644
--- a/searchlib/src/tests/common/location/geo_location_test.cpp
+++ b/searchlib/src/tests/common/location/geo_location_test.cpp
@@ -4,6 +4,7 @@
#include <vespa/searchlib/common/geo_location.h>
#include <vespa/searchlib/common/geo_location_spec.h>
#include <vespa/searchlib/common/geo_location_parser.h>
+#include <vespa/searchlib/query/tree/location.h>
#include <vespa/vespalib/gtest/gtest.h>
using search::common::GeoLocation;
@@ -22,12 +23,30 @@ bool is_parseable(const char *str) {
return parser.parseOldFormat(str);
}
+bool is_parseable_new(const char *str, bool with_field = false) {
+ GeoLocationParser parser;
+ if (with_field) {
+ return parser.parseWithField(str);
+ }
+ return parser.parseNoField(str);
+}
+
GeoLocation parse(const char *str) {
GeoLocationParser parser;
EXPECT_TRUE(parser.parseOldFormat(str));
return parser.getGeoLocation();
}
+GeoLocation parse_new(const std::string &str, bool with_field = false) {
+ GeoLocationParser parser;
+ if (with_field) {
+ EXPECT_TRUE(parser.parseWithField(str));
+ } else {
+ EXPECT_TRUE(parser.parseNoField(str));
+ }
+ return parser.getGeoLocation();
+}
+
TEST(GeoLocationParserTest, malformed_bounding_boxes_are_not_parseable) {
EXPECT_TRUE(is_parseable("[2,10,20,30,40]"));
EXPECT_FALSE(is_parseable("[2,10,20,30,40][2,10,20,30,40]"));
@@ -39,6 +58,15 @@ TEST(GeoLocationParserTest, malformed_bounding_boxes_are_not_parseable) {
EXPECT_FALSE(is_parseable("[10,20,30,40]"));
}
+TEST(GeoLocationParserTest, new_bounding_box_formats) {
+ EXPECT_TRUE(is_parseable_new("{b:{x:[10,30],y:[20,40]}}"));
+ EXPECT_TRUE(is_parseable_new("{b:{}}"));
+ EXPECT_TRUE(is_parseable_new("{b:[]}"));
+ EXPECT_TRUE(is_parseable_new("{b:10,b:20}"));
+ EXPECT_TRUE(is_parseable_new("{b:[10, 20, 30, 40]}"));
+ EXPECT_FALSE(is_parseable_new("{b:{x:[10,30],y:[20,40]}"));
+}
+
TEST(GeoLocationParserTest, malformed_circles_are_not_parseable) {
EXPECT_TRUE(is_parseable("(2,10,20,5,0,0,0)"));
EXPECT_FALSE(is_parseable("(2,10,20,5,0,0,0)(2,10,20,5,0,0,0)"));
@@ -51,94 +79,147 @@ TEST(GeoLocationParserTest, malformed_circles_are_not_parseable) {
EXPECT_FALSE(is_parseable("(10,20,5)"));
}
+TEST(GeoLocationParserTest, new_circle_formats) {
+ EXPECT_TRUE(is_parseable_new("{p:{x:10,y:20}}"));
+ EXPECT_TRUE(is_parseable_new("{p:{x:10,y:20},r:5}"));
+ EXPECT_TRUE(is_parseable_new("{p:{x:10, y:10}, r:5}"));
+ EXPECT_TRUE(is_parseable_new("{'p':{y:20,x:10},'r':5}"));
+ EXPECT_TRUE(is_parseable_new("{\n \"p\": { \"x\": 10, \"y\": 20},\n \"r\": 5\n}"));
+ // json demands colon:
+ EXPECT_FALSE(is_parseable_new("{p:{x:10,y:10},r=5}"));
+ // missing y -> 0 default:
+ EXPECT_TRUE(is_parseable_new("{p:{x:10},r:5}"));
+ // unused extra fields are ignored:
+ EXPECT_TRUE(is_parseable_new("{p:{x:10,y:10,z:10},r:5,c:1,d:17}"));
+}
+
TEST(GeoLocationParserTest, bounding_boxes_can_be_parsed) {
- auto loc = parse("[2,10,20,30,40]");
- EXPECT_EQ(false, loc.has_point);
- EXPECT_EQ(true, loc.bounding_box.active());
- EXPECT_EQ(0u, loc.x_aspect.multiplier);
- EXPECT_EQ(0, loc.point.x);
- EXPECT_EQ(0, loc.point.y);
- EXPECT_EQ(std::numeric_limits<uint32_t>::max(), loc.radius);
- EXPECT_EQ(10, loc.bounding_box.x.low);
- EXPECT_EQ(20, loc.bounding_box.y.low);
- EXPECT_EQ(30, loc.bounding_box.x.high);
- EXPECT_EQ(40, loc.bounding_box.y.high);
+ for (const auto & loc : {
+ parse("[2,10,20,30,40]"),
+ parse_new("{b:{x:[10,30],y:[20,40]}}")
+ }) {
+ EXPECT_EQ(false, loc.has_point);
+ EXPECT_EQ(true, loc.bounding_box.active());
+ EXPECT_EQ(0u, loc.x_aspect.multiplier);
+ EXPECT_EQ(0, loc.point.x);
+ EXPECT_EQ(0, loc.point.y);
+ EXPECT_EQ(std::numeric_limits<uint32_t>::max(), loc.radius);
+ EXPECT_EQ(10, loc.bounding_box.x.low);
+ EXPECT_EQ(20, loc.bounding_box.y.low);
+ EXPECT_EQ(30, loc.bounding_box.x.high);
+ EXPECT_EQ(40, loc.bounding_box.y.high);
+ }
}
TEST(GeoLocationParserTest, circles_can_be_parsed) {
- auto loc = parse("(2,10,20,5,0,0,0)");
- EXPECT_EQ(true, loc.has_point);
- EXPECT_EQ(true, loc.bounding_box.active());
- EXPECT_EQ(0u, loc.x_aspect.multiplier);
- EXPECT_EQ(10, loc.point.x);
- EXPECT_EQ(20, loc.point.y);
- EXPECT_EQ(5u, loc.radius);
- EXPECT_EQ(5, loc.bounding_box.x.low);
- EXPECT_EQ(15, loc.bounding_box.y.low);
- EXPECT_EQ(15, loc.bounding_box.x.high);
- EXPECT_EQ(25, loc.bounding_box.y.high);
+ for (const auto & loc : {
+ parse("(2,10,20,5,0,0,0)"),
+ parse_new("{p:{x:10,y:20},r:5}")
+ }) {
+ EXPECT_EQ(true, loc.has_point);
+ EXPECT_EQ(true, loc.bounding_box.active());
+ EXPECT_EQ(0u, loc.x_aspect.multiplier);
+ EXPECT_EQ(10, loc.point.x);
+ EXPECT_EQ(20, loc.point.y);
+ EXPECT_EQ(5u, loc.radius);
+ EXPECT_EQ(5, loc.bounding_box.x.low);
+ EXPECT_EQ(15, loc.bounding_box.y.low);
+ EXPECT_EQ(15, loc.bounding_box.x.high);
+ EXPECT_EQ(25, loc.bounding_box.y.high);
+ }
}
TEST(GeoLocationParserTest, circles_can_have_aspect_ratio) {
- auto loc = parse("(2,10,20,5,0,0,0,2147483648)");
- EXPECT_EQ(true, loc.has_point);
- EXPECT_EQ(true, loc.bounding_box.active());
- EXPECT_EQ(2147483648u, loc.x_aspect.multiplier);
- EXPECT_EQ(10, loc.point.x);
- EXPECT_EQ(20, loc.point.y);
- EXPECT_EQ(5u, loc.radius);
- EXPECT_EQ(-1, loc.bounding_box.x.low);
- EXPECT_EQ(15, loc.bounding_box.y.low);
- EXPECT_EQ(21, loc.bounding_box.x.high);
- EXPECT_EQ(25, loc.bounding_box.y.high);
+ for (const auto & loc : {
+ parse("(2,10,20,5,0,0,0,2147483648)"),
+ parse_new("{p:{x:10,y:20},r:5,a:2147483648}")
+ }) {
+ EXPECT_EQ(true, loc.has_point);
+ EXPECT_EQ(true, loc.bounding_box.active());
+ EXPECT_EQ(2147483648u, loc.x_aspect.multiplier);
+ EXPECT_EQ(10, loc.point.x);
+ EXPECT_EQ(20, loc.point.y);
+ EXPECT_EQ(5u, loc.radius);
+ EXPECT_EQ(-1, loc.bounding_box.x.low);
+ EXPECT_EQ(15, loc.bounding_box.y.low);
+ EXPECT_EQ(21, loc.bounding_box.x.high);
+ EXPECT_EQ(25, loc.bounding_box.y.high);
+ }
+ auto loc2 = parse_new("{p:{x:10,y:10},a:3123456789}");
+ EXPECT_EQ(3123456789, loc2.x_aspect.multiplier);
}
TEST(GeoLocationParserTest, bounding_box_can_be_specified_after_circle) {
- auto loc = parse("(2,10,20,5,0,0,0)[2,10,20,30,40]");
- EXPECT_EQ(true, loc.has_point);
- EXPECT_EQ(true, loc.bounding_box.active());
- EXPECT_EQ(0u, loc.x_aspect.multiplier);
- EXPECT_EQ(10, loc.point.x);
- EXPECT_EQ(20, loc.point.y);
- EXPECT_EQ(5u, loc.radius);
- EXPECT_EQ(10, loc.bounding_box.x.low);
- EXPECT_EQ(20, loc.bounding_box.y.low);
- EXPECT_EQ(15, loc.bounding_box.x.high);
- EXPECT_EQ(25, loc.bounding_box.y.high);
+ for (const auto & loc : {
+ parse("(2,10,20,5,0,0,0)[2,10,20,30,40]"),
+ parse_new("{p:{x:10,y:20},r:5,b:{x:[10,30],y:[20,40]}}")
+ }) {
+ EXPECT_EQ(true, loc.has_point);
+ EXPECT_EQ(true, loc.bounding_box.active());
+ EXPECT_EQ(0u, loc.x_aspect.multiplier);
+ EXPECT_EQ(10, loc.point.x);
+ EXPECT_EQ(20, loc.point.y);
+ EXPECT_EQ(5u, loc.radius);
+ EXPECT_EQ(10, loc.bounding_box.x.low);
+ EXPECT_EQ(20, loc.bounding_box.y.low);
+ EXPECT_EQ(15, loc.bounding_box.x.high);
+ EXPECT_EQ(25, loc.bounding_box.y.high);
+ }
}
TEST(GeoLocationParserTest, circles_can_be_specified_after_bounding_box) {
- auto loc = parse("[2,10,20,30,40](2,10,20,5,0,0,0)");
+ for (const auto & loc : {
+ parse("[2,10,20,30,40](2,10,20,5,0,0,0)"),
+ parse_new("{b:{x:[10,30],y:[20,40]},p:{x:10,y:20},r:5}")
+ }) {
+ EXPECT_EQ(true, loc.has_point);
+ EXPECT_EQ(true, loc.bounding_box.active());
+ EXPECT_EQ(0u, loc.x_aspect.multiplier);
+ EXPECT_EQ(10, loc.point.x);
+ EXPECT_EQ(20, loc.point.y);
+ EXPECT_EQ(5u, loc.radius);
+ EXPECT_EQ(10, loc.bounding_box.x.low);
+ EXPECT_EQ(20, loc.bounding_box.y.low);
+ EXPECT_EQ(15, loc.bounding_box.x.high);
+ EXPECT_EQ(25, loc.bounding_box.y.high);
+ }
+ const auto &loc = parse_new("{a:12345,b:{x:[8,10],y:[8,10]},p:{x:10,y:10},r:3}");
EXPECT_EQ(true, loc.has_point);
- EXPECT_EQ(true, loc.bounding_box.active());
- EXPECT_EQ(0u, loc.x_aspect.multiplier);
EXPECT_EQ(10, loc.point.x);
- EXPECT_EQ(20, loc.point.y);
- EXPECT_EQ(5u, loc.radius);
- EXPECT_EQ(10, loc.bounding_box.x.low);
- EXPECT_EQ(20, loc.bounding_box.y.low);
- EXPECT_EQ(15, loc.bounding_box.x.high);
- EXPECT_EQ(25, loc.bounding_box.y.high);
+ EXPECT_EQ(10, loc.point.y);
+ EXPECT_EQ(12345u, loc.x_aspect.multiplier);
}
TEST(GeoLocationParserTest, santa_search_gives_non_wrapped_bounding_box) {
- auto loc = parse("(2,122163600,89998536,290112,4,2000,0,109704)");
- EXPECT_GE(loc.bounding_box.x.high, loc.bounding_box.x.low);
- EXPECT_GE(loc.bounding_box.y.high, loc.bounding_box.y.low);
+ for (const auto & loc : {
+ parse("(2,122163600,89998536,290112,4,2000,0,109704)"),
+ parse_new("{p:{x:122163600,y:89998536},r:290112,a:109704}")
+ }) {
+ EXPECT_GE(loc.bounding_box.x.high, loc.bounding_box.x.low);
+ EXPECT_GE(loc.bounding_box.y.high, loc.bounding_box.y.low);
+ }
}
TEST(GeoLocationParserTest, near_boundary_search_gives_non_wrapped_bounding_box) {
- auto loc1 = parse("(2,2000000000,2000000000,3000000000,0,1,0)");
- EXPECT_GE(loc1.bounding_box.x.high, loc1.bounding_box.x.low);
- EXPECT_GE(loc1.bounding_box.y.high, loc1.bounding_box.y.low);
- EXPECT_EQ(std::numeric_limits<int32_t>::max(), loc1.bounding_box.y.high);
- EXPECT_EQ(std::numeric_limits<int32_t>::max(), loc1.bounding_box.y.high);
-
- auto loc2 = parse("(2,-2000000000,-2000000000,3000000000,0,1,0)");
- EXPECT_GE(loc2.bounding_box.x.high, loc2.bounding_box.x.low);
- EXPECT_GE(loc2.bounding_box.y.high, loc2.bounding_box.y.low);
- EXPECT_EQ(std::numeric_limits<int32_t>::min(), loc2.bounding_box.x.low);
- EXPECT_EQ(std::numeric_limits<int32_t>::min(), loc2.bounding_box.y.low);
+ for (const auto & loc1 : {
+ parse("(2,2000000000,2000000000,3000000000,0,1,0)"),
+ parse_new("{p:{x:2000000000,y:2000000000},r:3000000000}")
+ }) {
+ EXPECT_GE(loc1.bounding_box.x.high, loc1.bounding_box.x.low);
+ EXPECT_GE(loc1.bounding_box.y.high, loc1.bounding_box.y.low);
+ EXPECT_EQ(std::numeric_limits<int32_t>::max(), loc1.bounding_box.y.high);
+ EXPECT_EQ(std::numeric_limits<int32_t>::max(), loc1.bounding_box.y.high);
+ }
+
+ for (const auto & loc2 : {
+ parse("(2,-2000000000,-2000000000,3000000000,0,1,0)"),
+ parse_new("{p:{x:-2000000000,y:-2000000000},r:3000000000}")
+ }) {
+ EXPECT_GE(loc2.bounding_box.x.high, loc2.bounding_box.x.low);
+ EXPECT_GE(loc2.bounding_box.y.high, loc2.bounding_box.y.low);
+ EXPECT_EQ(std::numeric_limits<int32_t>::min(), loc2.bounding_box.x.low);
+ EXPECT_EQ(std::numeric_limits<int32_t>::min(), loc2.bounding_box.y.low);
+ }
}
void check_box(const GeoLocation &location, Box expected)
@@ -390,4 +471,67 @@ TEST(GeoLocationTest, box_point_radius_and_aspect) {
EXPECT_EQ(location.bounding_box.y.high, 700);
}
+TEST(GeoLocationParserTest, can_parse_what_query_tree_produces) {
+ search::query::Point point_1{-17, 42};
+ uint32_t distance = 12345;
+ uint32_t aspect_ratio = 67890;
+ search::query::Rectangle rectangle_1(-1, -2, 3, 4);
+
+ search::query::Location loc_1(point_1);
+ std::string str_1 = loc_1.getJsonFormatString();
+ auto result_1 = parse_new(str_1);
+
+ EXPECT_EQ(true, result_1.has_point);
+ EXPECT_EQ(false, result_1.has_radius());
+ EXPECT_EQ(false, result_1.x_aspect.active());
+ EXPECT_EQ(false, result_1.bounding_box.active());
+ EXPECT_EQ(-17, result_1.point.x);
+ EXPECT_EQ(42, result_1.point.y);
+
+ search::query::Location loc_1b(point_1, distance, aspect_ratio);
+ std::string str_1b = loc_1b.getJsonFormatString();
+ auto result_1b = parse_new(str_1b);
+
+ EXPECT_EQ(true, result_1b.has_point);
+ EXPECT_EQ(true, result_1b.has_radius());
+ EXPECT_EQ(true, result_1b.x_aspect.active());
+ EXPECT_EQ(true, result_1b.bounding_box.active());
+ EXPECT_EQ(-17, result_1b.point.x);
+ EXPECT_EQ(42, result_1b.point.y);
+ EXPECT_EQ(distance, result_1b.radius);
+ EXPECT_EQ(aspect_ratio, result_1b.x_aspect.multiplier);
+ EXPECT_EQ(42-distance, result_1b.bounding_box.y.low);
+ EXPECT_EQ(42+distance, result_1b.bounding_box.y.high);
+
+ search::query::Location loc_2(rectangle_1);
+ std::string str_2 = loc_2.getJsonFormatString();
+ auto result_2 = parse_new(str_2);
+
+ EXPECT_EQ(false, result_2.has_point);
+ EXPECT_EQ(false, result_2.has_radius());
+ EXPECT_EQ(false, result_2.x_aspect.active());
+ EXPECT_EQ(true, result_2.bounding_box.active());
+ EXPECT_EQ(-1, result_2.bounding_box.x.low);
+ EXPECT_EQ(-2, result_2.bounding_box.y.low);
+ EXPECT_EQ(3, result_2.bounding_box.x.high);
+ EXPECT_EQ(4, result_2.bounding_box.y.high);
+
+ search::query::Location loc_3(rectangle_1, point_1, distance, aspect_ratio);
+ std::string str_3 = loc_3.getJsonFormatString();
+ auto result_3 = parse_new(str_3);
+
+ EXPECT_EQ(true, result_3.has_point);
+ EXPECT_EQ(true, result_3.has_radius());
+ EXPECT_EQ(true, result_3.x_aspect.active());
+ EXPECT_EQ(true, result_3.bounding_box.active());
+ EXPECT_EQ(-17, result_3.point.x);
+ EXPECT_EQ(42, result_3.point.y);
+ EXPECT_EQ(distance, result_3.radius);
+ EXPECT_EQ(aspect_ratio, result_3.x_aspect.multiplier);
+ EXPECT_EQ(-1, result_3.bounding_box.x.low);
+ EXPECT_EQ(-2, result_3.bounding_box.y.low);
+ EXPECT_EQ(3, result_3.bounding_box.x.high);
+ EXPECT_EQ(4, result_3.bounding_box.y.high);
+}
+
GTEST_MAIN_RUN_ALL_TESTS()
diff --git a/searchlib/src/vespa/searchlib/common/geo_location_parser.cpp b/searchlib/src/vespa/searchlib/common/geo_location_parser.cpp
index 05c53348699..935e3e300eb 100644
--- a/searchlib/src/vespa/searchlib/common/geo_location_parser.cpp
+++ b/searchlib/src/vespa/searchlib/common/geo_location_parser.cpp
@@ -3,6 +3,11 @@
#include "geo_location_parser.h"
#include <limits>
#include <vespa/vespalib/stllike/asciistream.h>
+#include <vespa/vespalib/data/slime/slime.h>
+#include <vespa/vespalib/data/slime/json_format.h>
+
+#include <vespa/log/log.h>
+LOG_SETUP(".searchlib.common.geo_location_parser");
namespace {
@@ -39,7 +44,7 @@ GeoLocationParser::GeoLocationParser()
_max_x(std::numeric_limits<int32_t>::max()),
_min_y(std::numeric_limits<int32_t>::min()),
_max_y(std::numeric_limits<int32_t>::max()),
- _parseError(NULL)
+ _parseError(nullptr)
{}
bool
@@ -62,7 +67,7 @@ GeoLocationParser::parseOldFormatWithField(const std::string &str)
{
auto sep = str.find(':');
if (sep == std::string::npos) {
- _parseError = "Location string lacks field specification.";
+ _parseError = "Location string lacks field specification";
return false;
}
_field_name = str.substr(0, sep);
@@ -179,9 +184,86 @@ GeoLocationParser::parseOldFormat(const std::string &locStr)
return _valid;
}
+bool
+GeoLocationParser::parseWithField(const std::string &str)
+{
+ auto sep = str.find(':');
+ if (sep == std::string::npos) {
+ _parseError = "Location string lacks field specification";
+ return false;
+ }
+ _field_name = str.substr(0, sep);
+ std::string only_loc = str.substr(sep + 1);
+ return parseNoField(only_loc);
+}
+
+bool
+GeoLocationParser::parseNoField(const std::string &str)
+{
+ if (str.empty()) {
+ _parseError = "Location string is empty";
+ return false;
+ }
+ if (str[0] == '(' || str[0] == '[') {
+ return parseOldFormat(str);
+ }
+ if (str[0] != '{') {
+ _parseError = "Location string should start with '{'";
+ return false;
+ }
+ return parseJsonFormat(str);
+}
+
+bool
+GeoLocationParser::parseJsonFormat(const std::string &str)
+{
+ vespalib::Slime slime;
+ size_t decoded = vespalib::slime::JsonFormat::decode(str, slime);
+ if (decoded == 0) {
+ LOG(warning, "bad location JSON: %s\n",
+ slime.get()["error_message"].asString().data);
+ _parseError = "Failed decoding JSON format location";
+ return false;
+ }
+ // fprintf(stderr, "parsed location JSON %s -> %s\n", str.c_str(), slime.toString().c_str());
+ const auto &root = slime.get();
+ const auto &point = root["p"];
+ const auto &radius = root["r"];
+ const auto &aspect = root["a"];
+ const auto &bbox = root["b"];
+
+ if (point.valid()) {
+ _x = point["x"].asLong();
+ _y = point["y"].asLong();
+ _has_point = true;
+ }
+ if (radius.valid()) {
+ _radius = radius.asLong();
+ }
+ if (aspect.valid()) {
+ _x_aspect = aspect.asLong();
+ }
+ if (bbox.valid()) {
+ _min_x = bbox["x"][0].asLong();
+ _max_x = bbox["x"][1].asLong();
+ _min_y = bbox["y"][0].asLong();
+ _max_y = bbox["y"][1].asLong();
+ _has_bounding_box = true;
+ }
+ if (_has_point || _has_bounding_box) {
+ _valid = true;
+ } else {
+ _parseError = "Neither point nor bounding box found";
+ }
+ return _valid;
+}
+
GeoLocation
GeoLocationParser::getGeoLocation() const
{
+ if (! _valid) {
+ return GeoLocation();
+ }
GeoLocation::Aspect aspect(_x_aspect);
if (_has_bounding_box) {
GeoLocation::Range x_range{_min_x, _max_x};
diff --git a/searchlib/src/vespa/searchlib/common/geo_location_parser.h b/searchlib/src/vespa/searchlib/common/geo_location_parser.h
index 8936a620d21..7d725f4a035 100644
--- a/searchlib/src/vespa/searchlib/common/geo_location_parser.h
+++ b/searchlib/src/vespa/searchlib/common/geo_location_parser.h
@@ -17,6 +17,9 @@ class GeoLocationParser
public:
GeoLocationParser();
+ bool parseNoField(const std::string &locStr);
+ bool parseWithField(const std::string &locStr);
+
bool parseOldFormat(const std::string &locStr);
bool parseOldFormatWithField(const std::string &str);
@@ -42,6 +45,7 @@ private:
const char *_parseError;
bool correctDimensionalitySkip(const char * &p);
+ bool parseJsonFormat(const std::string &locStr);
};
} // namespace
diff --git a/searchlib/src/vespa/searchlib/query/tree/location.cpp b/searchlib/src/vespa/searchlib/query/tree/location.cpp
index 6e678f9e682..44f0b82d304 100644
--- a/searchlib/src/vespa/searchlib/query/tree/location.cpp
+++ b/searchlib/src/vespa/searchlib/query/tree/location.cpp
@@ -33,12 +33,13 @@ Location::Location(const Rectangle &rect)
bool
Location::operator==(const Location &other) const
{
- auto me = getOldFormatString();
- auto it = other.getOldFormatString();
+ auto me = getJsonFormatString();
+ auto it = other.getJsonFormatString();
if (me == it) {
return true;
} else {
// dump 'me' and 'it' here if unit tests fail
+ // fprintf(stderr, "me='%s', it='%s'\n", me.c_str(), it.c_str());
return false;
}
}
@@ -69,8 +70,37 @@ Location::getOldFormatString() const
return buf.str();
}
+std::string
+Location::getJsonFormatString() const
+{
+ // Only produce what search::common::GeoLocationParser can parse
+ vespalib::asciistream buf;
+ buf << "{";
+ if (has_point) {
+ buf << "p:{x:" << point.x << ",y:" << point.y << "}";
+ if (has_radius()) {
+ buf << "," << "r:" << radius;
+ }
+ if (x_aspect.active()) {
+ buf << "," << "a:" << x_aspect.multiplier;
+ }
+ }
+ if (bounding_box.active()) {
+ if (has_point) {
+ buf << ",";
+ }
+ buf << "b:{x:[" << bounding_box.x.low
+ << "," << bounding_box.x.high
+ << "],y:[" << bounding_box.y.low
+ << "," << bounding_box.y.high
+ << "]}" ;
+ }
+ buf << "}";
+ return buf.str();
+}
+
vespalib::asciistream &operator<<(vespalib::asciistream &out, const Location &loc) {
return out << loc.getOldFormatString();
}
-}
+} // namespace
diff --git a/searchlib/src/vespa/searchlib/query/tree/location.h b/searchlib/src/vespa/searchlib/query/tree/location.h
index 6b8090f45e1..143282e2958 100644
--- a/searchlib/src/vespa/searchlib/query/tree/location.h
+++ b/searchlib/src/vespa/searchlib/query/tree/location.h
@@ -22,6 +22,7 @@ public:
bool operator==(const Location &other) const;
std::string getOldFormatString() const;
+ std::string getJsonFormatString() const;
};
vespalib::asciistream &operator<<(vespalib::asciistream &out, const Location &loc);