Skip to content

Instantly share code, notes, and snippets.

@MartinBodocky
Last active February 4, 2019 11:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MartinBodocky/5da69aebcb85421363df to your computer and use it in GitHub Desktop.
Save MartinBodocky/5da69aebcb85421363df to your computer and use it in GitHub Desktop.
F# sample for Neo4j - Friends
#r "../packages/Neo4jClient.1.1.0.28/lib/net45/Neo4jClient.dll"
#r "../packages/Newtonsoft.Json.6.0.4/lib/net45/Newtonsoft.Json.dll"
open System
open System.Collections.Generic
open System.Linq
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Linq.RuntimeHelpers
open System.Linq.Expressions
open Neo4jClient
open Neo4jClient.Cypher
open Newtonsoft.Json
type Person() =
[<JsonProperty("name")>]
member val Name : string = "" with get,set
type Social() =
let mutable _graphClient = Unchecked.defaultof<IGraphClient>
do
_graphClient <- new GraphClient(new Uri("http://localhost:7474/db/data"), "neo4j", "FexGames9")
_graphClient.Connect()
member x.Setup() =
let people =
[|
[|"Jim" ; "Mike"|]
[|"Jim" ; "Billy"|]
[|"Anna" ; "Jim"|]
[|"Anna" ; "Mike"|]
[|"Sally" ; "Anna"|]
[|"Joe" ; "Sally"|]
[|"Joe" ; "Bob"|]
[|"Bob" ; "Sally"|]
|]
let query = _graphClient.Cypher.Unwind(people, "pair")
.Merge("(u1:Person { name: pair[0] })")
.Merge("(u2:Person { name: pair[1] })")
.Merge("(u1)-[:KNOWS]->(u2)")
printf "%s" query.Query.QueryText
query.ExecuteWithoutResults()
member x.FriendsOfAFriend (person : Person) =
let query =_graphClient.Cypher
.Match("(p:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf)")
.Where( fun (p : Person) -> p.Name = person.Name)
.AndWhere("NOT (p)-[:KNOWS]-(foaf)")
.Return(fun (foaf : ICypherResultItem) -> foaf.As<Person>())
printfn "%s" query.Query.QueryText
query.Results
member x.CommonFriends(p1: Person, p2: Person) =
let query = _graphClient.Cypher.Match("(p:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf:Person)")
.Where(fun(p:Person) -> p.Name = p1.Name)
.AndWhere(fun(foaf:Person) -> foaf.Name = p2.Name)
.Return(fun (friend : ICypherResultItem) -> friend.As<Person>())
printfn "%s" query.Query.QueryText
query.Results
member x.ConnectingPaths(person1: Person, person2: Person) =
let query = _graphClient.Cypher.Match("path = shortestPath((p1:Person)-[:KNOWS*..6]-(p2:Person))")
.Where(fun(p1 : Person) -> p1.Name = person1.Name)
.AndWhere(fun (p2 : Person) -> p2.Name = person2.Name)
.Return(fun (_ : ICypherResultItem) -> Return.As<IEnumerable<string>>("[n IN nodes(path) | n.name]"))
printfn "%s" query.Query.QueryText
query.Results.Single()
let social = new Social()
// execute setup
social.Setup()
let p1 = Person()
p1.Name <- "Joe"
let p2 = new Person()
p2.Name <- "Sally"
// friends of friends
let friendsOfFriends = social.FriendsOfAFriend(p1)
printfn "Joe's friends (of friends)"
friendsOfFriends |> Seq.toList |> List.iter(fun f -> printfn "%s" f.Name)
// common friends
let commonFriends = social.CommonFriends(p1,p2)
printfn "Joe and Sally's common friends"
commonFriends |> Seq.toList |> List.iter(fun f -> printfn "%s" f.Name)
// connecting paths
let connectingPaths = social.ConnectingPaths(p1,p2)
printfn "Path to Billy"
connectingPaths |> Seq.toList |> List.iter(fun f -> printfn "%A" f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment