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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Synonym response should not leak underlying structure.
kishorenc3f0bb24b45cc
diff --git a/include/collection.h b/include/collection.hindex 93ebb728..66bade45 100644--- a/include/collection.h+++ b/include/collection.h@@ -127,6 +127,20 @@ struct synonym_t {return obj;}+ nlohmann::json to_view_json() const {+ nlohmann::json obj;+ obj["id"] = id;+ obj["root"] = StringUtils::join(root, " ");++ obj["synonyms"] = nlohmann::json::array();++ for(const auto& synonym: synonyms) {+ obj["synonyms"].push_back(StringUtils::join(synonym, " "));+ }++ return obj;+ }+static Option<bool> parse(const nlohmann::json& synonym_json, synonym_t& syn) {if(synonym_json.count("id") == 0) {return Option<bool>(400, "Missing `id` field.");diff --git a/src/core_api.cpp b/src/core_api.cppindex f8e4d8f9..315ad166 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -1365,7 +1365,7 @@ bool get_synonyms(http_req &req, http_res &res) {auto& synonyms = collection->get_synonyms();for(const auto & kv: synonyms) {- nlohmann::json synonym = kv.second.to_json();+ nlohmann::json synonym = kv.second.to_view_json();res_json["synonyms"].push_back(synonym);}@@ -1388,7 +1388,7 @@ bool get_synonym(http_req &req, http_res &res) {bool found = collection->get_synonym(synonym_id, synonym);if(found) {- nlohmann::json synonym_json = synonym.to_json();+ nlohmann::json synonym_json = synonym.to_view_json();res.set_200(synonym_json.dump());return true;}diff --git a/test/collection_synonyms_test.cpp b/test/collection_synonyms_test.cppindex 73637c3f..b87e7335 100644--- a/test/collection_synonyms_test.cpp+++ b/test/collection_synonyms_test.cpp@@ -504,3 +504,16 @@ TEST_F(CollectionSynonymsTest, DeleteAndUpsertDuplicationOfSynonms) {coll_mul_fields->remove_synonym("samsung-synonyms");ASSERT_EQ(0, coll_mul_fields->get_synonyms().size());}++TEST_F(CollectionSynonymsTest, SynonymJsonSerialization) {+ synonym_t synonym1{"ipod-synonyms", {"apple", "ipod"}, {{"ipod"}, {"i", "pod"}, {"pod"}}};+ nlohmann::json obj = synonym1.to_view_json();+ ASSERT_STREQ("ipod-synonyms", obj["id"].get<std::string>().c_str());+ ASSERT_STREQ("apple ipod", obj["root"].get<std::string>().c_str());++ ASSERT_EQ(3, obj["synonyms"].size());+ ASSERT_STREQ("ipod", obj["synonyms"][0].get<std::string>().c_str());+ ASSERT_STREQ("i pod", obj["synonyms"][1].get<std::string>().c_str());+ ASSERT_STREQ("pod", obj["synonyms"][2].get<std::string>().c_str());++}\ No newline at end of file