diff --git a/src/client.rs b/src/client.rs index 90ba1cc..a3a84f9 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,39 +1,32 @@ use crate::channel_pool::ChannelPool; use crate::qdrant::alias_operations::Action; use crate::qdrant::collections_client::CollectionsClient; -use crate::qdrant::point_id::PointIdOptions; use crate::qdrant::points_client::PointsClient; -use crate::qdrant::points_selector::PointsSelectorOneOf; use crate::qdrant::snapshots_client::SnapshotsClient; use crate::qdrant::update_collection_cluster_setup_request::Operation; -use crate::qdrant::value::Kind; -use crate::qdrant::vectors::VectorsOptions; -use crate::qdrant::with_payload_selector::SelectorOptions; use crate::qdrant::{ - qdrant_client, shard_key, with_vectors_selector, AliasOperations, ChangeAliases, - ClearPayloadPoints, CollectionClusterInfoRequest, CollectionClusterInfoResponse, - CollectionExistsRequest, CollectionOperationResponse, CollectionParamsDiff, CountPoints, - CountResponse, CreateAlias, CreateCollection, CreateFieldIndexCollection, - CreateFullSnapshotRequest, CreateShardKey, CreateShardKeyRequest, CreateShardKeyResponse, - CreateSnapshotRequest, CreateSnapshotResponse, DeleteAlias, DeleteCollection, - DeleteFieldIndexCollection, DeleteFullSnapshotRequest, DeletePayloadPoints, DeletePointVectors, - DeletePoints, DeleteShardKey, DeleteShardKeyRequest, DeleteShardKeyResponse, - DeleteSnapshotRequest, DeleteSnapshotResponse, DiscoverBatchPoints, DiscoverBatchResponse, - DiscoverPoints, DiscoverResponse, FieldType, GetCollectionInfoRequest, + qdrant_client, shard_key, AliasOperations, ChangeAliases, ClearPayloadPoints, + CollectionClusterInfoRequest, CollectionClusterInfoResponse, CollectionExistsRequest, + CollectionOperationResponse, CollectionParamsDiff, CountPoints, CountResponse, CreateAlias, + CreateCollection, CreateFieldIndexCollection, CreateFullSnapshotRequest, CreateShardKey, + CreateShardKeyRequest, CreateShardKeyResponse, CreateSnapshotRequest, CreateSnapshotResponse, + DeleteAlias, DeleteCollection, DeleteFieldIndexCollection, DeleteFullSnapshotRequest, + DeletePayloadPoints, DeletePointVectors, DeletePoints, DeleteShardKey, DeleteShardKeyRequest, + DeleteShardKeyResponse, DeleteSnapshotRequest, DeleteSnapshotResponse, DiscoverBatchPoints, + DiscoverBatchResponse, DiscoverPoints, DiscoverResponse, FieldType, GetCollectionInfoRequest, GetCollectionInfoResponse, GetPoints, GetResponse, HealthCheckReply, HealthCheckRequest, HnswConfigDiff, ListAliasesRequest, ListAliasesResponse, ListCollectionAliasesRequest, ListCollectionsRequest, ListCollectionsResponse, ListFullSnapshotsRequest, - ListSnapshotsRequest, ListSnapshotsResponse, ListValue, NamedVectors, OptimizersConfigDiff, - PayloadIncludeSelector, PayloadIndexParams, PointId, PointStruct, PointVectors, PointsIdsList, - PointsOperationResponse, PointsSelector, PointsUpdateOperation, QuantizationConfigDiff, - ReadConsistency, RecommendBatchPoints, RecommendBatchResponse, RecommendGroupsResponse, - RecommendPointGroups, RecommendPoints, RecommendResponse, RenameAlias, ScrollPoints, - ScrollResponse, SearchBatchPoints, SearchBatchResponse, SearchGroupsResponse, + ListSnapshotsRequest, ListSnapshotsResponse, OptimizersConfigDiff, PayloadIndexParams, PointId, + PointStruct, PointVectors, PointsOperationResponse, PointsSelector, PointsUpdateOperation, + QuantizationConfigDiff, ReadConsistency, RecommendBatchPoints, RecommendBatchResponse, + RecommendGroupsResponse, RecommendPointGroups, RecommendPoints, RecommendResponse, RenameAlias, + ScrollPoints, ScrollResponse, SearchBatchPoints, SearchBatchResponse, SearchGroupsResponse, SearchPointGroups, SearchPoints, SearchResponse, SetPayloadPoints, ShardKey, ShardKeySelector, - SparseIndices, SparseVectorConfig, Struct, UpdateBatchPoints, UpdateBatchResponse, - UpdateCollection, UpdateCollectionClusterSetupRequest, UpdateCollectionClusterSetupResponse, - UpdatePointVectors, UpsertPoints, Value, Vector, Vectors, VectorsConfigDiff, VectorsSelector, - WithPayloadSelector, WithVectorsSelector, WriteOrdering, + SparseVectorConfig, UpdateBatchPoints, UpdateBatchResponse, UpdateCollection, + UpdateCollectionClusterSetupRequest, UpdateCollectionClusterSetupResponse, UpdatePointVectors, + UpsertPoints, Value, VectorsConfigDiff, VectorsSelector, WithPayloadSelector, + WithVectorsSelector, WriteOrdering, }; use anyhow::Result; #[cfg(feature = "serde")] @@ -52,143 +45,6 @@ pub use crate::config::{AsTimeout, CompressionEncoding, MaybeApiKey, QdrantClien /// A builder type for `QdrantClient`s pub type QdrantClientBuilder = QdrantClientConfig; -impl From for WithPayloadSelector { - fn from(flag: bool) -> Self { - WithPayloadSelector { - selector_options: Some(SelectorOptions::Enable(flag)), - } - } -} - -impl From> for WithPayloadSelector { - fn from(fields: Vec<&str>) -> Self { - WithPayloadSelector { - selector_options: Some(SelectorOptions::Include(PayloadIncludeSelector { - fields: fields.into_iter().map(|f| f.to_string()).collect(), - })), - } - } -} - -impl From> for PointsSelector { - fn from(point_ids: Vec) -> Self { - PointsSelector { - points_selector_one_of: Some(PointsSelectorOneOf::Points(PointsIdsList { - ids: point_ids, - })), - } - } -} - -impl From> for Vector { - fn from(vector: Vec) -> Self { - Vector { - data: vector, - indices: None, - } - } -} - -impl From> for Vector { - fn from(tuples: Vec<(u32, f32)>) -> Self { - Self::from(tuples.as_slice()) - } -} - -// Since we construct two new Vec's anyway it's fine to source from a reference -impl From<&[(u32, f32)]> for Vector { - fn from(tuples: &[(u32, f32)]) -> Self { - let mut indices = Vec::with_capacity(tuples.len()); - let mut values = Vec::with_capacity(tuples.len()); - for (i, w) in tuples { - indices.push(*i); - values.push(*w); - } - Vector { - data: values, - indices: Some(SparseIndices { data: indices }), - } - } -} - -impl From>> for Vectors { - fn from(named_vectors: HashMap>) -> Self { - Vectors { - vectors_options: Some(VectorsOptions::Vectors(NamedVectors { - vectors: named_vectors - .into_iter() - .map(|(k, v)| (k, v.into())) - .collect(), - })), - } - } -} - -impl From> for Vectors { - fn from(named_vectors: HashMap) -> Self { - Vectors { - vectors_options: Some(VectorsOptions::Vectors(NamedVectors { - vectors: named_vectors.into_iter().collect(), - })), - } - } -} - -impl From>> for Vectors { - fn from(named_vectors: HashMap>) -> Self { - Vectors { - vectors_options: Some(VectorsOptions::Vectors(NamedVectors { - vectors: named_vectors - .into_iter() - .map(|(k, v)| (k, v.into())) - .collect(), - })), - } - } -} - -// Since we construct two new Vec's anyway it's fine to source from a reference -impl From> for Vectors { - fn from(named_vectors: HashMap) -> Self { - Vectors { - vectors_options: Some(VectorsOptions::Vectors(NamedVectors { - vectors: named_vectors - .into_iter() - .map(|(k, v)| (k, v.into())) - .collect(), - })), - } - } -} - -impl From> for Vectors { - fn from(vector: Vec) -> Self { - Vectors { - vectors_options: Some(VectorsOptions::Vector(vector.into())), - } - } -} - -impl From> for WithVectorsSelector { - fn from(names: Vec<&str>) -> Self { - WithVectorsSelector { - selector_options: Some(with_vectors_selector::SelectorOptions::Include( - VectorsSelector { - names: names.into_iter().map(|name| name.to_string()).collect(), - }, - )), - } - } -} - -impl From for WithVectorsSelector { - fn from(flag: bool) -> Self { - WithVectorsSelector { - selector_options: Some(with_vectors_selector::SelectorOptions::Enable(flag)), - } - } -} - pub struct TokenInterceptor { api_key: Option, } @@ -1792,35 +1648,9 @@ impl QdrantClient { } } -impl PointStruct { - pub fn new(id: impl Into, vectors: impl Into, payload: Payload) -> Self { - Self { - id: Some(id.into()), - payload: payload.into(), - vectors: Some(vectors.into()), - } - } -} - -impl From for PointId { - fn from(val: String) -> Self { - Self { - point_id_options: Some(PointIdOptions::Uuid(val)), - } - } -} - -impl From for PointId { - fn from(val: u64) -> Self { - Self { - point_id_options: Some(PointIdOptions::Num(val)), - } - } -} - #[derive(Clone, PartialEq, Debug, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Payload(HashMap); +pub struct Payload(pub(crate) HashMap); impl From for HashMap { #[inline] @@ -1854,130 +1684,3 @@ impl Payload { self.0.insert(key.to_string(), val.into()); } } - -impl From for Value { - fn from(val: f64) -> Self { - Self { - kind: Some(Kind::DoubleValue(val)), - } - } -} - -impl From for Value { - fn from(val: i64) -> Self { - Self { - kind: Some(Kind::IntegerValue(val)), - } - } -} - -impl From for Value { - fn from(val: bool) -> Self { - Self { - kind: Some(Kind::BoolValue(val)), - } - } -} - -impl From for Value { - fn from(val: String) -> Self { - Self { - kind: Some(Kind::StringValue(val)), - } - } -} - -impl From<&str> for Value { - fn from(val: &str) -> Self { - Self { - kind: Some(Kind::StringValue(val.into())), - } - } -} - -impl From for Value { - fn from(val: Payload) -> Self { - Self { - kind: Some(Kind::StructValue(Struct { fields: val.0 })), - } - } -} - -impl From> for Value -where - T: Into, -{ - fn from(val: Vec) -> Self { - Self { - kind: Some(Kind::ListValue(ListValue { - values: val.into_iter().map(|v| v.into()).collect(), - })), - } - } -} - -impl From> for Value -where - T: Into, -{ - fn from(val: Vec<(&str, T)>) -> Self { - Self { - kind: Some(Kind::StructValue(Struct { - fields: val - .into_iter() - .map(|(k, v)| (k.to_string(), v.into())) - .collect(), - })), - } - } -} - -impl From for ShardKey { - fn from(key: shard_key::Key) -> Self { - ShardKey { key: Some(key) } - } -} - -impl From> for ShardKeySelector { - fn from(shard_keys: Vec) -> Self { - ShardKeySelector { - shard_keys: shard_keys.into_iter().map(ShardKey::from).collect(), - } - } -} - -impl From for shard_key::Key { - fn from(keyword: String) -> Self { - shard_key::Key::Keyword(keyword) - } -} - -impl From for shard_key::Key { - fn from(number: u64) -> Self { - shard_key::Key::Number(number) - } -} - -impl From for ShardKeySelector { - fn from(keyword: String) -> Self { - keyword.into() - } -} - -impl From for ShardKeySelector { - fn from(number: u64) -> Self { - number.into() - } -} - -impl From> for ShardKeySelector { - fn from(keywords: Vec) -> Self { - keywords.into() - } -} - -impl From> for ShardKeySelector { - fn from(numbers: Vec) -> Self { - numbers.into() - } -} diff --git a/src/grpc_ext.rs b/src/grpc_ext.rs new file mode 100644 index 0000000..78c06fd --- /dev/null +++ b/src/grpc_ext.rs @@ -0,0 +1,302 @@ +use crate::client::Payload; +use crate::qdrant::point_id::PointIdOptions; +use crate::qdrant::points_selector::PointsSelectorOneOf; +use crate::qdrant::value::Kind; +use crate::qdrant::vectors::VectorsOptions; +use crate::qdrant::with_payload_selector::SelectorOptions; +use crate::qdrant::{ + shard_key, with_vectors_selector, ListValue, NamedVectors, PayloadIncludeSelector, PointId, + PointStruct, PointsIdsList, PointsSelector, ShardKey, ShardKeySelector, SparseIndices, Struct, + Value, Vector, Vectors, VectorsSelector, WithPayloadSelector, WithVectorsSelector, +}; +use std::collections::HashMap; + +impl From for WithPayloadSelector { + fn from(flag: bool) -> Self { + WithPayloadSelector { + selector_options: Some(SelectorOptions::Enable(flag)), + } + } +} + +impl From> for WithPayloadSelector { + fn from(fields: Vec<&str>) -> Self { + WithPayloadSelector { + selector_options: Some(SelectorOptions::Include(PayloadIncludeSelector { + fields: fields.into_iter().map(|f| f.to_string()).collect(), + })), + } + } +} + +impl From> for PointsSelector { + fn from(point_ids: Vec) -> Self { + PointsSelector { + points_selector_one_of: Some(PointsSelectorOneOf::Points(PointsIdsList { + ids: point_ids, + })), + } + } +} + +impl From> for Vector { + fn from(vector: Vec) -> Self { + Vector { + data: vector, + indices: None, + } + } +} + +impl From> for Vector { + fn from(tuples: Vec<(u32, f32)>) -> Self { + Self::from(tuples.as_slice()) + } +} + +// Since we construct two new Vec's anyway it's fine to source from a reference +impl From<&[(u32, f32)]> for Vector { + fn from(tuples: &[(u32, f32)]) -> Self { + let mut indices = Vec::with_capacity(tuples.len()); + let mut values = Vec::with_capacity(tuples.len()); + for (i, w) in tuples { + indices.push(*i); + values.push(*w); + } + Vector { + data: values, + indices: Some(SparseIndices { data: indices }), + } + } +} + +impl From>> for Vectors { + fn from(named_vectors: HashMap>) -> Self { + Vectors { + vectors_options: Some(VectorsOptions::Vectors(NamedVectors { + vectors: named_vectors + .into_iter() + .map(|(k, v)| (k, v.into())) + .collect(), + })), + } + } +} + +impl From> for Vectors { + fn from(named_vectors: HashMap) -> Self { + Vectors { + vectors_options: Some(VectorsOptions::Vectors(NamedVectors { + vectors: named_vectors.into_iter().collect(), + })), + } + } +} + +impl From>> for Vectors { + fn from(named_vectors: HashMap>) -> Self { + Vectors { + vectors_options: Some(VectorsOptions::Vectors(NamedVectors { + vectors: named_vectors + .into_iter() + .map(|(k, v)| (k, v.into())) + .collect(), + })), + } + } +} + +// Since we construct two new Vec's anyway it's fine to source from a reference +impl From> for Vectors { + fn from(named_vectors: HashMap) -> Self { + Vectors { + vectors_options: Some(VectorsOptions::Vectors(NamedVectors { + vectors: named_vectors + .into_iter() + .map(|(k, v)| (k, v.into())) + .collect(), + })), + } + } +} + +impl From> for Vectors { + fn from(vector: Vec) -> Self { + Vectors { + vectors_options: Some(VectorsOptions::Vector(vector.into())), + } + } +} + +impl From> for WithVectorsSelector { + fn from(names: Vec<&str>) -> Self { + WithVectorsSelector { + selector_options: Some(with_vectors_selector::SelectorOptions::Include( + VectorsSelector { + names: names.into_iter().map(|name| name.to_string()).collect(), + }, + )), + } + } +} + +impl From for WithVectorsSelector { + fn from(flag: bool) -> Self { + WithVectorsSelector { + selector_options: Some(with_vectors_selector::SelectorOptions::Enable(flag)), + } + } +} + +impl From for Value { + fn from(val: f64) -> Self { + Self { + kind: Some(Kind::DoubleValue(val)), + } + } +} + +impl From for Value { + fn from(val: i64) -> Self { + Self { + kind: Some(Kind::IntegerValue(val)), + } + } +} + +impl From for Value { + fn from(val: bool) -> Self { + Self { + kind: Some(Kind::BoolValue(val)), + } + } +} + +impl From for Value { + fn from(val: String) -> Self { + Self { + kind: Some(Kind::StringValue(val)), + } + } +} + +impl From<&str> for Value { + fn from(val: &str) -> Self { + Self { + kind: Some(Kind::StringValue(val.into())), + } + } +} + +impl From for Value { + fn from(val: Payload) -> Self { + Self { + kind: Some(Kind::StructValue(Struct { fields: val.0 })), + } + } +} + +impl From> for Value +where + T: Into, +{ + fn from(val: Vec) -> Self { + Self { + kind: Some(Kind::ListValue(ListValue { + values: val.into_iter().map(|v| v.into()).collect(), + })), + } + } +} + +impl From> for Value +where + T: Into, +{ + fn from(val: Vec<(&str, T)>) -> Self { + Self { + kind: Some(Kind::StructValue(Struct { + fields: val + .into_iter() + .map(|(k, v)| (k.to_string(), v.into())) + .collect(), + })), + } + } +} + +impl From for ShardKey { + fn from(key: shard_key::Key) -> Self { + ShardKey { key: Some(key) } + } +} + +impl From> for ShardKeySelector { + fn from(shard_keys: Vec) -> Self { + ShardKeySelector { + shard_keys: shard_keys.into_iter().map(ShardKey::from).collect(), + } + } +} + +impl From for shard_key::Key { + fn from(keyword: String) -> Self { + shard_key::Key::Keyword(keyword) + } +} + +impl From for shard_key::Key { + fn from(number: u64) -> Self { + shard_key::Key::Number(number) + } +} + +impl From for ShardKeySelector { + fn from(keyword: String) -> Self { + keyword.into() + } +} + +impl From for ShardKeySelector { + fn from(number: u64) -> Self { + number.into() + } +} + +impl From> for ShardKeySelector { + fn from(keywords: Vec) -> Self { + keywords.into() + } +} + +impl From> for ShardKeySelector { + fn from(numbers: Vec) -> Self { + numbers.into() + } +} + +impl From for PointId { + fn from(val: String) -> Self { + Self { + point_id_options: Some(PointIdOptions::Uuid(val)), + } + } +} + +impl From for PointId { + fn from(val: u64) -> Self { + Self { + point_id_options: Some(PointIdOptions::Num(val)), + } + } +} + +impl PointStruct { + pub fn new(id: impl Into, vectors: impl Into, payload: Payload) -> Self { + Self { + id: Some(id.into()), + payload: payload.into(), + vectors: Some(vectors.into()), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 88791fe..6c16e9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,6 +111,7 @@ pub mod prelude; pub mod qdrant; pub mod config; pub mod filters; +pub mod grpc_ext; #[cfg(feature = "serde")] pub mod serde;