Created
August 30, 2012 14:48
-
-
Save jexp/3529999 to your computer and use it in GitHub Desktop.
Cypher Queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"}} | |
]) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment