Skip to content

Instantly share code, notes, and snippets.

@luanne
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save luanne/66acb4a7e53be6181449 to your computer and use it in GitHub Desktop.

Select an option

Save luanne/66acb4a7e53be6181449 to your computer and use it in GitHub Desktop.
Visiting Cities

Visiting Cities

"I am playing around with Neo4J, and I have nodes of persons and cities visited. Lets say that I have visited London, and I want to see I want to create a query returning all cities that has been visited by more than one of the persons that have visited a city you have visited. However, it must not be a city that you have visited. Whoa, that was a mouthful."

We’ll figure this out in a couple of steps using the data provided:

Setup

create (Chris:person {name:'Chris'}),(Jeff:person {name:'Jeff'}),(Pete:person {name:'Pete'}),(Alex:person{name:'Alex'}),(John:person {name:'John'}),(London:city {name:'London'}),(Paris:city {name:'Paris'}),(Mumbay:city {name:'Mumbay'}),(Moskva:city {name:'Moskva'}),(Seattle:city {name:'Seattle'}),(Chris)-[:VISITED]->(London),(Jeff)-[:VISITED]->(London),(John)-[:VISITED]->(London),(Jeff)-[:VISITED]->(Paris),(John)-[:VISITED]->(Paris),(Jeff)-[:VISITED]->(Seattle),(London)-[:VISITED_BY]->(Chris),(London)-[:VISITED_BY]->(Jeff),(London)-[:VISITED_BY]->(John),(Paris)-[:VISITED_BY]->(Jeff),(Paris)-[:VISITED_BY]->(John),(Seattle)-[:VISITED_BY]->(Jeff),(Pete)-[:VISITED]->(Seattle),(Seattle)-[:VISITED_BY]->(Pete),(Chris)-[:VISITED]->(Mumbay),(Mumbay)-[:VISITED_BY]->(Chris),(Alex)-[:VISITED]->(Mumbay),(Mumbay)-[:VISITED_BY]->(Alex),(Alex)-[:VISITED]->(Moskva),(Moskva)-[:VISITED_BY]->(Alex),(Pete)-[:VISITED]->(Moskva),(Moskva)-[:VISITED_BY]->(Pete)

The graph

match n return n

Queries:

Which people have visited the same cities that I’ve visited?

  • John, Jeff both visited London

  • Alex visited Mumbay

MATCH (chris:person {name:'Chris'})-[:VISITED]->(city)<-[:VISITED]-(otherPerson)
RETURN otherPerson.name;

Which cities were visited by people who visited the same cities that I’ve visited?

  • London (visited by Jeff and John)

  • Paris (visited by Jeff and John)

  • Seattle (visited by Jeff)

  • Mumbay (visited by Alex)

  • Moskva (visited by Alex)

MATCH (chris:person {name:'Chris'})-[:VISITED]->(city)<-[:VISITED]-(otherPerson)
MATCH (otherPerson)-[:VISITED]->(otherCity)
RETURN distinct otherCity.name;

Which cities were visited by people who visited the same cities that I’ve visited BUT that city should not have been visited by me AND more than one person should have visited that city.

  • Paris (Seattle had just Jeff visiting, Moskva just had Alex visiting. London & Mumbay have been visited by Chris)

MATCH (chris:person { name:'Chris' })-[:VISITED]->(city)<-[:VISITED]-(otherPerson)
MATCH (otherPerson)-[:VISITED]->(otherCity)
WHERE otherCity<>city
WITH otherCity.name AS cityName, collect(otherPerson.name) AS persons
WHERE length(persons)> 1
RETURN cityName

Created by Luanne Misquitta, GraphAware

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment