Skip to content

Commit 2d74302

Browse files
committed
feat(kg): rename spec type names
1 parent a456d0b commit 2d74302

File tree

4 files changed

+68
-69
lines changed

4 files changed

+68
-69
lines changed

examples/docs_to_knowledge_graph/main.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,31 @@ def docs_to_kg_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.D
9393
"document_node",
9494
cocoindex.storages.Neo4j(
9595
connection=conn_spec,
96-
mapping=cocoindex.storages.NodeMapping(label="Document")),
96+
mapping=cocoindex.storages.Nodes(label="Document")),
9797
primary_key_fields=["filename"],
9898
)
9999
# Declare reference Node to reference entity node in a relationship
100100
flow_builder.declare(
101-
cocoindex.storages.Neo4jDeclarations(
101+
cocoindex.storages.Neo4jDeclaration(
102102
connection=conn_spec,
103-
referenced_nodes=[
104-
cocoindex.storages.ReferencedNode(
105-
label="Entity",
106-
primary_key_fields=["value"],
107-
)
108-
]
103+
nodes_label="Entity",
104+
primary_key_fields=["value"],
109105
)
110106
)
111107
entity_relationship.export(
112108
"entity_relationship",
113109
cocoindex.storages.Neo4j(
114110
connection=conn_spec,
115-
mapping=cocoindex.storages.RelationshipMapping(
111+
mapping=cocoindex.storages.Relationships(
116112
rel_type="RELATIONSHIP",
117-
source=cocoindex.storages.NodeReferenceMapping(
113+
source=cocoindex.storages.NodeFromFields(
118114
label="Entity",
119115
fields=[
120116
cocoindex.storages.TargetFieldMapping(
121117
source="subject", target="value"),
122118
]
123119
),
124-
target=cocoindex.storages.NodeReferenceMapping(
120+
target=cocoindex.storages.NodeFromFields(
125121
label="Entity",
126122
fields=[
127123
cocoindex.storages.TargetFieldMapping(
@@ -136,13 +132,13 @@ def docs_to_kg_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.D
136132
"entity_mention",
137133
cocoindex.storages.Neo4j(
138134
connection=conn_spec,
139-
mapping=cocoindex.storages.RelationshipMapping(
135+
mapping=cocoindex.storages.Relationships(
140136
rel_type="MENTION",
141-
source=cocoindex.storages.NodeReferenceMapping(
137+
source=cocoindex.storages.NodeFromFields(
142138
label="Document",
143139
fields=[cocoindex.storages.TargetFieldMapping("filename")],
144140
),
145-
target=cocoindex.storages.NodeReferenceMapping(
141+
target=cocoindex.storages.NodeFromFields(
146142
label="Entity",
147143
fields=[cocoindex.storages.TargetFieldMapping(
148144
source="entity", target="value")],

python/cocoindex/storages.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TargetFieldMapping:
3737
target: str | None = None
3838

3939
@dataclass
40-
class NodeReferenceMapping:
40+
class NodeFromFields:
4141
"""Spec for a referenced graph node, usually as part of a relationship."""
4242
label: str
4343
fields: list[TargetFieldMapping]
@@ -50,30 +50,37 @@ class ReferencedNode:
5050
vector_indexes: Sequence[index.VectorIndexDef] = ()
5151

5252
@dataclass
53-
class NodeMapping:
53+
class Nodes:
5454
"""Spec to map a row to a graph node."""
5555
kind = "Node"
5656

5757
label: str
5858

5959
@dataclass
60-
class RelationshipMapping:
60+
class Relationships:
6161
"""Spec to map a row to a graph relationship."""
6262
kind = "Relationship"
6363

6464
rel_type: str
65-
source: NodeReferenceMapping
66-
target: NodeReferenceMapping
65+
source: NodeFromFields
66+
target: NodeFromFields
67+
68+
# For backwards compatibility only
69+
NodeMapping = Nodes
70+
RelationshipMapping = Relationships
71+
NodeReferenceMapping = NodeFromFields
6772

6873
class Neo4j(op.StorageSpec):
6974
"""Graph storage powered by Neo4j."""
7075

7176
connection: AuthEntryReference
72-
mapping: NodeMapping | RelationshipMapping
77+
mapping: Nodes | Relationships
7378

74-
class Neo4jDeclarations(op.DeclarationSpec):
79+
class Neo4jDeclaration(op.DeclarationSpec):
7580
"""Declarations for Neo4j."""
7681

7782
kind = "Neo4j"
7883
connection: AuthEntryReference
79-
referenced_nodes: Sequence[ReferencedNode] = ()
84+
nodes_label: str
85+
primary_key_fields: Sequence[str]
86+
vector_indexes: Sequence[index.VectorIndexDef] = ()

src/ops/storages/neo4j.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::prelude::*;
22

3-
use super::spec::{
4-
GraphDeclarations, GraphElementMapping, NodeReferenceMapping, TargetFieldMapping,
5-
};
3+
use super::spec::{GraphDeclaration, GraphElementMapping, NodeFromFieldsSpec, TargetFieldMapping};
64
use crate::setup::components::{self, State};
75
use crate::setup::{ResourceSetupStatusCheck, SetupChangeType};
86
use crate::{ops::sdk::*, setup::CombinedState};
@@ -32,7 +30,7 @@ pub struct Spec {
3230
pub struct Declaration {
3331
connection: spec::AuthEntryReference<ConnectionSpec>,
3432
#[serde(flatten)]
35-
decl: GraphDeclarations,
33+
decl: GraphDeclaration,
3634
}
3735

3836
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
@@ -994,7 +992,7 @@ struct DependentNodeLabelAnalyzer<'a> {
994992

995993
impl<'a> DependentNodeLabelAnalyzer<'a> {
996994
fn new(
997-
rel_end_spec: &'a NodeReferenceMapping,
995+
rel_end_spec: &'a NodeFromFieldsSpec,
998996
index_options_map: &'a HashMap<String, IndexOptions>,
999997
) -> Result<Self> {
1000998
Ok(Self {
@@ -1104,8 +1102,7 @@ impl StorageFactoryBase for Factory {
11041102
.chain(
11051103
declarations
11061104
.iter()
1107-
.flat_map(|d| d.decl.referenced_nodes.iter())
1108-
.map(|n| (n.label.clone(), n.index_options.clone())),
1105+
.map(|n| (n.decl.nodes_label.clone(), n.decl.index_options.clone())),
11091106
)
11101107
.collect::<HashMap<String, IndexOptions>>();
11111108
let mut label_value_field_types =
@@ -1212,37 +1209,41 @@ impl StorageFactoryBase for Factory {
12121209
.collect::<Result<Vec<_>>>()?;
12131210
let decl_output = declarations
12141211
.into_iter()
1215-
.flat_map(|d| {
1212+
.map(|d| {
12161213
let label_value_field_types = &label_value_field_types;
1217-
d.decl.referenced_nodes.into_iter().map(move |n| {
1218-
let setup_key = GraphElement {
1219-
connection: d.connection.clone(),
1220-
typ: ElementType::Node(n.label.clone()),
1221-
};
1222-
let primary_key_fields = n
1223-
.index_options
1224-
.primary_key_fields
1225-
.as_ref()
1214+
let setup_key = GraphElement {
1215+
connection: d.connection.clone(),
1216+
typ: ElementType::Node(d.decl.nodes_label.clone()),
1217+
};
1218+
let primary_key_fields = d
1219+
.decl
1220+
.index_options
1221+
.primary_key_fields
1222+
.as_ref()
1223+
.ok_or_else(|| {
1224+
api_error!(
1225+
"No primary key fields specified for node label `{}`",
1226+
&d.decl.nodes_label
1227+
)
1228+
})?
1229+
.iter()
1230+
.map(|f| f.clone())
1231+
.collect();
1232+
let setup_state = SetupState::new(
1233+
&setup_key.typ,
1234+
primary_key_fields,
1235+
&d.decl.index_options,
1236+
label_value_field_types
1237+
.get(&d.decl.nodes_label)
12261238
.ok_or_else(|| {
12271239
api_error!(
1228-
"No primary key fields specified for node label `{}`",
1229-
&n.label
1240+
"Data for nodes with label `{}` not provided",
1241+
d.decl.nodes_label
12301242
)
1231-
})?
1232-
.iter()
1233-
.map(|f| f.clone())
1234-
.collect();
1235-
let setup_state = SetupState::new(
1236-
&setup_key.typ,
1237-
primary_key_fields,
1238-
&n.index_options,
1239-
label_value_field_types.get(&n.label).ok_or_else(|| {
1240-
api_error!("Data for nodes with label `{}` not provided", n.label)
12411243
})?,
1242-
None,
1243-
)?;
1244-
Ok((setup_key, setup_state))
1245-
})
1244+
None,
1245+
)?;
1246+
Ok((setup_key, setup_state))
12461247
})
12471248
.collect::<Result<Vec<_>>>()?;
12481249
Ok((data_coll_output, decl_output))

src/ops/storages/spec.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,34 @@ impl TargetFieldMapping {
1717
}
1818

1919
#[derive(Debug, Deserialize)]
20-
pub struct NodeReferenceMapping {
20+
pub struct NodeFromFieldsSpec {
2121
pub label: String,
2222
pub fields: Vec<TargetFieldMapping>,
2323
}
2424

2525
#[derive(Debug, Deserialize)]
26-
pub struct NodeMapping {
26+
pub struct NodesSpec {
2727
pub label: String,
2828
}
2929

3030
#[derive(Debug, Deserialize)]
31-
pub struct RelationshipMapping {
31+
pub struct RelationshipsSpec {
3232
pub rel_type: String,
33-
pub source: NodeReferenceMapping,
34-
pub target: NodeReferenceMapping,
33+
pub source: NodeFromFieldsSpec,
34+
pub target: NodeFromFieldsSpec,
3535
}
3636

3737
#[derive(Debug, Deserialize)]
3838
#[serde(tag = "kind")]
3939
pub enum GraphElementMapping {
40-
Relationship(RelationshipMapping),
41-
Node(NodeMapping),
40+
Relationship(RelationshipsSpec),
41+
Node(NodesSpec),
4242
}
4343

4444
#[derive(Debug, Deserialize)]
45-
pub struct ReferencedNodeSpec {
46-
pub label: String,
45+
pub struct GraphDeclaration {
46+
pub nodes_label: String,
4747

4848
#[serde(flatten)]
4949
pub index_options: spec::IndexOptions,
5050
}
51-
52-
#[derive(Debug, Deserialize)]
53-
pub struct GraphDeclarations {
54-
pub referenced_nodes: Vec<ReferencedNodeSpec>,
55-
}

0 commit comments

Comments
 (0)