You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Other arguments can be passed in as positional arguments or keyword arguments, aftert the function spec.
161
+
Other arguments can be passed in as positional arguments or keyword arguments, after the function spec.
168
162
169
163
<Tabs>
170
164
<TabItemvalue="python"label="Python"default>
@@ -300,6 +294,29 @@ CocoIndex provides a common way to configure indexes for various storages.
300
294
301
295
## Miscellaneous
302
296
297
+
### Target Declarations
298
+
299
+
Most time a target storage is created by calling `export()` method on a collector, and this `export()` call comes with configurations needed for the target storage, e.g. options for storage indexes.
300
+
Occasionally, you may need to specify some configurations for target storage out of the context of any specific data collector.
301
+
302
+
For example, for graph database targets like `Neo4j`, you may have a data collector to export data to Neo4j relationships, which will create nodes referenced by various relationships in turn.
303
+
These nodes don't directly come from any specific data collector (consider relationships from different data collectors may share the same nodes).
304
+
To specify configurations for these nodes, you can *declare* spec for related node labels.
305
+
306
+
`FlowBuilder` provides `declare()` method for this purpose, which takes the spec to declare, as provided by various target types.
307
+
308
+
<Tabs>
309
+
<TabItemvalue="python"label="Python"default>
310
+
311
+
```python
312
+
flow_builder.declare(
313
+
cocoindex.storages.Neo4jDeclarations(...)
314
+
)
315
+
```
316
+
317
+
</TabItem>
318
+
</Tabs>
319
+
303
320
### Auth Registry
304
321
305
322
CocoIndex manages an auth registry. It's an in-memory key-value store, mainly to store authentication information for a backend.
@@ -310,11 +327,10 @@ Operation spec is the default way to configure a backend. But it has the followi
310
327
* Once an operation is removed after flow definition code change, the spec is also gone.
311
328
But we still need to be able to drop the backend (e.g. a table) by `cocoindex setup` or `cocoindex drop`.
312
329
313
-
314
330
Auth registry is introduced to solve the problems above. It works as follows:
315
331
316
332
* You can create new **auth entry** by a key and a value.
317
-
* You can references the entry by the key, and pass it as part of spec for certain operations. e.g. `Neo4jRelationship` takes `connection` field in the form of auth entry reference.
333
+
* You can references the entry by the key, and pass it as part of spec for certain operations. e.g. `Neo4j` takes `connection` field in the form of auth entry reference.
For each target storage, data are exported from a data collector, containing data of multiple entries, each with multiple fields.
10
+
The way to map data from a data collector to a target storage depends on data model of the target storage.
11
+
12
+
## Entry-Oriented Targets
13
+
14
+
Entry-Oriented Storage organizes data into independent entries, such as rows, key-value pairs, or documents.
15
+
Each entry is self-contained and does not explicitly link to others.
16
+
There is usually a straightforward mapping from data collector rows to entries.
17
+
18
+
### Postgres
9
19
10
20
Exports data to Postgres database (with pgvector extension).
11
21
22
+
#### Data Mapping
23
+
24
+
Here's how CocoIndex data elements map to Postgres elements during export:
25
+
26
+
| CocoIndex Element | Postgres Element |
27
+
|-------------------|------------------|
28
+
| an export target | a unique table |
29
+
| a collected row | a row |
30
+
| a field | a column |
31
+
32
+
For example, if you have a data collector that collects rows with fields `id`, `title`, and `embedding`, it will be exported to a Postgres table with corresponding columns.
33
+
It should be a unique table, meaning that no other export target should export to the same table.
34
+
35
+
#### Spec
36
+
12
37
The spec takes the following fields:
13
38
14
39
*`database_url` (type: `str`, optional): The URL of the Postgres database to use as the internal storage, e.g. `postgres://cocoindex:cocoindex@localhost/cocoindex`. If unspecified, will use the same database as the [internal storage](/docs/core/basics#internal-storage).
15
40
16
41
*`table_name` (type: `str`, optional): The name of the table to store to. If unspecified, will generate a new automatically. We recommend specifying a name explicitly if you want to directly query the table. It can be omitted if you want to use CocoIndex's query handlers to query the table.
17
42
18
-
## Qdrant
43
+
###Qdrant
19
44
20
45
Exports data to a [Qdrant](https://qdrant.tech/) collection.
21
46
47
+
#### Data Mapping
48
+
49
+
Here's how CocoIndex data elements map to Qdrant elements during export:
50
+
51
+
| CocoIndex Element | Qdrant Element |
52
+
|-------------------|------------------|
53
+
| an export target | a unique collection |
54
+
| a collected row | a point |
55
+
| a field | a named vector (for fields with vector type); a field within payload (otherwise) |
56
+
57
+
#### Spec
58
+
22
59
The spec takes the following fields:
23
60
24
61
*`collection_name` (type: `str`, required): The name of the collection to export the data to.
@@ -46,9 +83,97 @@ doc_embeddings.export(
46
83
47
84
You can find an end-to-end example [here](https://github.com/cocoindex-io/cocoindex/tree/main/examples/text_embedding_qdrant).
48
85
49
-
## Neo4j
86
+
## Property Graph Targets
87
+
88
+
Property graph is a graph data model where both nodes and relationships can have properties.
89
+
90
+
### Data Mapping
91
+
92
+
In CocoIndex, you can export data to property graph databases.
93
+
This usually involves more than one collectors, and you export them to different types of graph elements (nodes and relationships).
94
+
In particular,
95
+
96
+
1. You can export rows from some collectors to nodes in the graph.
97
+
2. You can export rows from some other collectors to relationships in the graph.
98
+
3. Some nodes referenced by relationships exported in 2 may not exist as nodes exported in 1.
99
+
CocoIndex will automatically create and keep these nodes, as long as they're still referenced by at least one relationship.
100
+
This guarantees that all relationships exported in 2 are valid.
101
+
102
+
We provide common types `NodeMapping`, `RelationshipMapping`, and `ReferencedNode`, to configure for each situation.
103
+
They're agnostic to specific graph databases.
104
+
105
+
#### Nodes
106
+
107
+
Here's how CocoIndex data elements map to nodes in the graph:
108
+
109
+
| CocoIndex Element | Graph Element |
110
+
|-------------------|------------------|
111
+
| an export target | nodes with a unique label |
112
+
| a collected row | a node |
113
+
| a field | a property of node |
114
+
115
+
Note that the label used in different `NodeMapping`s should be unique.
116
+
117
+
`cocoindex.storages.NodeMapping` is to describe mapping to nodes. It has the following fields:
118
+
119
+
*`label` (type: `str`): The label of the node.
120
+
121
+
For example, if you have a data collector that collects rows with fields `id`, `name` and `gender`, it can be exported to a node with label `Person` and properties `id``name` and `gender`.
50
122
51
-
If you don't have a Postgres database, you can start a Postgres SQL database for cocoindex using our docker compose config:
123
+
#### Relationships
124
+
125
+
Here's how CocoIndex data elements map to relationships in the graph:
126
+
127
+
| CocoIndex Element | Graph Element |
128
+
|-------------------|------------------|
129
+
| an export target | relationships with a unique type |
130
+
| a collected row | a relationship |
131
+
| a field | a property of relationship, or a property of source/target node, based on configuration |
132
+
133
+
Note that the type used in different `RelationshipMapping`s should be unique.
134
+
135
+
`cocoindex.storages.RelationshipMapping` is to describe mapping to relationships. It has the following fields:
136
+
137
+
*`rel_type` (type: `str`): The type of the relationship.
138
+
*`source`/`target` (type: `cocoindex.storages.NodeReferenceMapping`): Specify how to extract source/target node information from the collected row. It has the following fields:
139
+
*`label` (type: `str`): The label of the node.
140
+
*`fields` (type: `Sequence[cocoindex.storages.TargetFieldMapping]`): Specify field mappings from the collected rows to node properties, with the following fields:
141
+
*`source` (type: `str`): The name of the field in the collected row.
142
+
*`target` (type: `str`, optional): The name of the field to use as the node field. If unspecified, will use the same as `source`.
143
+
144
+
:::note Map necessary fields for nodes of relationships
145
+
146
+
You need to map the following fields for nodes of each relationship:
147
+
148
+
* Make sure all primary key fields for the label are mapped.
149
+
* Optionally, you can also map non-key fields. If you do so, please make sure all value fields are mapped.
150
+
151
+
:::
152
+
153
+
All fields in the collector that are not used in mappings for source or target node fields will be mapped to relationship properties.
154
+
155
+
#### Nodes only referenced by relationships
156
+
157
+
If a node appears as source or target of a relationship, but not exported using `NodeMapping`, CocoIndex will automatically create and keep these nodes until it's no longer referenced by any relationships.
158
+
159
+
:::note Merge of node values
160
+
161
+
If the same node (as identified by primary key values) appears multiple times (e.g. they're referenced by different relationships),
162
+
CocoIndex uses value fields provided by an arbitrary one of them.
163
+
The best practice is to make the value fields consistent across different appearances of the same node, to avoid non-determinism in the exported graph.
164
+
165
+
:::
166
+
167
+
If a node's label specified in `NodeReferenceMapping` doesn't exist in any `NodeMapping`, you need to [declare](../core/flow_def#target-declarations) a `ReferencedNode` to configure [storage indexes](../core/flow_def#storage-indexes) for nodes with this label.
168
+
The following options are supported:
169
+
170
+
*`primary_key_fields` (required)
171
+
*`vector_indexes` (optional)
172
+
173
+
174
+
### Neo4j
175
+
176
+
If you don't have a Neo4j database, you can start a Neo4j database using our docker compose config:
52
177
53
178
```bash
54
179
docker compose -f <(curl -L https://raw.githubusercontent.com/cocoindex-io/cocoindex/refs/heads/main/dev/neo4j.yaml) up -d
@@ -69,25 +194,10 @@ The `Neo4j` storage exports each row as a relationship to Neo4j Knowledge Graph.
69
194
*`user` (type: `str`): Username for the Neo4j database.
70
195
*`password` (type: `str`): Password for the Neo4j database.
71
196
*`db` (type: `str`, optional): The name of the Neo4j database to use as the internal storage, e.g. `neo4j`.
72
-
*`mapping`: The mapping from collected row to nodes or relationships of the graph. 2 variations are supported:
73
-
*`cocoindex.storages.NodeMapping`: Each collected row is mapped to a node in the graph. It has the following fields:
74
-
*`label`: The label of the node.
75
-
*`cocoindex.storages.RelationshipMapping`: Each collected row is mapped to a relationship in the graph,
76
-
With the following fields:
77
-
78
-
*`rel_type` (type: `str`): The type of the relationship.
79
-
*`source`/`target` (type: `cocoindex.storages.NodeReferenceMapping`): The source/target node of the relationship, with the following fields:
80
-
*`label` (type: `str`): The label of the node.
81
-
*`fields` (type: `Sequence[cocoindex.storages.TargetFieldMapping]`): Map fields from the collector to nodes in Neo4j, with the following fields:
82
-
*`source` (type: `str`): The name of the field in the collected row.
83
-
*`target` (type: `str`, optional): The name of the field to use as the node field. If unspecified, will use the same as `source`.
84
-
85
-
:::info
197
+
*`mapping` (type: `NodeMapping | RelationshipMapping`): The mapping from collected row to nodes or relationships of the graph. 2 variations are supported:
86
198
87
-
All fields specified in `fields.source` will be mapped to properties of source/target nodes. All remaining fields will be mapped to relationship properties by default.
199
+
Neo4j also provides a declaration spec `Neo4jDeclaration`, to configure indexing options for nodes only referenced by relationships. It has the following fields:
88
200
89
-
:::
201
+
*`connection` (type: auth reference to `Neo4jConnectionSpec`)
* `nodes_storage_spec` (type: `dict[str, cocoindex.storages.NodeStorageSpec]`): This configures indexes for different node labels. Key is the node label. The value type `NodeStorageSpec` has the following fields to configure [storage indexes](../core/flow_def#storage-indexes) for the node.
92
-
* `primary_key_fields` is required.
93
-
* `vector_indexes` is also supported and optional.
0 commit comments