Skip to content

Commit

Permalink
refactor!: improve APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss committed Dec 16, 2024
1 parent c562b3e commit dede426
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
22 changes: 19 additions & 3 deletions examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,33 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
client.collection_info("my_test").await
);

let p = client.get_point("my_test", 2).await;
let p = client.get_point("my_test", &PointId::from(2)).await;
println!("The second point is {:?}", p);

let ps = client.get_points("my_test", vec![1, 2, 3, 4, 5, 6]).await;
let ps = client
.get_points(
"my_test",
&vec![1, 2, 3, 4, 5, 6]
.into_iter()
.map(|id| PointId::from(id))
.collect::<Vec<_>>(),
)
.await;
println!("The 1-6 points are {:?}", ps);

let q = vec![0.2, 0.1, 0.9, 0.7];
let r = client.search_points("my_test", q, 2, None).await;
println!("Search result points are {:?}", r);

let r = client.delete_points("my_test", vec![1, 4]).await;
let r = client
.delete_points(
"my_test",
&vec![1, 4]
.into_iter()
.map(|id| PointId::from(id))
.collect::<Vec<_>>(),
)
.await;
println!("Delete points result is {:?}", r);

println!(
Expand Down
33 changes: 26 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,32 @@ use anyhow::{anyhow, bail, Error};
use serde::{Deserialize, Serialize};
use serde_json::json;
use serde_json::{Map, Value};
use std::fmt::Display;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PointId {
Uuid(String),
Num(u64),
}
impl From<u64> for PointId {
fn from(num: u64) -> Self {
PointId::Num(num)
}
}
impl From<String> for PointId {
fn from(uuid: String) -> Self {
PointId::Uuid(uuid)
}
}
impl Display for PointId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PointId::Uuid(uuid) => write!(f, "{}", uuid),
PointId::Num(num) => write!(f, "{}", num),
}
}
}

/// The point struct.
/// A point is a record consisting of a vector and an optional payload.
Expand Down Expand Up @@ -179,7 +198,7 @@ impl Qdrant {
pub async fn search_points(
&self,
collection_name: &str,
point: Vec<f32>,
vector: Vec<f32>,
limit: u64,
score_threshold: Option<f32>,
) -> Result<Vec<ScoredPoint>, Error> {
Expand All @@ -192,7 +211,7 @@ impl Qdrant {
};

let params = json!({
"vector": point,
"vector": vector,
"limit": limit,
"with_payload": true,
"with_vector": true,
Expand Down Expand Up @@ -226,7 +245,7 @@ impl Qdrant {
}
}

pub async fn get_points(&self, collection_name: &str, ids: Vec<u64>) -> Vec<Point> {
pub async fn get_points(&self, collection_name: &str, ids: &[PointId]) -> Vec<Point> {
#[cfg(feature = "logging")]
info!(target: "stdout", "get points from collection '{}'", collection_name);

Expand All @@ -246,7 +265,7 @@ impl Qdrant {
ps
}

pub async fn get_point(&self, collection_name: &str, id: u64) -> Point {
pub async fn get_point(&self, collection_name: &str, id: &PointId) -> Point {
#[cfg(feature = "logging")]
info!(target: "stdout", "get point from collection '{}' with id {}", collection_name, id);

Expand All @@ -255,7 +274,7 @@ impl Qdrant {
serde_json::from_value(r.clone()).unwrap()
}

pub async fn delete_points(&self, collection_name: &str, ids: Vec<u64>) -> Result<(), Error> {
pub async fn delete_points(&self, collection_name: &str, ids: &[PointId]) -> Result<(), Error> {
#[cfg(feature = "logging")]
info!(target: "stdout", "delete points from collection '{}'", collection_name);

Expand Down Expand Up @@ -651,7 +670,7 @@ impl Qdrant {
Ok(json)
}

pub async fn get_point_api(&self, collection_name: &str, id: u64) -> Result<Value, Error> {
pub async fn get_point_api(&self, collection_name: &str, id: &PointId) -> Result<Value, Error> {
let url = format!(
"{}/collections/{}/points/{}",
self.url_base, collection_name, id,
Expand Down

0 comments on commit dede426

Please sign in to comment.