Skip to content

Instantly share code, notes, and snippets.

@MartinBodocky
Forked from cskardon/neo4j-fsharp-basics.fs
Created January 3, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinBodocky/96c0a688f79385d89db3 to your computer and use it in GitHub Desktop.
Save MartinBodocky/96c0a688f79385d89db3 to your computer and use it in GitHub Desktop.
Basic usage of Neo4j from F# (http://geekswithblogs.net/cskardon/archive/2013/11/27/using-neo4j-with-f-ndash-cypher-2.0.aspx) Check the URL for your local Neo4j server!
open System
open Neo4jClient
open System.Linq
[<CLIMutable>]
type Person = { Name:string; Twitter:string }
[<CLIMutable>]
type Knows = { How:string }
[<EntryPoint>]
let main argv =
let client = new GraphClient(new Uri("http://localhost.:7474/db/data"));
client.Connect();
let createPerson person =
client.Cypher
.Create("(p:Person {param})")
.WithParam("param", person)
.Return<Person>("p")
.Results
.Single()
let pA = createPerson { Name = "PersonA"; Twitter = "tA" }
let pB = createPerson { Name = "PersonB"; Twitter = "tB" }
let pC = createPerson { Name = "PersonC"; Twitter = "tC" }
let pD = createPerson { Name = "PersonD"; Twitter = "tD" }
let follows target source =
client.Cypher
.Match("(s:Person)", "(t:Person)")
.Where(fun s -> s.Twitter = source.Twitter)
.AndWhere(fun t -> t.Twitter = target.Twitter)
.CreateUnique("s-[:follows]->t")
.ExecuteWithoutResults()
pB |> follows pA
pC |> follows pA
pD |> follows pB
pD |> follows pC
let knows target (details : Knows) source =
client.Cypher
.Match("(s:Person)", "(t:Person)")
.Where(fun s -> s.Twitter = source.Twitter)
.AndWhere(fun t -> t.Twitter = target.Twitter)
.CreateUnique("s-[:knows {knowsData}]->t")
.WithParam("knowsData", details)
.ExecuteWithoutResults()
pB |> knows pC {How = "colleagues"}
let pAfollowers =
client.Cypher
.Match("n<-[:follows]-e")
.Where(fun n -> n.Twitter = "tA")
.Return<Person>("e")
.Results
.Select(fun x -> x.Name)
for follower in pAfollowers do
printfn "%s" follower
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment