forked from neo4j-product-examples/graphrag-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
northwind-data.cypher
97 lines (81 loc) · 3.86 KB
/
northwind-data.cypher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//THIS FILE IS FOR WHEN YOU ARE USING OPENAI API DIRECTLY:
//set openai api key for text embedding - REPLACE WITH YOUR OWN
:param provider => 'OpenAI';
:param openAIKey => "<your OpenAI API Key>";
/////////////////////////////////////////////////////////
// Load Northwind Data
/////////////////////////////////////////////////////////
CREATE CONSTRAINT Product_productID IF NOT EXISTS FOR (p:Product) REQUIRE (p.productID) IS UNIQUE;
CREATE CONSTRAINT Category_categoryID IF NOT EXISTS FOR (c:Category) REQUIRE (c.categoryID) IS UNIQUE;
CREATE CONSTRAINT Supplier_supplierID IF NOT EXISTS FOR (s:Supplier) REQUIRE (s.supplierID) IS UNIQUE;
CREATE CONSTRAINT Customer_customerID IF NOT EXISTS FOR (c:Customer) REQUIRE (c.customerID) IS UNIQUE;
CREATE CONSTRAINT Order_orderID IF NOT EXISTS FOR (o:Order) REQUIRE (o.orderID) IS UNIQUE;
CREATE CONSTRAINT Address_addressID IF NOT EXISTS FOR (a:Address) REQUIRE (a.addressID) IS UNIQUE;
LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/products.csv" AS row
MERGE (n:Product {productID:row.productID})
SET n += row,
n.unitPrice = toFloat(row.unitPrice),
n.unitsInStock = toInteger(row.unitsInStock), n.unitsOnOrder = toInteger(row.unitsOnOrder),
n.reorderLevel = toInteger(row.reorderLevel), n.discontinued = (row.discontinued <> "0");
LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/categories.csv" AS row
MERGE (n:Category {categoryID:row.categoryID})
SET n += row;
LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/suppliers.csv" AS row
MERGE (n:Supplier {supplierID:row.supplierID})
SET n += row;
MATCH (p:Product),(c:Category)
WHERE p.categoryID = c.categoryID
MERGE (p)-[:BELONGS_TO]->(c);
MATCH (p:Product),(s:Supplier)
WHERE p.supplierID = s.supplierID
MERGE (s)<-[:SUPPLIED_BY]-(p);
LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/customers.csv" AS row
MERGE (n:Customer {customerID:row.customerID})
SET n += row;
LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/orders.csv" AS row
MERGE (o:Order {orderID:row.orderID})
SET o.customerID = row.customerID,
o.employeeID = row.employeeID,
o.orderDate = row.orderDate,
o.requiredDate = row.requiredDate,
o.shippedDate = row.shippedDate,
o.shipVia = row.shipVia,
o.freight = row.freight
MERGE (a:Address {addressID: apoc.text.join([coalesce(row.shipName, ''), coalesce(row.shipAddress, ''),
coalesce(row.shipCity, ''), coalesce(row.shipRegion, ''), coalesce(row.shipPostalCode, ''),
coalesce(row.shipCountry, '')], ', ')})
SET a.name = row.shipName,
a.address = row.shipAddress,
a.city = row.shipCity,
a.region = row.shipRegion,
a.postalCode = row.shipPostalCode,
a.country = row.shipCountry
MERGE (o)-[:SHIPPED_TO]->(a)
WITH o
MATCH (c:Customer)
WHERE c.customerID = o.customerID
MERGE (c)-[:ORDERED]->(o);
LOAD CSV WITH HEADERS FROM "https://data.neo4j.com/northwind/order-details.csv" AS row
MATCH (p:Product), (o:Order)
WHERE p.productID = row.productID AND o.orderID = row.orderID
MERGE (o)-[details:ORDER_CONTAINS]->(p)
SET details = row,
details.quantity = toInteger(row.quantity);
/////////////////////////////////////////////////////////
// Set Text Property and Vector Index
/////////////////////////////////////////////////////////
//create text and embedding vector properties
MATCH(p:Product)-[:BELONGS_TO]-(c:Category)
SET p.text = "Product Category: " + c.categoryName + ' - ' + c.description + "\nProduct Name: " + p.productName
WITH p, genai.vector.encode(p.text, 'OpenAI', {token:$openAIKey}) AS textEmbedding
CALL db.create.setNodeVectorProperty(p,'textEmbedding', textEmbedding)
RETURN p.productID, p.text, p.textEmbedding;
//create vector index
CREATE VECTOR INDEX product_text_embeddings
FOR (n:Product) ON (n.textEmbedding)
OPTIONS {indexConfig: {
`vector.dimensions`: 1536,
`vector.similarity_function`: 'cosine'
}};
//await index coming online
CALL db.awaitIndex("product_text_embeddings", 300);