Subscribe via RSS Feed

Quick Glance at Neo4j Cypher Queries for Sample Graph Database

May 24, 2013 3 Comments


We have tried to show different Neo4j Cypher Queries to show different capabilities of graph database.

To start with graph database preliminary concepts, read Preliminary Concepts of Graph Database.

.We have given below the cypher queries, which are based on a model – which is described in Case Study in Graph Database in Context of Neo4j Cypher Query.

Installation -

Install Neo4J from http://www.neo4j.org/download

Starting Neo4j Server in Windows -

- neo4j.bat start

Then go to browser at the address – http://localhost:7474

Now the cypher queries, which we have tested on the Data Console which is in relation to the mentioned model.

Creating Nodes -

CREATE (user {entitytype:’user’, name : ‘bill’}) return user;

CREATE (company {entitytype:’company’, name : ‘CTS’}) return company;

CREATE (project {entitytype:’project’, name : ‘ABC Insurance’}) return project;

CREATE (skill {entitytype:’skill’, name : ‘HTML, CSS’}) return skill;

Some of the Nodes creation examples are shown here.

Creating Relationships -

START user=node(1), company=node(9) CREATE relation = user-[:WORKS_AT]->company return relation;

START user=node(7), company=node(9) CREATE relation = user-[:FOLLOWS]->company return relation;

START user=node(5), skill=node(12) CREATE relation = user-[:HAS_SKILL]->skill return relation;

START user=node(6), seconduser=node(7) CREATE relation = user-[:FRIEND]->seconduser return relation;

START user=node(6), work=node(11) CREATE relation = user-[:CURRENT_ENGADGEMENT]->work return relation;

START user=node(6), work=node(11) CREATE relation = user-[:CURRENT_ENGADGEMENT]->work return relation;

START company=node(9), project=node(11) CREATE relation = company-[:EXECUTING_UNDER]->project return relation;

Some of the relation creation examples are shown here.

Removing node -

START nullnode=node(0), DELETE nullnode; // Delete a Node

Fetching Nodes and Relationships -

start n = node(*) return n;// See all the nodes
start r = relationship(*) return r; //see all the relationships

start n = node:node_auto_index(name = ‘john’) return n;// Selecting particular user –>Selecting Data by Indexes

START n=node(*) WHERE has(n.name) RETURN n; // Selecting those nodes where we have name properties

START n=node(*) WHERE has(n.name) and (n.entitytype = ‘skill’) RETURN n; // Selecting nodes where entitytype = ‘Skill’

index –indexes // To Get the Indexes in nodes and relationships

START n=node(*) WHERE has(n.name) and n.name =~ ‘j.*’ return n; //Selecting nodes from indexes where node name starts with “j”

START n=node(*) WHERE has(n.name) and n.name =~ ‘.*j.*’ return n; //Selecting nodes from indexes where node name contains “j”

start n = node(6) match(n)–(x) return x; //return all the related nodes with node 6

start n = node:node_auto_index(name=’andrew’) match(n)–(x) return x; //return all the related nodes with node name = ‘andrew’

start n = node:node_auto_index(name=’andrew’) match(n)<–(x) return x; // return all nodes which are having inbound relationship with ‘andrew’

start n = node:node_auto_index(name=’andrew’) match(n)–>(x) return x; // return all nodes which are having outbound relationship with ‘andrew’

// Note : Here all entitytype (skill, project, user, company) to which andrew is having outbound relationship will return

START n=node:node_auto_index(name=’andrew’) MATCH (n)-[:WORKS_AT]->(x) RETURN x;

//Match by relationship type and outbound nodes

START n=node:node_auto_index(name=’linda’) MATCH (n)-[:FRIEND]->(x) RETURN x;

// Here result will be ‘andrew’ who is outbound relationship to ‘linda’

START n=node:node_auto_index(name=’linda’) MATCH (n)<-[:FRIEND]-(x) RETURN x;

// Here result will be ‘john’ for whom ‘linda’ is outbound relationship

START n=node:node_auto_index(name=’andrew’) MATCH (n)-[r:WORKS_AT]->() return r;

// Match by relationship type and use an identifier

START a=node:node_auto_index(name=’andrew’) MATCH (a)-[:WORKS_AT]->(b)<-[:WORKS_AT]-(c) return a,b,c

//return Multiple relationships

// Here all the people, who are working in ‘IBM’ will be returned, as ‘andrew’ is working in ‘IBM’

START n=node(*) WHERE has(n.name) and (n.name =~ ‘john.*’) RETURN n;

// Where condition in the Query and ‘property exists in node’ clause and Like clause

START n=node(*) WHERE n.entitytype? = “skill” RETURN n;

