From 43f600ca3a9fdbd72cbae740e5275107c2dca924 Mon Sep 17 00:00:00 2001 From: Xin Liu Date: Thu, 21 Nov 2024 21:35:04 +0800 Subject: [PATCH] feat: add `collection_exists` and `delete_collection` Signed-off-by: Xin Liu --- src/lib.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 71fc9ac..fa2d2ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,10 @@ impl Qdrant { } pub async fn create_collection(&self, collection_name: &str, size: u32) -> Result<(), Error> { + if self.collection_exists(collection_name).await? { + bail!("Collection '{}' already exists", collection_name); + } + let params = json!({ "vectors": { "size": size, @@ -81,6 +85,18 @@ impl Qdrant { self.create_collection_api(collection_name, ¶ms).await } + pub async fn collection_exists(&self, collection_name: &str) -> Result { + self.collection_exists_api(collection_name).await + } + + pub async fn delete_collection(&self, collection_name: &str) -> Result<(), Error> { + if !self.collection_exists(collection_name).await? { + bail!("Collection '{}' does not exist", collection_name); + } + + self.delete_collection_api(collection_name).await + } + pub async fn upsert_points( &self, collection_name: &str, @@ -209,6 +225,13 @@ impl Qdrant { } } + pub async fn collection_exists_api(&self, collection_name: &str) -> Result { + let url = format!("{}/collections/{}/exists", self.url_base, collection_name,); + let client = reqwest::Client::new(); + let res = client.get(&url).send().await?; + Ok(res.status().is_success()) + } + pub async fn delete_collection_api(&self, collection_name: &str) -> Result<(), Error> { let url = format!("{}/collections/{}", self.url_base, collection_name,);