1
+ ######################################
2
+ # Query the database
3
+ ######################################
4
+
5
+ result = driver . execute_query (
6
+ 'MATCH (p:Person {age: $age}) RETURN p.name AS name' ,
7
+ nil , # auth_token not specified
8
+ { database : 'neo4j' } , # config
9
+ age : 42
10
+ )
11
+
12
+ # To access records after
13
+ records = result . records
14
+ records . each do |r |
15
+ puts r
16
+ end
17
+
18
+ # Summary information
19
+ summary = result . summary
20
+ puts "The query #{ summary . query . text } returned #{ records . size } records in #{ summary . result_available_after } ms."
21
+
22
+ ######################################
23
+ # Write to the database
24
+ ######################################
25
+
26
+ # Create two nodes and a relationship
27
+ result = driver . execute_query (
28
+ 'CREATE (a:Person {name: $name})
29
+ CREATE (b:Person {friend: $name})
30
+ CREATE (a)-[:KNOWS]->(b)' ,
31
+ nil ,
32
+ { database : 'neo4j' } ,
33
+ name : 'Alice' ,
34
+ friend : 'David'
35
+ )
36
+
37
+ summary = result . summary
38
+ puts "Created #{ summary . counters . nodes_created } records in #{ summary . result_available_after } ms."
39
+
40
+ ######################################
41
+ # Read from the database
42
+ ######################################
43
+
44
+ # Retrieve all Person nodes who know other Persons
45
+ result = driver . execute_query (
46
+ 'MATCH (p:Person)-[:KNOWS]->(:Person)
47
+ RETURN p.name AS name' ,
48
+ nil ,
49
+ { database : 'neo4j' }
50
+ )
51
+
52
+ records = result . records
53
+ records . each do |r |
54
+ puts r
55
+ end
56
+
57
+ summary = result . summary
58
+ puts "The query #{ summary . query } returned #{ records . size } records in #{ summary . result_available_after } ms."
59
+
60
+ ######################################
61
+ # Update the database
62
+ ######################################
63
+
64
+ # Update node Alice to add an age property
65
+ result = driver . execute_query (
66
+ 'MATCH (p:Person {name: $name})
67
+ SET p.age = $age' ,
68
+ nil ,
69
+ { database : 'neo4j' } ,
70
+ name : 'Alice' ,
71
+ age : 42
72
+ )
73
+
74
+ summary = result . summary
75
+ puts "Query updated the database?"
76
+ puts summary . counters . contains_updates?
77
+
78
+ # Create a relationship :KNOWS between Alice and Bob
79
+ result = driver . execute_query (
80
+ 'MATCH (alice:Person {name: $name})
81
+ MATCH (bob:Person {name: $friend})
82
+ CREATE (alice)-[:KNOWS]->(bob)' ,
83
+ nil ,
84
+ { database : 'neo4j' } ,
85
+ name : 'Alice' ,
86
+ friend : 'Bob'
87
+ )
88
+
89
+ summary = result . summary
90
+ puts 'Query updated the database?'
91
+ puts summary . counters . contains_updates?
92
+
93
+ ######################################
94
+ # Delete from the database
95
+ ######################################
96
+
97
+ # Remove the Alice node and all its relationships
98
+ result = driver . execute_query (
99
+ 'MATCH (p:Person {name: $name})
100
+ DETACH DELETE p' ,
101
+ nil ,
102
+ { database : 'neo4j' } ,
103
+ name : 'Alice'
104
+ )
105
+
106
+ summary = result . summary
107
+ puts "Query updated the database?"
108
+ puts summary . counters . contains_updates?
109
+
110
+ ######################################
111
+ # Summary
112
+ ######################################
113
+
114
+ # Retrieve the execution summary
115
+ result = driver . execute_query (
116
+ "UNWIND ['Alice', 'Bob'] AS name
117
+ MERGE (p:Person {name: name})" ,
118
+ nil ,
119
+ { database : 'neo4j' }
120
+ )
121
+
122
+ puts result . summary
123
+
124
+ # Query counters
125
+ result = driver . execute_query (
126
+ "MERGE (p:Person {name: $name})
127
+ MERGE (p)-[:KNOWS]->(:Person {name: $friend})" ,
128
+ nil ,
129
+ { database : 'neo4j' } ,
130
+ name : 'Mark' ,
131
+ friend : 'Bob'
132
+ )
133
+ query_counters = result . summary . counters
134
+ puts query_counters
135
+
136
+ # Query execution plan
137
+ result = driver . execute_query (
138
+ 'EXPLAIN MATCH (p {name: $name}) RETURN p' ,
139
+ nil ,
140
+ { database : 'neo4j' } ,
141
+ name : 'Alice'
142
+ )
143
+ query_plan = result . summary . plan . arguments [ 'string-representation' ]
144
+ puts query_plan
145
+
146
+ # Notifications with Neo4j status codes
147
+ result = driver . execute_query (
148
+ "MATCH p=shortestPath((:Person {name: $start})-[*]->(:Person {name: $end}))
149
+ RETURN p" ,
150
+ nil ,
151
+ { database : 'neo4j' } ,
152
+ start : 'Alice' ,
153
+ end : 'Bob'
154
+ )
155
+ notifications = result . summary . notifications
156
+ puts notifications
157
+
158
+ # Notifications with GQL status codes
159
+ result = execute_query (
160
+ "MATCH p=shortestPath((:Person {name: $start})-[*]->(:Person {name: $end}))
161
+ RETURN p" ,
162
+ nil ,
163
+ { database : 'neo4j' } ,
164
+ start : 'Alice' ,
165
+ end : 'Bob'
166
+ )
167
+ statuses = result . summary . gql_status_objects
168
+ puts statuses
169
+
170
+ # Filter notifications
171
+ # Allow only WARNING notifications, but not of HINT or GENERIC classifications
172
+ driver = Neo4j ::Driver ::GraphDatabase . driver (
173
+ 'bolt://localhost:7687' ,
174
+ Neo4j ::Driver ::AuthTokens . basic ( 'neo4j' , 'password' ) ,
175
+ notification_config : {
176
+ minimum_severity : :warning , # use :off to disable entirely
177
+ disabled_categories : [ :hint , :generic ]
178
+ }
179
+ )
0 commit comments