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
Copy file name to clipboardExpand all lines: docs/docs/query.mdx
+113-1Lines changed: 113 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,10 @@ The main functionality of CocoIndex is indexing.
12
12
The goal of indexing is to enable efficient querying against your data.
13
13
You can use any libraries or frameworks of your choice to perform queries.
14
14
At the same time, CocoIndex provides seamless integration between indexing and querying workflows.
15
-
For example, you can share transformations between indexing and querying, and easily retrieve table names when using CocoIndex's default naming conventions.
15
+
16
+
* You can share transformations between indexing and querying.
17
+
* You can define query handlers, so that you can easily run queries in tools like CocoInsight.
18
+
* You can easily retrieve table names when using CocoIndex's default naming conventions.
Query handlers let you expose a simple function that takes a query string and returns structured results. They are discoverable by tools like CocoInsight so you can query your indexes without writing extra glue code.
85
+
86
+
-**What you write**: a plain Python function `def search(query: str) -> cocoindex.QueryOutput`.
87
+
-**How you register**: decorate it with `@<your_flow>.query_handler(...)` or call `flow.add_query_handler(...)` directly.
88
+
-**What you return**: a `cocoindex.QueryOutput(results=[...], query_info=...)`.
89
+
-**Optional metadata**: `QueryHandlerResultFields` tells tools which fields contain the embedding vector and score.
90
+
91
+
### Minimum Query Handler
92
+
93
+
A minimum query handler looks like this:
94
+
95
+
<Tabs>
96
+
<TabItemvalue="python"label="Python">
97
+
98
+
```python
99
+
@my_flow.query_handler(name="run_query") # Name is optional, use the function name by default
- The decorator registers the handler as a query handler for the flow. It doesn't change the function signature: you can still call the function directly.
115
+
116
+
Your function returns a `cocoindex.QueryOutput`, with a `results` field, which is a list of dicts (or dataclass instances) representing query results.
117
+
Each element is a query result. All data types convertible to JSON are supported. Embeddings can be `list[float]` or numpy array.
118
+
119
+
A simple query handler like this will enable CocoInsight to display the query results for you to view easily.
120
+
121
+
### Query Handler with Additional Information
122
+
123
+
You can provide additional information by extra fields like this:
124
+
125
+
<Tabs>
126
+
<TabItemvalue="python"label="Python">
127
+
128
+
```python
129
+
@my_flow.query_handler(
130
+
name="run_query", # Name is optional, use the function name by default
131
+
result_fields=cocoindex.QueryHandlerResultFields(
132
+
embedding=["embedding"], # path to the vector field in each result
133
+
score="score", # numeric similarity score (higher is better)
-`result_fields` within `query_handler` specifies field names in the query results returned by the query handler. This provides metadata for tools like CocoInsight to recognize structure of the query results, as specified by the following fields (all optional):
157
+
-`embedding` is a list of keys that navigates to the embedding in each result (use multiple in case of multiple embeddings, e.g. using different models).
158
+
-`score` should point to a numeric field where larger means more relevant.
159
+
160
+
-`QueryOutput.query_info` specifies information for the query itself, with the following fields (all optional):
161
+
-`embedding` is the embedding of the query.
162
+
-`similarity_metric` is the similarity metric used to query the index.
163
+
164
+
165
+
### Directly Register without Decorator
166
+
167
+
The above example can be written without decorator like this:
In your indexing flow, when you export data to a target, you can specify the target name (e.g. a database table name, a collection name, the node label in property graph databases, etc.) explicitly,
0 commit comments