Skip to content

Commit 503d622

Browse files
committed
addressed the review comments
1 parent 12e9d3c commit 503d622

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

docs/docs/core/flow_def.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ Types of the fields must be key types. See [Key Types](data_types#key-types) for
313313

314314
* `field_name`: the field to create vector index.
315315
* `metric`: the similarity metric to use.
316-
* `method`: the index algorithm and optional tuning parameters. Defaults to HNSW. Use `cocoindex.HnswVectorIndexMethod()` or `cocoindex.IvfFlatVectorIndexMethod()` to customize the method and its parameters.
316+
* `method` (optional): the index algorithm and optional tuning parameters. Leave unset to use the target default (HNSW for Postgres). Use `cocoindex.HnswVectorIndexMethod()` or `cocoindex.IvfFlatVectorIndexMethod()` to customize the method and its parameters.
317317

318318
#### Similarity Metrics
319319

python/cocoindex/index.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from dataclasses import dataclass, field
2+
from dataclasses import dataclass
33
from typing import Sequence, Union
44

55

@@ -13,7 +13,7 @@ class VectorSimilarityMetric(Enum):
1313
class HnswVectorIndexMethod:
1414
"""HNSW vector index parameters."""
1515

16-
type: str = field(init=False, default="hnsw")
16+
kind: str = "Hnsw"
1717
m: int | None = None
1818
ef_construction: int | None = None
1919

@@ -22,7 +22,7 @@ class HnswVectorIndexMethod:
2222
class IvfFlatVectorIndexMethod:
2323
"""IVFFlat vector index parameters."""
2424

25-
type: str = field(init=False, default="ivfflat")
25+
kind: str = "IvfFlat"
2626
lists: int | None = None
2727

2828

@@ -37,7 +37,7 @@ class VectorIndexDef:
3737

3838
field_name: str
3939
metric: VectorSimilarityMetric
40-
method: VectorIndexMethod = field(default_factory=HnswVectorIndexMethod)
40+
method: VectorIndexMethod | None = None
4141

4242

4343
@dataclass

src/base/spec.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl fmt::Display for VectorSimilarityMetric {
385385
}
386386

387387
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
388-
#[serde(tag = "type", rename_all = "snake_case")]
388+
#[serde(tag = "kind")]
389389
pub enum VectorIndexMethod {
390390
Hnsw {
391391
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -399,20 +399,11 @@ pub enum VectorIndexMethod {
399399
},
400400
}
401401

402-
impl Default for VectorIndexMethod {
403-
fn default() -> Self {
404-
Self::Hnsw {
405-
m: None,
406-
ef_construction: None,
407-
}
408-
}
409-
}
410-
411402
impl VectorIndexMethod {
412403
pub fn kind(&self) -> &'static str {
413404
match self {
414-
Self::Hnsw { .. } => "hnsw",
415-
Self::IvfFlat { .. } => "ivfflat",
405+
Self::Hnsw { .. } => "Hnsw",
406+
Self::IvfFlat { .. } => "IvfFlat",
416407
}
417408
}
418409

@@ -439,16 +430,16 @@ impl fmt::Display for VectorIndexMethod {
439430
parts.push(format!("ef_construction={}", ef));
440431
}
441432
if parts.is_empty() {
442-
write!(f, "hnsw")
433+
write!(f, "Hnsw")
443434
} else {
444-
write!(f, "hnsw({})", parts.join(","))
435+
write!(f, "Hnsw({})", parts.join(","))
445436
}
446437
}
447438
Self::IvfFlat { lists } => {
448439
if let Some(lists) = lists {
449-
write!(f, "ivfflat(lists={lists})")
440+
write!(f, "IvfFlat(lists={lists})")
450441
} else {
451-
write!(f, "ivfflat")
442+
write!(f, "IvfFlat")
452443
}
453444
}
454445
}
@@ -459,16 +450,18 @@ impl fmt::Display for VectorIndexMethod {
459450
pub struct VectorIndexDef {
460451
pub field_name: FieldName,
461452
pub metric: VectorSimilarityMetric,
462-
#[serde(default)]
463-
pub method: VectorIndexMethod,
453+
#[serde(default, skip_serializing_if = "Option::is_none")]
454+
pub method: Option<VectorIndexMethod>,
464455
}
465456

466457
impl fmt::Display for VectorIndexDef {
467458
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
468-
if self.method.is_default() {
469-
write!(f, "{}:{}", self.field_name, self.metric)
470-
} else {
471-
write!(f, "{}:{}:{}", self.field_name, self.metric, self.method)
459+
match &self.method {
460+
None => write!(f, "{}:{}", self.field_name, self.metric),
461+
Some(method) if method.is_default() => {
462+
write!(f, "{}:{}", self.field_name, self.metric)
463+
}
464+
Some(method) => write!(f, "{}:{}:{}", self.field_name, self.metric, method),
472465
}
473466
}
474467
}

src/ops/targets/postgres.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -461,24 +461,24 @@ fn to_vector_similarity_metric_sql(metric: VectorSimilarityMetric) -> &'static s
461461
}
462462

463463
fn to_index_spec_sql(index_spec: &VectorIndexDef) -> Cow<'static, str> {
464-
let method = match &index_spec.method {
465-
spec::VectorIndexMethod::Hnsw { .. } => "hnsw",
466-
spec::VectorIndexMethod::IvfFlat { .. } => "ivfflat",
467-
};
468-
let options = match &index_spec.method {
469-
spec::VectorIndexMethod::Hnsw { m, ef_construction } => {
464+
let (method, options) = match index_spec.method.as_ref() {
465+
Some(spec::VectorIndexMethod::Hnsw { m, ef_construction }) => {
470466
let mut opts = Vec::new();
471467
if let Some(m) = m {
472468
opts.push(format!("m = {}", m));
473469
}
474470
if let Some(ef) = ef_construction {
475471
opts.push(format!("ef_construction = {}", ef));
476472
}
477-
opts
473+
("hnsw", opts)
478474
}
479-
spec::VectorIndexMethod::IvfFlat { lists } => lists
480-
.map(|lists| vec![format!("lists = {}", lists)])
481-
.unwrap_or_default(),
475+
Some(spec::VectorIndexMethod::IvfFlat { lists }) => (
476+
"ivfflat",
477+
lists
478+
.map(|lists| vec![format!("lists = {}", lists)])
479+
.unwrap_or_default(),
480+
),
481+
None => ("hnsw", Vec::new()),
482482
};
483483
let with_clause = if options.is_empty() {
484484
String::new()
@@ -501,9 +501,9 @@ fn to_vector_index_name(table_name: &str, vector_index_def: &spec::VectorIndexDe
501501
vector_index_def.field_name,
502502
to_vector_similarity_metric_sql(vector_index_def.metric)
503503
);
504-
if !vector_index_def.method.is_default() {
504+
if let Some(method) = vector_index_def.method.as_ref().filter(|m| !m.is_default()) {
505505
name.push_str("__");
506-
name.push_str(vector_index_def.method.kind());
506+
name.push_str(&method.kind().to_ascii_lowercase());
507507
}
508508
name
509509
}

0 commit comments

Comments
 (0)