Get By Predicate
Retrieve one or more stored vectors and their associated metadata from a store by applying a predicate filter. Unlike Get Key, which retrieves a single item by its unique key, Get by Predicate allows querying based on conditions defined on metadata fields (for example, "all items where category = book"). This is useful for flexible filtering, targeted queries, or conditional retrieval.
Source Code Exampleβ
Click to expand
use ahnlich_client_rs::db::DbClient;
use ahnlich_types::{
db::query::GetPred,
metadata::{MetadataValue, metadata_value::Value},
predicates::{
Predicate, PredicateCondition,
predicate::Kind as PredicateKind,
predicate_condition::Kind as PredicateConditionKind,
Equals,
},
};
use tokio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "http://127.0.0.1:1369";
let client = DbClient::new(addr.to_string()).await?;
let condition = PredicateCondition {
kind: Some(PredicateConditionKind::Value(Predicate {
kind: Some(PredicateKind::Equals(Equals {
key: "role".into(),
value: Some(MetadataValue {
value: Some(Value::RawString("admin".into())),
}),
})),
})),
};
let get_pred_params = GetPred {
store: "Main".to_string(),
condition: Some(condition),
};
let result = client.get_pred(get_pred_params, None).await?;
println!("Fetched rows: {:#?}", result);
Ok(())
}
Parametersβ
-
params: GetPredβ Contains the store name and predicate condition used to filter results. -
tracing_id: Option<String>β Optional trace context for observability, attached to the request if provided.
Returnsβ
-
Getβ Response with all matched vectors and metadata. -
AhnlichErrorβ Error if the store is missing, the predicate is invalid, or the server cannot complete the request.
Behaviorβ
-
Builds a gRPC request from the given
GetPredparameters. -
Attaches optional tracing metadata using
add_trace_parent. -
Calls the
get_predRPC on the server through the cloned client. -
Waits asynchronously for the serverβs response and extracts the inner
Getpayload with.into_inner().