Skip to content

Instantly share code, notes, and snippets.

@georgyangelov
Last active August 29, 2015 14:13
Show Gist options
  • Save georgyangelov/1f7216fec754136b21ff to your computer and use it in GitHub Desktop.
Save georgyangelov/1f7216fec754136b21ff to your computer and use it in GitHub Desktop.
Sample Neo4j/Cypher queries
create
(p1:Person {name: 'Бригадир Цветков', age: 24, gender: 'm'}),
(p2:Person {name: 'Букетка Градинарова', age: 26, gender: 'f'}),
(p3:Person {name: 'Вовеки Веков Иванов', age: 25, gender: 'm'}),
(p4:Person {name: 'Гъдьо Гъдев', age: 22, gender: 'm'}),
(p5:Person {name: 'Дар Донков', age: 27, gender: 'm'}),
(p6:Person {name: 'Кирка Градинарска', age: 29, gender: 'f'}),
(p7:Person {name: 'Кучо Кучев', age: 30, gender: 'm'}),
(p8:Person {name: 'Лимон Генов', age: 50, gender: 'm'}),
(p9:Person {name: 'Марулка Митева', age: 19, gender: 'f'}),
(p10:Person {name: 'Мишка Върбанова', age: 33, gender: 'f'}),
(p11:Person {name: 'Пульо Пулев', age: 26, gender: 'm'}),
(p12:Person {name: 'Салфетка Иванова', age: 40, gender: 'f'})
create
(p3)-[:friend_with]->(p1),
(p2)-[:friend_with]->(p5),
(p6)-[:friend_with]->(p1),
(p10)-[:friend_with]->(p6),
(p7)-[:friend_with]->(p8),
(p4)-[:friend_with]->(p9),
(p5)-[:friend_with]->(p4),
(p9)-[:friend_with]->(p8),
(p11)-[:friend_with]->(p7),
(p10)-[:friend_with]->(p2)
create
(i1:Hobby {name: 'филми'}),
(i2:Hobby {name: 'комютърни игри'}),
(i3:Hobby {name: 'програмиране'}),
(i4:Hobby {name: 'тенис'}),
(i5:Hobby {name: 'готвене'}),
(i6:Hobby {name: 'телевизия'}),
(i7:Hobby {name: 'ски'})
create
(p1)-[:likes {hours: 1, years: 3}]->(i6),
(p1)-[:likes {hours: 5, years: 4}]->(i3),
(p1)-[:likes {hours: 3, years: 1}]->(i2),
(p2)-[:likes {hours: 7, years: 2}]->(i3),
(p2)-[:likes {hours: 6, years: 1}]->(i1),
(p2)-[:likes {hours: 2, years: 1}]->(i2),
(p3)-[:likes {hours: 7, years: 3}]->(i2),
(p3)-[:likes {hours: 8, years: 4}]->(i6),
(p3)-[:likes {hours: 1, years: 6}]->(i5),
(p4)-[:likes {hours: 4, years: 6}]->(i4),
(p4)-[:likes {hours: 9, years: 3}]->(i5),
(p4)-[:likes {hours: 2, years: 1}]->(i3),
(p5)-[:likes {hours: 6, years: 8}]->(i6),
(p5)-[:likes {hours: 8, years: 1}]->(i3),
(p5)-[:likes {hours: 5, years: 310}]->(i2),
(p6)-[:likes {hours: 9, years: 11}]->(i7),
(p6)-[:likes {hours: 9, years: 4}]->(i5),
(p6)-[:likes {hours: 4, years: 7}]->(i3),
(p7)-[:likes {hours: 6}]->(i5),
(p7)-[:likes {hours: 3}]->(i1),
(p7)-[:likes {hours: 4}]->(i2),
(p8)-[:likes {hours: 8}]->(i2),
(p8)-[:likes {hours: 7}]->(i3),
(p8)-[:likes {hours: 4}]->(i6),
(p9)-[:likes {hours: 8}]->(i6),
(p9)-[:likes {hours: 6}]->(i3),
(p9)-[:likes {hours: 5}]->(i7),
(p10)-[:likes {hours: 1}]->(i2),
(p10)-[:likes {hours: 7}]->(i5),
(p10)-[:likes {hours: 1}]->(i3),
(p11)-[:likes {hours: 9}]->(i7),
(p11)-[:likes {hours: 2}]->(i5),
(p11)-[:likes {hours: 2}]->(i3),
(p12)-[:likes {hours: 3}]->(i2),
(p12)-[:likes {hours: 9}]->(i1),
(p12)-[:likes {hours: 5, years: 7}]->(i4)
# Всички хобита
match (a:Hobby) return a
# Всички хора
match (p:Person) return p
# Всички хора между 20 и 30 години
match (p:Person) where p.age >= 20 and p.age <= 30 return p.name, p.age
# Всички хора с техните хобита
match (p:Person)-[:likes]-(h:Hobby) return p.name, h.name
# Всички хора, които харесват да готвят
match (p:Person)-[:likes]-(:Hobby {name: 'готвене'}) return p.name
# Какво харесва Кучо Кучев
match (:Person {name: 'Кучо Кучев'})-[:likes]-(hobby) return hobby.name
# Всички жени с поне един общ интерес с Кучо
match (:Person {name: 'Кучо Кучев'})-[:likes]->(hobby)<-[:likes]-(p:Person {gender: 'f'}) return p.name, hobby.name
# Като горното, но заедно с брой общи интереси и сортиране
match (:Person {name: 'Кучо Кучев'})-[:likes]->(hobby)<-[:likes]-(p:Person {gender: 'f'})
return p.name, count(hobby) as hobby_count
order by hobby_count desc
# Да намерим жена на Кучо
match (:Person {name: 'Кучо Кучев'})-[ku4o_like:likes]->(hobby)<-[girl_like:likes]-(p:Person {gender: 'f'})
return p.name as wife_candidate,
count(hobby) as hobby_count,
sum(abs(ku4o_like.hours - girl_like.hours)) as like_delta
order by hobby_count desc, like_delta asc
match (ku4o:Person {name: 'Кучо Кучев'})-[ku4o_like:likes]->(hobby)<-[girl_like:likes]-(p:Person {gender: 'f'})
with
p,
count(hobby) as hobby_count,
sum(abs(ku4o_like.hours - girl_like.hours)) as like_delta,
abs(ku4o.age - p.age) as age_delta
return
p,
hobby_count * 10 - like_delta*0.1 - age_delta as score
order by score desc
# Всички приятелства
match (p:Person)-[:friend_with]->(p2:Person) return p, p2
# Изтриване на връзка
match (p:Person {name: 'Букетка Градинарова'})-[f:friend_with]->()
delete f
# Всички хора без приятели
match (p:Person)
where not (p)-[:friend_with]-()
return p
# Да препоръчаме приятели на Марулка
match (marulka:Person {name: 'Марулка Митева'})-[:friend_with]-()-[:friend_with]-(p)
where not p = marulka
return p
# Да препоръчаме още приятели на марулка
match (marulka:Person {name: 'Марулка Митева'})-[:friend_with*1..3]-(p)
where not p = marulka and not (marulka)-[:friend_with]-(p)
return p
# Да препоръчаме още приятели на марулка (подредени по релевантност)
match (marulka:Person {name: 'Марулка Митева'})-[f:friend_with*1..3]-(p)
where not p = marulka and not (marulka)-[:friend_with]-(p)
return p.name, length(f) - 1 as length
order by length asc
# Тенис партньор на Марулка, който е тренирал от поне 5 години и в момента играе поне 2 часа на седмица
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment