public
Last active

Cypher Queries

  • Download Gist
Cypher.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
ExecutionEngine engine = new ExecutionEngine(graphDB);
 
String query = "start n=node:Person(name={name})
match n-[:ACTS_IN]->movie<-[:ACTS_IN]-friend
return friend";
ExecutionResult result = engine.query(query, map("name", "Keanu");
 
for (Map<String,Object> row : result) {
Node friend = row.get("friend");
}
 
Iterator<Node> friends = result.columnAs("friend");
 
for (Node friend : friends) {
friend.getProperty("name");
}
CypherDSL.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Execute q = start(node("john", john)).
match(path().from("john").out("friend").link().out("friend").to("fof")).
returns(properties("john.name", "fof.name"));
ExecutionResult result = engine.execute( q.toString() ).toString();
 
 
// With QueryDSL integration
 
class Person {
String firstName;
int age;
}
 
// QPerson generated by QueryDSL build step
 
QPerson person = QPerson.person;
Assert.assertEquals( "START person=node(1,2,3) WHERE person.firstName=\"P\" and person.age>25 RETURN person",
CypherQueryDSL.start( node( "person", 1, 2, 3 ) )
.where( person.firstName.eq( "P" ).and( person.age.gt( 25 ) ) )
.returns( nodes( "person" ) )
.toString() );
CypherSpringDataNeo4j.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
public interface MovieRepository extends GraphRepository<Movie> {
@Query("start user=node:User({0}) match user-[r:RATED]->movie return movie order by r.stars desc limit 10")
Iterable<Movie> getTopRatedMovies(User uer);
}
 
 
interface MovieRepository extends GraphRepository<Movie> {
@Query("
start user=node({0})
match user-[:FRIEND]-friend-[r:RATED]->movie
return movie
order by avg(r.stars) desc, count(*) desc
limit 10
")
Iterable<Movie> recommendMovies(User me);
}
 
@NodeEntity
public class Group {
@Query(value = "start n=node({self}) match (n)-[r]->(friend) where r.type = {relType} return friend",
params = {"relType", "FRIEND"})
private Iterable<Person> friends;
}
cypher-neo4j.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# neo4j.rb
 
q = Neo4j._query("START n=node(42) RETURN n")
q.first(:n) #=> the @node
q.columns.first => :n
 
 
# neo4j.rb Cypher DSL
 
Neo4j.query do
start(n=node(3,4,5))
where(n[:name] == 'kalle')
ret n
end
cypher-neography.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# neography
 
@neo.execute_query("start n=node(id) return n", {:id => 3})
 
# batching
 
def batch(prepared)
params = prepared.map { |p| [:execute_query, p[:query], p[:params]] }
@neo.batch(*params)
end
 
batch([{:query => "create n={name : {name}} return n", :params => {:name => "Foo"}},
{:query => "create n={name : {name}} return n", :params => {:name => "Bar"}},
{:query => "create n={name : {name}} return n", :params => {:name => "Foobar"}}
])
cypher.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
; neocons
(ns neocons.docs.examples
(:require [clojurewerkz.neocons.rest :as nr]
[clojurewerkz.neocons.rest.nodes :as nn]
[clojurewerkz.neocons.rest.relationships :as nrl]
[clojurewerkz.neocons.rest.cypher :as cy]))
 
(defn -main
[& args]
(nr/connect! "http://localhost:7474/db/data/")
(let [amy (nn/create {:username "amy" :age 27})
bob (nn/create {:username "bob" :age 28})
_ (nrl/create amy bob :friend {:source "college"})
res (cy/tquery "START x = node({ids}) RETURN x.username, x.age" {:ids (map :id [amy bob])})]
(println res)))
cypher.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// readify neo4jclient
 
return graphClient.RootNode
.StartCypher("root")
.Match("root-[:BELONGS]->(user)")
.Return<SimpleResultDto>("user")
.Results
.OrderBy(u => u.Username);
 
 
[Test]
public void WhereBooleanOperations()
{
// http://docs.neo4j.org/chunked/1.6/query-where.html#where-boolean-operations
// START n=node(3, 1)
// WHERE (n.age < 30 and n.name = "Tobias") or not(n.name = "Tobias")
// RETURN n
 
var client = Substitute.For<IGraphClient>();
var query = new CypherFluentQuery(client)
.Start("n", (NodeReference)3, (NodeReference)1)
.Where<FooNode>(n => (n.Age < 30 && n.Name == "Tobias") || n.Name != "Tobias")
.Return<object>("n")
.Query;
 
Assert.AreEqual("START n=node({p0}, {p1})\r\nWHERE (((n.Age < {p2}) AND (n.Name = {p3})) OR (n.Name != {p4}))\r\nRETURN n".Replace("'","\""), query.QueryText);
Assert.AreEqual(3, query.QueryParameters["p0"]);
Assert.AreEqual(1, query.QueryParameters["p1"]);
Assert.AreEqual(30, query.QueryParameters["p2"]);
Assert.AreEqual("Tobias", query.QueryParameters["p3"]);
Assert.AreEqual("Tobias", query.QueryParameters["p4"]);
}
cypher.php
PHP
1 2 3 4 5 6 7 8
// Neo4jPHP
 
$queryString = "START n=node({start}) ".
"MATCH (n)<-[:KNOWS]-(x)".
"WHERE x.name = {name}".
"RETURN x";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryString, array('start' => 1, 'name' => 'Bob'));
$result = $query->getResultSet();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.