Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Force geopoint field to be sortable when schema is created.

Kishore Nallan84f5c4cd721c
diff --git a/src/field.cpp b/src/field.cpp
index ee3d5811..3bfc089b 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -525,6 +525,11 @@ Option<bool> field::json_field_to_field(nlohmann::json& field_json, std::vector<
field_json[fields::nested_array] = 0;
}
+ if(field_json[fields::type] == field_types::GEOPOINT && field_json[fields::sort] == false) {
+ LOG(WARNING) << "Forcing geopoint field `" << field_json[fields::name].get<std::string>() << "` to be sortable.";
+ field_json[fields::sort] = true;
+ }
+
the_fields.emplace_back(
field(field_json[fields::name], field_json[fields::type], field_json[fields::facet],
field_json[fields::optional], field_json[fields::index], field_json[fields::locale],
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index 5f1d3a09..464c4f5f 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -1399,6 +1399,56 @@ TEST_F(CollectionFilteringTest, GeoPolygonFilteringSouthAmerica) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionFilteringTest, GeoPointFilteringWithNonSortableLocationField) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("loc", field_types::GEOPOINT, false),
+ field("points", field_types::INT32, false),};
+
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "fields": [
+ {"name": "title", "type": "string", "sort": false},
+ {"name": "loc", "type": "geopoint", "sort": false},
+ {"name": "points", "type": "int32", "sort": false}
+ ]
+ })"_json;
+
+ auto coll_op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(coll_op.ok());
+ Collection* coll1 = coll_op.get();
+
+ std::vector<std::vector<std::string>> records = {
+ {"Palais Garnier", "48.872576479306765, 2.332291112241466"},
+ {"Sacre Coeur", "48.888286721920934, 2.342340862419206"},
+ {"Arc de Triomphe", "48.87538726829884, 2.296113163780903"},
+ };
+
+ for(size_t i=0; i<records.size(); i++) {
+ nlohmann::json doc;
+
+ std::vector<std::string> lat_lng;
+ StringUtils::split(records[i][1], lat_lng, ", ");
+
+ double lat = std::stod(lat_lng[0]);
+ double lng = std::stod(lat_lng[1]);
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = records[i][0];
+ doc["loc"] = {lat, lng};
+ doc["points"] = i;
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ // pick a location close to only the Sacre Coeur
+ auto results = coll1->search("*",
+ {}, "loc: (48.90615915923891, 2.3435897727061175, 3 km)",
+ {}, {}, {0}, 10, 1, FREQUENCY).get();
+
+ ASSERT_EQ(1, results["found"].get<size_t>());
+ ASSERT_EQ(1, results["hits"].size());
+}
+
TEST_F(CollectionFilteringTest, FilteringWithPrefixSearch) {
Collection *coll1;