Set
The Set request inserts or updates a vector in a given store. Each vector is stored alongside optional metadata and a unique key. If a key already exists in the store, calling Set with the same key will overwrite the existing vector and metadata.
Source Code Exampleβ
Click to expand
use ahnlich_client_rs::db::DbClient;
use ahnlich_types::{
db::query::Set,
keyval::{DbStoreEntry, StoreKey, StoreValue},
metadata::{MetadataValue, metadata_value::Value},
};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to DB server
let db_client = DbClient::new("127.0.0.1:1369".to_string()).await?;
let tracing_id: Option<String> = None;
// Construct inputs for the "set"
let inputs = vec![DbStoreEntry {
key: Some(StoreKey {
key: vec![0.5, 0.2, 0.9], // must match store dimension
}),
value: Some(StoreValue {
value: HashMap::from_iter([(
"role".into(),
MetadataValue {
value: Some(Value::RawString("admin".into())),
},
)]),
}),
}];
let params = Set {
store: "Main".to_string(), // store must already exist
inputs,
};
// Call set
match db_client.set(params, tracing_id).await {
Ok(result) => {
println!("Set operation result: {:?}", result);
}
Err(err) => {
eprintln!("Error inserting vector: {:?}", err);
}
}
Ok(())
}
Parametersβ
-
params: Setβ Defines the data to be stored. This includes:-
storeβ The name of the target store. -
keyβ A unique identifier for the vector. -
vectorβ The vector data (asVec<f32>). -
metadataβ Optional keyβvalue pairs to annotate the vector (e.g., labels, categories).
-
-
tracing_id: Option<String>β An optional tracing identifier for distributed observability.
Returnsβ
-
Ok(SetResult)β Contains information about the completed operation, such as success status and assigned identifiers. -
Err(AhnlichError)β Returned if the operation fails. Common error cases include:-
Target store does not exist.
-
Provided vector does not match the dimensionality of the store.
-
Invalid key or malformed metadata.
-
Transport or server-side errors.
-
Behaviorβ
-
Setis an upsert operation: it will insert if the key does not exist, or update if it already exists. -
Vectors written with
Setare immediately available for similarity searches (e.g.,Get Sim N) or retrieval by key. -
Metadata can be leveraged in predicate-based queries if a predicate index is defined.
-
Ensure consistent key usage to avoid unintended overwrites.