Skip to content

Commit 7c55c69

Browse files
committed
updates for @vector
1 parent 26e1d5c commit 7c55c69

File tree

4 files changed

+151
-191
lines changed

4 files changed

+151
-191
lines changed

modules/ROOT/content-nav.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*** xref:directives/schema-configuration/field-configuration.adoc[]
3030
** xref:directives/indexes-and-constraints.adoc[]
3131
** xref:directives/custom-logic.adoc[]
32-
** xref:directives/genai.adoc[]
3332
** xref:directives/custom-directives.adoc[]
3433
3534
* xref:queries-aggregations/index.adoc[Queries and aggregations]

modules/ROOT/pages/directives/genai.adoc

Lines changed: 0 additions & 174 deletions
This file was deleted.

modules/ROOT/pages/directives/index.adoc

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,15 @@ Particularly useful for types that are not correctly pluralized or are non-Engli
105105
|===
106106
| Directive | Description
107107

108-
| xref::/directives/indexes-and-constraints.adoc#type-definitions-indexes-fulltext[`@fulltext`]
108+
| xref::/directives/indexes-and-constraints.adoc#_fulltext_indexes[`@fulltext`]
109109
| Indicates that there should be a fulltext index inserted into the database for the specified Node and its properties.
110110

111-
| xref::/directives/indexes-and-constraints.adoc#type-definitions-constraints-unique[`@unique`]
111+
| xref::/directives/indexes-and-constraints.adoc#_unique_node_property_constraints[`@unique`]
112112
| Indicates that there should be a uniqueness constraint in the database for the fields that it is applied to.
113113

114+
| xref::/directives/indexes-and-constraints.adoc#_vector_index_search[`@vector`]
115+
| Perform a vector index search on your database either based by passing in a vector index or a search phrase.
116+
114117
|===
115118

116119
== Custom logic
@@ -137,20 +140,6 @@ of any required fields that is passed as arguments to the custom resolver.
137140

138141
|===
139142

140-
== Generative AI
141-
142-
[cols="2,5"]
143-
|===
144-
| Directive | Description
145-
146-
| xref::/directives/genai.adoc#_vector[`@vector`]
147-
| Use an existing vector embedding on a node in the database to perform a nearest neighbor search.
148-
149-
| xref::/directives/genai.adoc#_genai[`@genAI`]
150-
| Perform a query which utilizes the link::https://neo4j.com/docs/cypher-manual/current/genai-integrations/[Neo4j GenAI plugin] to create a vector embedding for a search phrase and then compares it to existing vector embeddings on nodes in the database.
151-
152-
|===
153-
154143
== OGM
155144

156145
[cols="2,5"]

