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 drop colllection API.

Kishore Nallanbe0a222ccb2e
diff --git a/TODO.md b/TODO.md
index a3cee9f8..54717680 100644
--- a/TODO.md
+++ b/TODO.md
@@ -40,13 +40,16 @@
- ~~Snippet should only be around surrounding matching tokens~~
- ~~Proper pagination~~
- ~~Pagination parameter~~
+- ~~Drop collection API~~
- When prefix=true, use token_ranking_field for token ordering only for last word
- Query token ids should match query token ordering
- ID should not have "/"
- Group results by field
+- Number of records in collection
+- Fix API response codes
+- JSONP response
- Use rocksdb batch put for atomic insertion
- Handle store-get() not finding a key
-- Fix API response codes
- ~~Test for asc/desc upper/lower casing~~
- ~~Test for search without any sort_by given~~
- ~~Test for collection creation validation~~
diff --git a/include/api.h b/include/api.h
index 7d18bd45..8597ca81 100644
--- a/include/api.h
+++ b/include/api.h
@@ -4,6 +4,8 @@
void post_create_collection(http_req & req, http_res & res);
+void del_drop_collection(http_req & req, http_res & res);
+
void get_search(http_req & req, http_res & res);
void post_add_document(http_req & req, http_res & res);
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 84f27ac2..cc6b2e74 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -51,7 +51,7 @@ public:
Collection* get_collection(std::string collection_name);
- bool drop_collection(std::string collection_name);
+ Option<bool> drop_collection(std::string collection_name);
uint32_t get_next_collection_id();
diff --git a/src/api.cpp b/src/api.cpp
index e17eafaf..59b00c6a 100644
--- a/src/api.cpp
+++ b/src/api.cpp
@@ -115,6 +115,21 @@ void post_create_collection(http_req & req, http_res & res) {
res.send_201(req.body);
}
+void del_drop_collection(http_req & req, http_res & res) {
+ std::string doc_id = req.params["id"];
+
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+ Option<bool> drop_result = collectionManager.drop_collection(req.params["collection"]);
+
+ if(!drop_result.ok()) {
+ return res.send(drop_result.code(), drop_result.error());
+ }
+
+ nlohmann::json json_response;
+ json_response["collection"] = req.params["collection"];
+ res.send_200(json_response.dump());
+}
+
void get_search(http_req & req, http_res & res) {
auto begin = std::chrono::high_resolution_clock::now();
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index eebd2210..885935b2 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -153,10 +153,10 @@ Collection* CollectionManager::get_collection(std::string collection_name) {
return nullptr;
}
-bool CollectionManager::drop_collection(std::string collection_name) {
+Option<bool> CollectionManager::drop_collection(std::string collection_name) {
Collection* collection = get_collection(collection_name);
if(collection == nullptr) {
- return false;
+ return Option<bool>(404, "No collection with name `" + collection_name + "` found.");
}
store->remove(Collection::get_meta_key(collection_name));
@@ -176,7 +176,7 @@ bool CollectionManager::drop_collection(std::string collection_name) {
delete collection;
collection = nullptr;
- return true;
+ return Option<bool>(true);
}
uint32_t CollectionManager::get_next_collection_id() {
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 4aea7a79..3da9adfb 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -19,7 +19,11 @@ int main(int argc, char **argv) {
options.get<uint32_t>("listen-port")
);
+ // collection management
server.post("/collection", post_create_collection, true);
+ server.del("/collection/:collection", del_drop_collection, true);
+
+ // document management
server.post("/collection/:collection", post_add_document, true);
server.get("/collection/:collection/search", get_search, false);
server.del("/collection/:collection/:id", del_remove_document, true);