DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Working with Neo4j: Multiple Starting Nodes by Index Lookup

Working with Neo4j: Multiple Starting Nodes by Index Lookup

Mark Needham user avatar by
Mark Needham
·
Jul. 31, 12 · Interview
Like (0)
Save
Tweet
Share
6.11K Views

Join the DZone community and get the full member experience.

Join For Free

I spent a bit of time this evening extracting some data from the ThoughtWorks graph for our marketing team who were interested in anything related to our three European offices in London, Manchester and Hamburg.

The most interesting things we can explore relate to the relationship between people and the offices.

The model around people and offices looks like this:

Current home office

I added a ‘current_home_office’ relationship to make it easier to quickly get to the nodes of people who are currently working in a specific office.

I previously had to use a bit of a convoluted query to achieve the same thing and while the ‘current_home_office’ relationship would need to be maintained as people move offices since I’m just playing around with this data it makes my life much easier.

I wanted to find the number of different offices that people in Europe had worked in which I’ve previously calculated by running three separate queries and manually compiling the results:

START office=node:offices(name = "London - UK South") 
MATCH person-[:current_home_office]->office, person-[:member_of]->otherOffice 
RETURN distinct person.name, count(distinct(otherOffice)) AS offices, ID(person)
ORDER BY offices DESC
START office=node:offices(name = "Manchester - UK North") 
MATCH person-[:current_home_office]->office, person-[:member_of]->otherOffice 
RETURN distinct person.name, count(distinct(otherOffice)) AS offices, ID(person)
ORDER BY offices DESC
START office=node:offices(name = "Hamburg - Germany") 
MATCH person-[:current_home_office]->office, person-[:member_of]->otherOffice 
RETURN distinct person.name, count(distinct(otherOffice)) AS offices, ID(person)
ORDER BY offices DESC

Here we start from a specific office and then match all the people who have this as their current home office. We also match all the offices that person has been a member of and then return a count of the unique offices the person has been to.

That approach works fine but it always felt like I was missing out on a simple way of doing all three of those queries in one since they are identical except for the office name.

What I wanted to do is define 3 starting nodes based on combining those Lucene index lookups into one which captured all 3 offices.

I started just trying to get the offices to return while following the Lucene query parser syntax documentation.

For some reason my initial attempt returned neither an error nor any results:

neo4j-sh (0)$ START office=node:offices("name:'London - UK South' OR name:'Hamburg - Germany' 
              OR name:'Manchester - UK North'")  RETURN office
neo4j-sh (0)$

When I switched the quotes around it was much happier:

neo4j-sh (0)$ START office=node:offices('name:"London - UK South" OR name:"Hamburg - Germany" 
              OR name:"Manchester - UK North"')  RETURN office
==> +-------------------------------------------------------------------------+
==> | office                                                                  |
==> +-------------------------------------------------------------------------+
==> | Node[4171]{type->"office",country->"UK",name->"Manchester - UK North"}  |
==> | Node[4168]{type->"office",country->"UK",name->"London - UK South"}      |
==> | Node[4177]{type->"office",country->"Germany",name->"Hamburg - Germany"} |
==> +-------------------------------------------------------------------------+
==> 3 rows, 0 ms
==>

We can then reduce the queries which find how many offices people from ThoughtWorks Europe have worked in down to the following query:

START office=node:offices('name:"London - UK South" OR name:"Hamburg - Germany" OR name:"Manchester - UK North"') 
MATCH person-[:current_home_office]->office, person-[:member_of]->otherOffice 
RETURN distinct person.name, count(distinct(otherOffice)) AS offices
ORDER BY offices DESC
Database Neo4j Data (computing) Lucene Parser (programming language) teams Documentation Syntax (programming languages) Graph (Unix)

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Application Assessment Questions for Migration Projects
  • Essential Mobile App Security Tips for Coders in 2023: Make Your App Unhackable
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • DevOps Roadmap for 2022

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: