Skip to content

Instantly share code, notes, and snippets.

View vladbatushkov's full-sized avatar
🥥
Sabai sabai

Vlad Batushkov vladbatushkov

🥥
Sabai sabai
View GitHub Profile
@vladbatushkov
vladbatushkov / omgc-may-16-3.cypher
Created May 16, 2019 17:35
Set SHARE relationship
MATCH (m1:Movie), (m2:Movie)
WHERE NOT (m1)-[:SHARE]-(m2) AND id(m1) <> id(m2)
WITH m1, m2, apoc.coll.intersection(m1.keywords, m2.keywords) as sameKeys
WITH m1, m2, sameKeys, size(sameKeys) as total
WHERE total > 3
MERGE (m1)-[:SHARE { keywords: sameKeys }]->(m2)
@vladbatushkov
vladbatushkov / omgc-may-16-4.cypher
Created May 16, 2019 17:35
Result of same keywords of moives.
MATCH (m1:Movie)-[s:SHARE]->(m2:Movie)
WITH apoc.coll.removeAll(s.keywords, ["about", "though", "their", "takes", "which", "around", "other", "makes", "films", "scene", "often", "while", "another", "after", "perphaps", "should" ]) as valids, m1.title as film1, m2.title as film2
WITH film1, film2, valids, size(valids) as total
WHERE total > 3
RETURN film1, film2, valids, total
ORDER BY total DESC
LIMIT 20
WITH "https://en.wikipedia.org" as url
CALL apoc.load.html(url + "/wiki/List_of_museums_in_Moscow", { data: "div.mw-content-ltr > div > ul a" }) YIELD value
UNWIND value.data as item
WITH { name: item.text, url: item.attributes.href } as m, url
CALL apoc.load.html(url + m.url, { data: "table.infobox.vcard span.geo" }) YIELD value
UNWIND value.data as coord
WITH apoc.text.split(coord.text, "; ") as coords, m
WITH { name: m.name, latitude: toFloat(coords[0]), longitude: toFloat(coords[1]) } as n
MERGE (:Museum { name: n.name, location: point({ latitude: n.latitude, longitude: n.longitude }) })
MATCH (m1:Museum), (m2:Museum)
WHERE id(m1) <> id(m2)
WITH m1, collect({ toId: id(m2), dist: apoc.math.round(distance(m1.location, m2.location) / 1000, 1) }) as distances
WITH m1, filter(x IN distances WHERE x.dist > 0.0) as needToWalk
WITH m1, apoc.coll.sortMaps(needToWalk, '^dist') as sorted
UNWIND sorted[0..3] as nearm
MATCH (m:Museum)
WHERE NOT (m1)-[:WALK]-(m) AND id(m) = nearm.toId
MERGE (m1)-[:WALK { distanceKm: nearm.dist }]->(m)
@vladbatushkov
vladbatushkov / omgc-may-18-3.cypher
Last active May 19, 2019 13:09
Museums to visit.
MATCH (from:Museum)
MATCH (to:Museum)
WHERE id(from) < id(to)
CALL algo.kShortestPaths.stream(from, to, 1, 'distanceKm') YIELD index, nodeIds, costs
WITH [node in algo.getNodesById(nodeIds) | node.name] AS museums,
duration({ minutes: reduce(acc = 0.0, cost in costs | acc + (cost / 4 * 60)) }) AS walkDuration,
duration({ minutes: reduce(acc = 20.0, cost in costs | acc + 20) }) AS visitDuration
WITH museums, walkDuration, visitDuration, duration({ minutes: walkDuration.minutes + visitDuration.minutes }) as totalDuration
WHERE totalDuration.minutes <= 300
RETURN DISTINCT museums, size(museums) as totalMuseums, walkDuration.hours + "h " + walkDuration.minutesOfHour + "m" as walkDuration, visitDuration.hours + "h " + visitDuration.minutesOfHour + "m" as visitDuration, totalDuration.hours + "h " + totalDuration.minutesOfHour + "m" as totalDuration
@vladbatushkov
vladbatushkov / import.cypher
Last active October 6, 2020 05:06
Workshop_WorkingGroup_Session1
//Enable multi statement query editor in Database Settings (!)
MATCH (n) DETACH DELETE n;
LOAD CSV WITH HEADERS FROM 'https://vbatushkov.bitbucket.io/log_of_calls.csv' AS line
MERGE (c1:City { name: line.from_city })
MERGE (p1:Person { name: line.from_name, number: line.from_number, gender: line.from_gender })
MERGE (p1)-[:FROM]->(c1)
MERGE (c2:City { name: line.to_city })
MERGE (p2:Person { name: line.to_name, number: line.to_number, gender: line.to_gender })
MERGE (p2)-[:FROM]->(c2)
CREATE (c:Call { from: datetime(line.from_dt),
@vladbatushkov
vladbatushkov / calls_init_by_Liam.cypher
Created July 10, 2019 15:15
How many calls initiated by Liam?
MATCH (p:Person)-[:OUT]->(c:Call)
WHERE p.name = "Liam"
RETURN p.name, count(c) as calls
@vladbatushkov
vladbatushkov / calls_in_May.cypher
Created July 10, 2019 16:15
How many calls started in May?
MATCH (c:Call)
WHERE c.from.month = 5
RETURN count(c) as calls
@vladbatushkov
vladbatushkov / calls_by_Robert.cypher
Last active July 30, 2019 16:03
Robert called to this persons.
MATCH (p1:Person)-[:OUT]->(:Call)<-[:IN]-(p2:Person)
WHERE p1.name = "Robert"
RETURN p2
@vladbatushkov
vladbatushkov / calls_in_Feb_by_day.cypher
Last active August 1, 2019 18:08
Day in February with lowest number of calls.
// How to use NOT: https://neo4j.com/docs/cypher-manual/current/clauses/where/#filter-on-patterns-using-not
MATCH (c:Call)
WHERE c.from.month = 2
RETURN date(c.from), count(c) as calls
ORDER BY calls
LIMIT 10