// Default true for nodes if property is missing and all other matching nodes

START n=node(*) WHERE n.entitytype! = “skill” RETURN n;

// Only for nodes with matching nodes.  Note : “!=” and “! =” are not same

START persons=node(*), company=node:node_auto_index(name=’IBM’) WHERE has(persons.name) and (persons.entitytype=’user’) and not(persons–>company) RETURN persons;

// Those persons will be returned who are not related to ‘IBM’riend

START persons=node(*), company=node:node_auto_index(name=’IBM’) WHERE has(persons.name) and (persons.entitytype=’user’) and (persons–>company) RETURN persons;

// Those persons will be returned who are has relation to ‘IBM’ (here note the ‘WORKS_AT’ and ‘FOLLOWS’ relationship)

START persons=node(*), company=node:node_auto_index(name=’IBM’) WHERE has(persons.name) and (persons.entitytype=’user’) and (persons-[:WORKS_AT]->company) RETURN persons;

// Those persons will be returned who are has relation to ‘IBM’ (Only ‘WORKS_AT’ relationship)

START me=node:node_auto_index(name=’andrew’) MATCH me–>friend where (friend.entitytype=’user’) RETURN count(friend),friend;

// Return only those users who are outbound to ‘andrew’

START me=node:node_auto_index(name=’andrew’) MATCH me<–friend<–friend_of_friend where (friend_of_friend.entitytype=’user’) RETURN count(friend_of_friend),friend_of_friend;

// This is a ‘friend of friend’ relationship. Here ‘John’ is friend of ‘linda’ and ‘linda’ is friend of ‘andrew’

START me=node:node_auto_index(name=’andrew’),company=node:node_auto_index(name=’CTS’) MATCH me<–friend<–friend_of_friend where (friend_of_friend.entitytype=’user’) and (friend_of_friend-[:WORKS_AT]->company) RETURN count(friend_of_friend),friend_of_friend,company;

// This is a ‘friend of friend’ relationship. Here ‘John’ is friend of ‘linda’ and ‘linda’ is friend of ‘andrew’ and he is returned as he is working in ‘CTS’

START me=node:node_auto_index(name=’andrew’),company=node(8,9) MATCH me<–friend<–friend_of_friend where (friend_of_friend.entitytype=’user’) and (friend_of_friend-[:WORKS_AT]->company) RETURN count(friend_of_friend),friend_of_friend,company;

// Same Query of above for selection of all the companies

start n = node:node_auto_index(name=’UI Restructuring’) match n<–x<-[:WORKS_AT]-x1 return x1;

// Here the project is done in ‘IBM’ and nodes – ‘x1′ are working in the company;

start n = node:node_auto_index(name=’UI Restructuring’) match n<–x<-[:FOLLOWS]-x1 return x1;

// Here the project is done in ‘IBM’ and nodes – ‘x1′ are following the company;

START n=node:node_auto_index(name=’jude’) MATCH (n)-[:WORKS_AT]->x<-[:FOLLOWS]-x1 return x1;

// return all the nodes which follows the company on which ‘jude’ is working;

START n=node:node_auto_index(name=’CTS’) MATCH (n)<-[r:WORKS_AT]-() RETURN type(r), count(*);

// How much nodes are in the graph who are with ‘WORKS_AT’ in ‘CTS’

START n=node(*) RETURN distinct(n.entitytype?); // Select distinct entity types

START person=node:node_auto_index(‘name: *’) MATCH person-[?:WORKS_AT]->company where person.entitytype =’user’ RETURN person.name

// return all the nodes with ‘WORKS_AT’ relationship

START begin = node(4), end = node(7) MATCH p = begin -[*]-> end foreach(n in nodes(p) : SET n.network = true);

// Updating connecting nodes and example of loop in cypher query

START begin = node(4), end = node(7) MATCH p = begin -[*]-> end return *; // showing the result of updation in connecting nodes

START n=node(1) MATCH p=n-[*..]->b WHERE ANY(x in nodes(p) WHERE x.network? = true) RETURN p;

// Example of Predicates

References :

1> http://docs.neo4j.org/refcard/1.9/

2> http://java.dzone.com/articles/10-caveats-neo4j-users-should

3> http://docs.neo4j.org/chunked/milestone/cypher-query-lang.html

We have shown some of the queries, which we can use in practical applications in Graph Database. And as this is a new type of NoSql Database, the above post is an introduction for some of the neo4j cypher query examples from our end.

We will try to put the performance and optimisation related issues in Neo4j in our later articles.

So keep reading. Comments are welcome.

Enter your email address:

Delivered by FeedBurner

Sign Up to read the rest of the content

Email will be used only for updates of our site

No Thanks