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
Support upsert of synonym.
kishorenccd0a58d7b55e
diff --git a/src/collection.cpp b/src/collection.cppindex b5260096..63ea015c 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -1563,10 +1563,6 @@ Option<bool> Collection::remove_if_found(uint32_t seq_id, const bool remove_from}Option<uint32_t> Collection::add_override(const override_t & override) {- if(overrides.count("id") != 0) {- return Option<uint32_t>(409, "There is already another entry with that `id`.");- }-bool inserted = store->insert(Collection::get_override_key(name, override.id), override.to_json().dump());if(!inserted) {return Option<uint32_t>(500, "Error while storing the override on disk.");@@ -1907,7 +1903,11 @@ void Collection::synonym_reduction(const std::vector<std::string>& tokens, std::Option<bool> Collection::add_synonym(const synonym_t& synonym) {if(synonym_definitions.count(synonym.id) != 0) {- return Option<bool>(409, "There is already another synonym with that `id`.");+ // first we have to delete existing entries so we can upsert+ Option<bool> rem_op = remove_synonym(synonym.id);+ if(!rem_op.ok()) {+ return rem_op;+ }}synonym_definitions[synonym.id] = synonym;diff --git a/test/collection_synonyms_test.cpp b/test/collection_synonyms_test.cppindex 9c597d6c..a126b2a5 100644--- a/test/collection_synonyms_test.cpp+++ b/test/collection_synonyms_test.cpp@@ -361,6 +361,12 @@ TEST_F(CollectionSynonymsTest, MultiWaySynonym) {ASSERT_EQ(2, res["found"].get<uint32_t>());ASSERT_STREQ("<mark>Samuel</mark> L. <mark>Jackson</mark>", res["hits"][0]["highlights"][0]["snippet"].get<std::string>().c_str());ASSERT_STREQ("<mark>Samuel</mark> L. <mark>Jackson</mark>", res["hits"][1]["highlights"][0]["snippet"].get<std::string>().c_str());++ // for now we don't support synonyms on ANY prefix++ res = coll_mul_fields->search("ler", {"starring"}, "", {}, {}, 0, 10, 1, FREQUENCY, true).get();+ ASSERT_EQ(0, res["hits"].size());+ ASSERT_EQ(0, res["found"].get<uint32_t>());}TEST_F(CollectionSynonymsTest, ExactMatchRankedHigherThanSynonymMatch) {@@ -482,9 +488,18 @@ TEST_F(CollectionSynonymsTest, DeleteAndUpsertDuplicationOfSynonms) {ASSERT_STREQ("samsung-synonyms", coll_mul_fields->get_synonyms()["samsung-synonyms"].id.c_str());// try to upsert synonym with same ID++ synonym2.root = {"s3", "smartphone"};auto upsert_op = coll_mul_fields->add_synonym(synonym2);- ASSERT_FALSE(upsert_op.ok());- ASSERT_STREQ("There is already another synonym with that `id`.", upsert_op.error().c_str());+ ASSERT_TRUE(upsert_op.ok());++ ASSERT_EQ(1, coll_mul_fields->get_synonyms().size());++ synonym_t synonym2_updated;+ coll_mul_fields->get_synonym(synonym2.id, synonym2_updated);++ ASSERT_EQ("s3", synonym2_updated.root[0]);+ ASSERT_EQ("smartphone", synonym2_updated.root[1]);coll_mul_fields->remove_synonym("samsung-synonyms");ASSERT_EQ(0, coll_mul_fields->get_synonyms().size());