modules/ROOT/pages/directives/indexes-and-constraints.adoc

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,149 @@ const schema = await neoSchema.getSchema();
247247
248248
await neoSchema.assertIndexesAndConstraints({ options: { create: true }});
249249
----
250+
251+
252+
:description: Directives related to generative AI in the Neo4j GraphQL Library.
253+
254+
255+
== Vector index search
256+
257+
With the `@vector` GraphQL directive you can query your database to perform a vector index search.
258+
Queries are performed by passing in either a vector index or a query phrase.
259+
260+
A query by vector index finds nodes with a vector embedding similar to that index, a nearest neighbor search.
261+
262+
In contrast, a query by phrase (a string of text) forwards the phrase to the link:https://neo4j.com/docs/cypher-manual/current/genai-integrations/[Neo4j GenAI plugin] and the plugin generates a vector embedding for it.
263+
This embedding is then compared to the node vector embeddings in the database and the result set.
264+
265+
[NOTE]
266+
.Prerequisites
267+
====
268+
* The database must be Neo4j version 5.15 or higher.
269+
* The node vector embeddings already exist in the database.
270+
* The embeddings must have been created using the same method.
271+
* Queries by vector index cannot be performed across multiple labels.
272+
* Queries by phrase require credentials for the Neo4j GenAI plugin.
273+
====
274+
275+
276+
=== Definition
277+
278+
[source, graphql]
279+
----
280+
"""Informs @neo4j/graphql that there should be a vector index in the database, allows users to search by the index in the generated schema."""
281+
directive @vector(indexes: [VectorIndexInput]!) on OBJECT
282+
----
283+
284+
`VectorIndexInput` is defined as follows:
285+
286+
[source, graphql]
287+
----
288+
input VectorIndexInput {
289+
"""(Required) The name of the vector index."""
290+
indexName: String!
291+
"""(Required) The name of the embedding property on the node."""
292+
property: String!
293+
"""(Required) The name of the query."""
294+
queryName: String
295+
"""(Optional) The name of the provider."""
296+
provider: String
297+
"""(Optional) The name of the callback function."""
298+
callback: String
299+
}
300+
----
301+
302+
If the optional field `provider` is set, the type can be used for a query by phrase, otherwise for a query by vector index.
303+
304+
=== Usage
305+
306+
==== Query by vector index
307+
308+
Perform a nearest neighbor search by passing a vector index to your database and find nodes with a vector embedding similar to that index.
309+
310+
.Type definition
311+
[source, graphql]
312+
----
313+
type Product @vector(indexes: [{
314+
indexName: "productDescriptionIndex",
315+
property: "descriptionVector",
316+
queryName: "searchByDescription"
317+
}]) {
318+
id: ID!
319+
name: String!
320+
description: String!
321+
}
322+
----
323+
324+
This defines the query to be performed on all `Product` nodes which have a vector index named `productDescriptionIndex` for the property `descriptionVector`, implying that a vector index has been created for the `description` property of each node.
325+
326+
.Example query
327+
[source, graphql]
328+
----
329+
query FindSimilarProducts($vector: [Float]!) {
330+
searchByDescription(vector: $vector) {
331+
productsConnection {
332+
edges {
333+
cursor
334+
score
335+
node {
336+
id
337+
name
338+
description
339+
}
340+
}
341+
}
342+
}
343+
}
344+
----
345+
346+
The query returns all `Product` nodes with a vector embedding on their `descriptionVector` property which is similar to the vector index argument `$vector`.
347+
348+
=== Query by phrase
349+
350+
Perform a query which utilizes the link:https://neo4j.com/docs/cypher-manual/current/genai-integrations/[Neo4j GenAI plugin] to create a vector embedding for a search phrase and then compares it to existing vector embeddings on nodes in the database.
351+
352+
[NOTE]
353+
====
354+
Requires credentials for the plugin.
355+
====
356+
357+
.Type definition
358+
[source, graphql]
359+
----
360+
type Product @vector(indexes: [{
361+
indexName: "productDescriptionIndex",
362+
property: "descriptionVector",
363+
provider: OpenAI, # Assuming this is configured in the server
364+
queryName: "searchByPhrase"
365+
}]) {
366+
id: ID!
367+
name: String!
368+
description: String!
369+
}
370+
----
371+
372+
This defines the query to be performed on all `Product` nodes which have a vector index named `productDescriptionIndex` for the property `descriptionVector`, implying that a vector index has been created for the `description` property of each node.
373+
374+
.Example query
375+
[source, graphql]
376+
----
377+
query SearchProductsByPhrase($phrase: String!) {
378+
searchByPhrase(phrase: $phrase) {
379+
productsConnection {
380+
edges {
381+
cursor
382+
score
383+
node {
384+
id
385+
name
386+
description
387+
}
388+
}
389+
}
390+
}
391+
}
392+
----
393+
394+
First, the query passes the query phrase argument `$phrase` to the GenAI plugin and lets it generate a vector embedding for the phrase.
395+
Then it returns all `Product` nodes with a vector embedding on their `descriptionVector` property which is similar to the vector embedding generated by the plugin.

0 commit comments

Comments
 (0)