-
Notifications
You must be signed in to change notification settings - Fork 0
/
beer_reviews.cypher
222 lines (192 loc) · 9.28 KB
/
beer_reviews.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘ NEO4J CONFIGURATION ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
// I ▬▬▬▬▬▬ Adding apoc procedures ▬▬▬▬▬▬
// To check neo4j version :
call dbms.components() yield name, versions, edition unwind versions as version return name, version, edition;
"""
If your NEO4J version is 4.1.X then download any 4.1.X apoc package
Download jar file from https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/tag/4.1.0.12
Place into your $NEO4J_HOME/plugins folder. ubuntu : sudo mv /home/user0/apoc-4.1.0.12-all.jar /var/lib/neo4j/plugins/apoc-4.1.0.12-all.jar
restart NEO4J : sudo systemctl restart neo4j.service
Check if apoc is correctly installed :
1. Open NEO4J Desktop
2. execute following command : call apoc.help('apoc')
"""
// II ▬▬▬▬▬▬ Adding beer_reviews.csv to project from VM ▬▬▬▬▬▬
dowload file from https://www.kaggle.com/datasets/rdoume/beerreviews
use pscp to load file on 10.8.2.35 with user:user0
change directory file to : file:/var/lib/neo4j/import/beer_reviews.csv
// III ▬▬▬▬▬▬ NEO4J Memory Configuration ▬▬▬▬▬▬
cd/etc/neo4j
neo4j-admin memrec --memory=4G
"""
# Based on the above, the following memory settings are recommended:
dbms.memory.heap.initial_size=2g
dbms.memory.heap.max_size=2g
dbms.memory.pagecache.size=512m
"""
// → modification of neo4j.conf
// ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘ NODES CONFIGURATION ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
// I. ▬▬▬▬▬▬ Reviews ▬▬▬▬▬▬
// Creating the table
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH
toIntegerOrNull(row.brewery_id) as brewery_id,
toIntegerOrNull(row.beer_beerid) as beer_beerid,
toStringOrNull(row.review_profilename) as review_profilename,
toIntegerOrNull(row.review_palate) as review_palate,
toIntegerOrNull(row.review_appearance) as review_appearance,
toIntegerOrNull(row.review_aroma) as review_aroma,
toIntegerOrNull(row.review_overall) as review_overall,
toIntegerOrNull(row.review_taste) as review_taste
RETURN
brewery_id,
beer_beerid,
review_palate,
review_profilename,
review_appearance,
review_aroma,
review_overall,
review_taste
LIMIT 5;
// Creating nodes for the graph
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH
toInteger(row.brewery_id) as brewery_id,
toInteger(row.beer_beerid) as beer_beerid,
toInteger(row.review_palate) as review_palate,
toString(row.review_profilename) as review_profilename,
toInteger(row.review_appearance) as review_appearance,
toInteger(row.review_aroma) as review_aroma,
toInteger(row.review_overall) as review_overall,
toInteger(row.review_taste) as review_taste
WHERE brewery_id >=24 and brewery_id<26 and review_profilename is not null
MERGE (r:Reviews {brewery_id: brewery_id, beer_beerid: beer_beerid, review_profilename: review_profilename})
SET
r.brewery_id = brewery_id,
r.beer_beerid = beer_beerid,
r.review_palate = review_palate,
r.review_profilename = review_profilename,
r.review_appearance = review_appearance,
r.review_aroma = review_aroma,
r.review_overall = review_overall,
r.review_taste = review_taste
RETURN
count(r);
// To displays created nodes
MATCH (n)
RETURN n
// II. ▬▬▬▬▬▬ REVIEWER ▬▬▬▬▬▬
// Creating the table
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH
toStringOrNull(row.review_profilename) as review_profilename
RETURN
DISTINCT review_profilename
LIMIT 5;
// Creating nodes for the graph
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH
toInteger(row.brewery_id) as brewery_id,
toString(row.review_profilename) as review_profilename
WHERE review_profilename is not null and brewery_id < 25 // filtering on brewery id
MERGE (r:Reviewer {review_profilename: review_profilename})
SET
r.review_profilename = review_profilename
RETURN COUNT(r);
// III. ▬▬▬▬▬▬ Beers and Breweries ▬▬▬▬▬▬
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH
toIntegerOrNull(row.brewery_id) as brewery_id,
toIntegerOrNull(row.beer_beerid) as beer_beerid,
toStringOrNull(row.brewery_name) as brewery_name,
toStringOrNull(row.beer_name) as beer_name,
toStringOrNull(row.beer_style) as beer_style,
toIntegerOrNull(row.beer_abv) as beer_abv
RETURN
brewery_id,
beer_beerid,
brewery_name,
beer_name,
beer_style,
beer_abv
LIMIT 5;
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH
toInteger(row.brewery_id) as brewery_id,
toInteger(row.beer_beerid) as beer_beerid,
toString(row.brewery_name) as brewery_name,
toString(row.beer_name) as beer_name,
toString(row.beer_style) as beer_style,
toInteger(row.beer_abv) as beer_abv
WHERE brewery_id < 25
MERGE (b:Beers {brewery_id: brewery_id, beer_beerid:beer_beerid})
SET
b.brewery_id = brewery_id,
b.beer_beerid = beer_beerid,
b.brewery_name = brewery_name,
b.beer_name = beer_name,
b.beer_style = beer_style,
b.beer_abv = beer_abv
RETURN
count(b);
// ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘ RELATIONS CONFIGURATION ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
"""
// Creating relations between Beers and reviews:
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH toInteger(row.brewery_id) AS brewery_id, toInteger(row.beer_beerid) AS beer_beerid, toString(row.review_profilename) AS review_profilename
MATCH (p:Reviewer {review_profilename: review_profilename})
MATCH (o:Beers {brewery_id: brewery_id, beer_beerid: beer_beerid})
MERGE (o)-[rel:CONTAINS {review_profilename: review_profilename}]->(p)
RETURN count(rel);
"""
// I. ▬▬▬▬▬▬ Creating relations between Review and Reviewer ▬▬▬▬▬▬
USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH toInteger(row.brewery_id) AS brewery_id, toInteger(row.beer_beerid) AS beer_beerid, toString(row.review_profilename) AS review_profilename
MATCH (p:Reviewer {review_profilename: review_profilename})
MATCH (o:Reviews {review_profilename: review_profilename, brewery_id: brewery_id, beer_beerid: beer_beerid})
MERGE (o)-[rel:did_a_review {review_profilename: review_profilename}]->(p)
RETURN count(rel);
// II. ▬▬▬▬▬▬ Creating relations between Reviews and Beers ▬▬▬▬▬▬
USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM 'file:///beer_reviews.csv' AS row
WITH toInteger(row.brewery_id) AS brewery_id, toInteger(row.beer_beerid) AS beer_beerid, toString(row.review_profilename) AS review_profilename
MATCH (p:Reviews {brewery_id: brewery_id, beer_beerid: beer_beerid, review_profilename: review_profilename})
MATCH (o:Beers {brewery_id: brewery_id, beer_beerid: beer_beerid})
MERGE (o)-[rel:reviewed {brewery_id: brewery_id, beer_beerid: beer_beerid, review_profilename: review_profilename}]->(p)
RETURN count(rel);
// ▬▬▬▬▬▬ Displaying all relations and nodes ▬▬▬▬▬▬
// Initial limit of displayed nodes is 300. To change it, run this command :
:config initialNodeDisplay: 1000
// Command to display all nodes from all relations
MATCH
p=()-[r:did_a_review]->() ,
q=()-[s:reviewed]->()
RETURN p,q
LIMIT 500
// All reviews from review_profilename: "philbertk"
MATCH p=(m:Reviews {review_profilename:"philbertk"})<-[:reviewed]-(beer_name)
RETURN beer_name;
//All beers and reviews done by "philbertk"
MATCH q=()-[s:reviewed]-({review_profilename:"philbertk"}),
p=()-[t:did_a_review]-({review_profilename:"philbertk"})
RETURN p,q
// All reviews from 'Widmer Hefeweizen' beer :
MATCH q=()-[s:reviewed]-({beer_name: "Widmer Hefeweizen"})
RETURN q
// Displaying most reviewed beers :
MATCH p=(m:Reviews)<-[:reviewed]-(b)
RETURN b.beer_name, count(m) as count
order by count desc;
//▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼ RECOMMANDATIONS ▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
//Displaying 5 most similar beers to "Widmer Hefeweizen" according to the overall review:
//selecting Reviewers who did a Review which reviewed the Beer called "Widmer Hefeweizen"
MATCH (a:Beers {beer_name: "Widmer Hefeweizen"})-[:reviewed]->(b:Reviews)-[:did_a_review]->(c:Reviewer)
//selecting Beers being reviewed by the profilenames who also reviewed "Widmer Hefeweizen"
MATCH (c)-[:did_a_review]-(e:Reviews)-[:reviewed]-(h:Beers)
//We only keep profilenames who rated our beer at min 4
WHERE b.review_overall>=4
RETURN h.beer_name as similar_beers, count(e) AS number_of_reviews
ORDER BY number_of_reviews DESC
SKIP 1 //we skip first row as it is the beer we are comparing
LIMIT 5