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

API for summary of a collection, including the number of documents indexed in the collection.

Kishore Nallan3e54cb402267
diff --git a/TODO.md b/TODO.md
index 3cfead19..68428d37 100644
--- a/TODO.md
+++ b/TODO.md
@@ -54,6 +54,7 @@
- ID should not have "/"
- Group results by field
- Handle store-get() not finding a key
+- Delete using range: https://github.com/facebook/rocksdb/wiki/Delete-A-Range-Of-Keys
- ~~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 359df3f5..24f8352c 100644
--- a/include/api.h
+++ b/include/api.h
@@ -10,6 +10,8 @@ void del_drop_collection(http_req & req, http_res & res);
void get_search(http_req & req, http_res & res);
+void get_collection_summary(http_req & req, http_res & res);
+
void post_add_document(http_req & req, http_res & res);
void get_fetch_document(http_req & req, http_res & res);
diff --git a/include/collection.h b/include/collection.h
index 16296986..f63d8acd 100644
--- a/include/collection.h
+++ b/include/collection.h
@@ -43,6 +43,8 @@ private:
uint32_t collection_id;
+ size_t num_documents;
+
// Auto incrementing record ID used internally for indexing - not exposed to the client
uint32_t next_seq_id;
@@ -122,6 +124,8 @@ public:
std::string get_name();
+ size_t get_num_documents();
+
uint32_t get_collection_id();
uint32_t get_next_seq_id();
diff --git a/src/api.cpp b/src/api.cpp
index adab54ea..9cef8c4b 100644
--- a/src/api.cpp
+++ b/src/api.cpp
@@ -254,6 +254,21 @@ void get_search(http_req & req, http_res & res) {
std::cout << "Time taken: " << timeMillis << "ms" << std::endl;
}
+void get_collection_summary(http_req & req, http_res & res) {
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+ Collection* collection = collectionManager.get_collection(req.params["collection"]);
+
+ if(collection == nullptr) {
+ return res.send_404();
+ }
+
+ nlohmann::json json_response;
+
+ json_response["name"] = collection->get_name();
+ json_response["num_documents"] = collection->get_num_documents();
+ res.send_200(json_response.dump());
+}
+
void post_add_document(http_req & req, http_res & res) {
CollectionManager & collectionManager = CollectionManager::get_instance();
Collection* collection = collectionManager.get_collection(req.params["collection"]);
diff --git a/src/collection.cpp b/src/collection.cpp
index 3092562f..86be8e91 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -30,6 +30,8 @@ Collection::Collection(const std::string name, const uint32_t collection_id, con
spp::sparse_hash_map<uint32_t, int64_t> * doc_to_score = new spp::sparse_hash_map<uint32_t, int64_t>();
sort_index.emplace(sort_field.name, doc_to_score);
}
+
+ num_documents = 0;
}
Collection::~Collection() {
@@ -208,6 +210,7 @@ Option<uint32_t> Collection::index_in_memory(const nlohmann::json &document, uin
doc_to_score->emplace(seq_id, document[sort_field.name].get<int64_t>());
}
+ num_documents += 1;
return Option<>(200);
}
@@ -1200,6 +1203,8 @@ Option<std::string> Collection::remove(const std::string & id) {
store->remove(get_doc_id_key(id));
store->remove(get_seq_id_key(seq_id));
+ num_documents -= 1;
+
return Option<std::string>(id);
}
@@ -1226,6 +1231,10 @@ std::string Collection::get_name() {
return name;
}
+size_t Collection::get_num_documents() {
+ return num_documents;
+}
+
uint32_t Collection::get_collection_id() {
return collection_id;
}
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 31f0308d..bb1bb66c 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -29,11 +29,12 @@ int main(int argc, char **argv) {
// collection management
server.post("/collections", post_create_collection, true);
+ server.get("/collections", get_collections, true);
server.del("/collections/:collection", del_drop_collection, true);
// document management
- server.get("/collections", get_collections, true);
server.post("/collections/:collection", post_add_document, true);
+ server.get("/collections/:collection", get_collection_summary, true);
server.get("/collections/:collection/search", get_search, false);
server.get("/collections/:collection/:id", get_fetch_document, true);
server.del("/collections/:collection/:id", del_remove_document, true);