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

Expose document update API endpoint.

kishorenc3ed27ab8148a
diff --git a/include/collection.h b/include/collection.h
index 5faad469..1069d379 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -222,11 +222,12 @@ public:
std::string get_default_sorting_field();
- Option<doc_seq_id_t> to_doc(const std::string & json_str, nlohmann::json & document, bool upsert);
+ Option<doc_seq_id_t> to_doc(const std::string& json_str, nlohmann::json& document, bool upsert,
+ const std::string& id="");
nlohmann::json get_summary_json();
- Option<nlohmann::json> add(const std::string & json_str, const bool upsert=false);
+ Option<nlohmann::json> add(const std::string & json_str, const bool upsert=false, const std::string& id="");
nlohmann::json add_many(std::vector<std::string>& json_lines);
diff --git a/include/core_api.h b/include/core_api.h
index f4536644..e5b00fca 100644
--- a/include/core_api.h
+++ b/include/core_api.h
@@ -23,6 +23,8 @@ bool get_export_documents(http_req& req, http_res& res);
bool post_add_document(http_req& req, http_res& res);
+bool put_upsert_document(http_req& req, http_res& res);
+
bool post_import_documents(http_req& req, http_res& res);
bool get_fetch_document(http_req& req, http_res& res);
diff --git a/src/collection.cpp b/src/collection.cpp
index 99d77ff2..4b0a9e98 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -98,7 +98,8 @@ void Collection::increment_next_seq_id_field() {
next_seq_id++;
}
-Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::json & document, bool upsert) {
+Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::json & document,
+ bool upsert, const std::string& id) {
try {
document = nlohmann::json::parse(json_str);
} catch(const std::exception& e) {
@@ -110,6 +111,10 @@ Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::
return Option<doc_seq_id_t>(400, "Bad JSON: not a properly formed document.");
}
+ if(upsert && document.count("id") == 0 && !id.empty()) {
+ document["id"] = id;
+ }
+
if(document.count("id") == 0) {
if(upsert) {
return Option<doc_seq_id_t>(400, "For update, the `id` key must be present.");
@@ -174,9 +179,9 @@ nlohmann::json Collection::get_summary_json() {
return json_response;
}
-Option<nlohmann::json> Collection::add(const std::string & json_str, const bool upsert) {
+Option<nlohmann::json> Collection::add(const std::string & json_str, const bool upsert, const std::string& id) {
nlohmann::json document;
- Option<doc_seq_id_t> doc_seq_id_op = to_doc(json_str, document, upsert);
+ Option<doc_seq_id_t> doc_seq_id_op = to_doc(json_str, document, upsert, id);
if(!doc_seq_id_op.ok()) {
return Option<nlohmann::json>(doc_seq_id_op.code(), doc_seq_id_op.error());
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 460bfc70..6bbf7fcb 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -717,6 +717,28 @@ bool post_add_document(http_req & req, http_res & res) {
return true;
}
+bool put_upsert_document(http_req & req, http_res & res) {
+ std::string doc_id = req.params["id"];
+
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+ Collection* collection = collectionManager.get_collection(req.params["collection"]);
+
+ if(collection == nullptr) {
+ res.set_404();
+ return false;
+ }
+
+ Option<nlohmann::json> upserted_doc_op = collection->add(req.body, true, doc_id);
+
+ if(!upserted_doc_op.ok()) {
+ res.set(upserted_doc_op.code(), upserted_doc_op.error());
+ return false;
+ }
+
+ res.set_201(upserted_doc_op.get().dump());
+ return true;
+}
+
bool get_fetch_document(http_req & req, http_res & res) {
std::string doc_id = req.params["id"];
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 4164110e..a0caf2fc 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -21,6 +21,7 @@ void master_server_routes() {
// document management - `/documents/:id` end-points must be placed last in the list
server->post("/collections/:collection/documents", post_add_document);
+ server->put("/collections/:collection/documents/:id", put_upsert_document);
server->get("/collections/:collection/documents/search", get_search);
server->post("/collections/:collection/documents/import", post_import_documents, true, true);
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 897d3b45..c7589dca 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2303,6 +2303,20 @@ TEST_F(CollectionTest, UpdateDocument) {
ASSERT_EQ(1, res["hits"].size());
ASSERT_EQ(99, res["hits"][0]["document"]["points"].get<size_t>());
+
+ // id can be passed by param
+ nlohmann::json doc4;
+ doc4["points"] = 105;
+
+ add_op = coll1->add(doc4.dump(), true, "100");
+ ASSERT_TRUE(add_op.ok());
+
+ res = coll1->search("*", {"tags"}, "points: > 101", {}, sort_fields, 0, 10, 1,
+ token_ordering::FREQUENCY, true, 10, spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 5, "title").get();
+
+ ASSERT_EQ(1, res["hits"].size());
+ ASSERT_EQ(105, res["hits"][0]["document"]["points"].get<size_t>());
}
TEST_F(CollectionTest, SearchHighlightFieldFully) {