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

Fix `geo_distance_meters` returning values in KM/MI. (#1962)

Harpreet Sangar31d495729844
diff --git a/include/field.h b/include/field.h
index 24588f33..9a8fa154 100644
--- a/include/field.h
+++ b/include/field.h
@@ -487,7 +487,6 @@ struct sort_by {
int64_t geopoint;
uint32_t exclude_radius;
uint32_t geo_precision;
- std::string unit;
missing_values_t missing_values;
eval_t eval;
@@ -525,7 +524,6 @@ struct sort_by {
geopoint = other.geopoint;
exclude_radius = other.exclude_radius;
geo_precision = other.geo_precision;
- unit = other.unit;
missing_values = other.missing_values;
eval = other.eval;
reference_collection_name = other.reference_collection_name;
@@ -545,7 +543,6 @@ struct sort_by {
geopoint = other.geopoint;
exclude_radius = other.exclude_radius;
geo_precision = other.geo_precision;
- unit = other.unit;
missing_values = other.missing_values;
eval = other.eval;
reference_collection_name = other.reference_collection_name;
diff --git a/include/index.h b/include/index.h
index b9cfe25c..46e845f8 100644
--- a/include/index.h
+++ b/include/index.h
@@ -1065,7 +1065,9 @@ public:
void aggregate_facet(const size_t group_limit, facet& this_facet, facet& acc_facet) const;
float get_distance(const string& geo_field_name, const uint32_t& seq_id,
- const S2LatLng& reference_lat_lng, const std::string& unit) const;
+ const S2LatLng& reference_lat_lng) const;
+
+ void get_top_k_result_ids(const std::vector<std::vector<KV*>>& raw_result_kvs, std::vector<uint32_t>& result_ids) const;
};
template<class T>
diff --git a/src/collection.cpp b/src/collection.cpp
index 2708b4a5..044740f1 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1375,7 +1375,6 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<
if(unit != "km" && unit != "mi") {
return Option<bool>(400, "Sort field's parameter unit must be either `km` or `mi`.");
}
- sort_field_std.unit = unit;
std::vector<std::string> dist_values;
StringUtils::split(param_parts[1], dist_values, unit);
@@ -2664,7 +2663,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
GeoPoint::unpack_lat_lng(sort_field.geopoint, reference_lat_lng);
geo_distances[sort_field.name] = index->get_distance(sort_field.name, field_order_kv->key,
- reference_lat_lng, sort_field.unit);
+ reference_lat_lng);
} else if(sort_field.geopoint != 0) {
geo_distances[sort_field.name] = std::abs(field_order_kv->scores[sort_field_index]);
} else if(sort_field.name == sort_field_const::vector_query &&
diff --git a/src/index.cpp b/src/index.cpp
index cf49a410..ad45ba8b 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -7922,7 +7922,7 @@ Option<uint32_t> Index::get_sort_index_value_with_lock(const std::string& collec
}
float Index::get_distance(const string& geo_field_name, const uint32_t& seq_id,
- const S2LatLng& reference_lat_lng, const std::string& unit) const {
+ const S2LatLng& reference_lat_lng) const {
std::unique_lock lock(mutex);
int64_t distance = 0;
@@ -7955,16 +7955,7 @@ float Index::get_distance(const string& geo_field_name, const uint32_t& seq_id,
}
}
- double dist = distance;
- if (unit == "km") {
- dist = dist * 0.001;
- } else if (unit == "mi") {
- dist = dist * 0.00062137;
- } else {
- return 0;
- }
-
- return std::round(dist * 1000.0) / 1000.0;
+ return std::round((double)distance * 1000.0) / 1000.0;
}
/*
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index d8806f3f..f68399d8 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -945,7 +945,7 @@ TEST_F(CollectionSortingTest, GeoPointSortingWithPrecision) {
std::vector<std::string> expected_ids = {
"6", "2", "1", "0", "3", "4", "7", "5"
};
- std::vector<float> geo_distance_meters = {0.726,0.461,0.46,0.467,1.786,2.007,3.556,3.299};
+ std::vector<float> geo_distance_meters = {726,461,460,467,1786,2007,3556,3299};
for (size_t i = 0; i < expected_ids.size(); i++) {
auto const& hit = results["hits"][i];