Skip to content

Commit 379797c

Browse files
committed
chore: Handle all BasicValue types in search(), doc updates
Signed-off-by: Anush008 <[email protected]>
1 parent 8125f53 commit 379797c

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

docs/docs/ops/storages.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: CocoIndex Built-in Storages
77

88
## Postgres
99

10-
`Postgres` exports data to Postgres database (with pgvector extension).
10+
Exports data to Postgres database (with pgvector extension).
1111

1212
The spec takes the following fields:
1313

@@ -17,10 +17,10 @@ The spec takes the following fields:
1717

1818
## Qdrant
1919

20-
`Qdrant` exports data to a [Qdrant](https://qdrant.tech/) collection.
20+
Exports data to a [Qdrant](https://qdrant.tech/) collection.
2121

2222
The spec takes the following fields:
2323

24-
* `qdrant_url` (type: `str`, required): The [gRPC URL](https://qdrant.tech/documentation/interfaces/#grpc-interface) of the Qdrant instance. Defaults to http://localhost:6334/.
24+
* `qdrant_url` (type: `str`, required): The [gRPC URL](https://qdrant.tech/documentation/interfaces/#grpc-interface) of the Qdrant instance. Defaults to `http://localhost:6334/`.
2525

2626
* `collection` (type: `str`, required): The name of the collection to export the data to.

python/cocoindex/storages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Qdrant(op.StorageSpec):
1616
"""Storage powered by Qdrant - https://qdrant.tech/."""
1717

1818
collection_name: str
19+
grpc_url: str = "http://localhost:6334/"
1920

2021
@dataclass
2122
class Neo4jConnectionSpec:

src/base/value.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ impl std::fmt::Display for KeyValue {
174174
}
175175

176176
impl KeyValue {
177-
pub fn fields_iter<'a>(
178-
&'a self,
179-
num_fields: usize,
180-
) -> Result<impl Iterator<Item = &'a KeyValue>> {
177+
pub fn fields_iter(&self, num_fields: usize) -> Result<impl Iterator<Item = &KeyValue>> {
181178
let slice = if num_fields == 1 {
182179
std::slice::from_ref(self)
183180
} else {

src/ops/storages/qdrant.rs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fn key_value_fields_iter<'a>(
3636
#[derive(Debug, Deserialize, Clone)]
3737
pub struct Spec {
3838
collection_name: String,
39+
grpc_url: String,
3940
}
4041

4142
pub struct Executor {
@@ -193,29 +194,53 @@ fn into_value(point: &ScoredPoint, schema: &FieldSchema) -> Result<Value> {
193194
.get(field_name)
194195
.map(|v| BasicValue::Json(Arc::from(v.clone().into_json()))),
195196

196-
BasicValueType::Vector(_) => {
197-
let vectors_options = point.vectors.clone().unwrap().vectors_options.unwrap();
198-
199-
match vectors_options {
197+
BasicValueType::Vector(_) => point
198+
.vectors
199+
.as_ref()
200+
.and_then(|v| v.vectors_options.as_ref())
201+
.and_then(|vectors_options| match vectors_options {
200202
VectorsOptions::Vector(vector) => {
201-
let x = vector
203+
let values = vector
202204
.data
203-
.into_iter()
204-
.map(BasicValue::Float32)
205+
.iter()
206+
.map(|f| BasicValue::Float32(*f))
205207
.collect::<Vec<_>>();
206-
Some(BasicValue::Vector(Arc::from(x)))
208+
Some(BasicValue::Vector(Arc::from(values)))
207209
}
208210
VectorsOptions::Vectors(vectors) => {
209-
let vector = vectors.vectors[field_name].clone();
210-
let x = vector
211-
.data
212-
.into_iter()
213-
.map(BasicValue::Float32)
214-
.collect::<Vec<_>>();
215-
Some(BasicValue::Vector(Arc::from(x)))
211+
vectors.vectors.get(field_name).map(|vector| {
212+
let values = vector
213+
.data
214+
.iter()
215+
.map(|f| BasicValue::Float32(*f))
216+
.collect::<Vec<_>>();
217+
BasicValue::Vector(Arc::from(values))
218+
})
216219
}
217-
}
218-
}
220+
}),
221+
222+
BasicValueType::Date
223+
| BasicValueType::LocalDateTime
224+
| BasicValueType::OffsetDateTime
225+
| BasicValueType::Time
226+
| BasicValueType::Uuid => point.payload.get(field_name).and_then(|v| {
227+
v.as_str()
228+
.map(|s| BasicValue::Str(Arc::from(s.to_string())))
229+
}),
230+
BasicValueType::Range => point.payload.get(field_name).and_then(|v| {
231+
v.as_struct().and_then(|s| {
232+
let start = s.fields.get("start").and_then(|f| f.as_integer());
233+
let end = s.fields.get("end").and_then(|f| f.as_integer());
234+
235+
match (start, end) {
236+
(Some(start), Some(end)) => Some(BasicValue::Range(RangeValue {
237+
start: start as usize,
238+
end: end as usize,
239+
})),
240+
_ => None,
241+
}
242+
})
243+
}),
219244
_ => {
220245
anyhow::bail!("Unsupported value type")
221246
}
@@ -243,7 +268,8 @@ impl QueryTarget for Executor {
243268
.query(Query::new_nearest(query.vector))
244269
.limit(query.limit as u64)
245270
.using(query.vector_field_name)
246-
.with_payload(true),
271+
.with_payload(true)
272+
.with_vectors(true),
247273
)
248274
.await?
249275
.result;
@@ -300,21 +326,18 @@ impl StorageFactoryBase for Arc<Factory> {
300326
_storage_options: IndexOptions,
301327
_context: Arc<FlowInstanceContext>,
302328
) -> Result<ExportTargetBuildOutput<Self>> {
303-
// TODO(Anush008): Add as a field to the Spec
304-
305329
if key_fields_schema.len() > 1 {
306330
api_bail!(
307331
"Expected only one primary key for the point ID. Got {}.",
308332
key_fields_schema.len()
309333
)
310334
}
311335

312-
let url = "http://localhost:6334/";
313336
let collection_name = spec.collection_name.clone();
314337

315338
let executors = async move {
316339
let executor = Arc::new(Executor::new(
317-
url.to_string(),
340+
spec.grpc_url,
318341
spec.collection_name.clone(),
319342
key_fields_schema,
320343
value_fields_schema,

0 commit comments

Comments
 (0)