mirror of
https://github.com/typesense/typesense.git
synced 2025-05-17 04:02:36 +08:00
When sorting on geo field, return distance from reference point in response.
This commit is contained in:
parent
40305791f8
commit
4d1390ce91
@ -1173,6 +1173,17 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
|
||||
wrapper_doc["text_match"] = field_order_kv->scores[field_order_kv->match_score_index];
|
||||
}
|
||||
|
||||
nlohmann::json geo_distances;
|
||||
|
||||
for(size_t sort_field_index = 0; sort_field_index < sort_fields_std.size(); sort_field_index++) {
|
||||
const auto& sort_field = sort_fields_std[sort_field_index];
|
||||
if(sort_field.geopoint != 0) {
|
||||
geo_distances[sort_field.name] = std::abs(field_order_kv->scores[sort_field_index]);
|
||||
}
|
||||
}
|
||||
|
||||
wrapper_doc["geo_distance_meters"] = geo_distances;
|
||||
|
||||
hits_array.push_back(wrapper_doc);
|
||||
}
|
||||
|
||||
|
@ -1061,7 +1061,7 @@ TEST_F(CollectionFilteringTest, GeoPointFiltering) {
|
||||
ASSERT_FALSE(add_op.ok());
|
||||
ASSERT_EQ("Field `loc` must be a geopoint.", add_op.error());
|
||||
|
||||
// under corcion mode, it should work
|
||||
// under coercion mode, it should work
|
||||
bad_doc["loc"] = {"48.91", "2.33"};
|
||||
add_op = coll1->add(bad_doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_REJECT);
|
||||
ASSERT_TRUE(add_op.ok());
|
||||
|
@ -585,9 +585,13 @@ TEST_F(CollectionSortingTest, GeoPointSorting) {
|
||||
ASSERT_STREQ(expected_ids[i].c_str(), results["hits"][i]["document"]["id"].get<std::string>().c_str());
|
||||
}
|
||||
|
||||
ASSERT_EQ(348, results["hits"][0]["geo_distance_meters"]["loc"].get<int>());
|
||||
ASSERT_EQ(900, results["hits"][1]["geo_distance_meters"]["loc"].get<int>());
|
||||
ASSERT_EQ(1973, results["hits"][2]["geo_distance_meters"]["loc"].get<int>());
|
||||
|
||||
// desc, without filter
|
||||
geo_sort_fields = {
|
||||
sort_by("loc(48.84442912268208, 2.3490714964332353)", "DESC")
|
||||
sort_by("loc(48.84442912268208, 2.3490714964332353)", "DESC")
|
||||
};
|
||||
|
||||
results = coll1->search("*",
|
||||
|
Loading…
x
Reference in New Issue
Block a user