Skip to content

mumez/SCypherGraph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SCypherGraph

High-level object wrapper of Neo4j graph database using SmallBolt and SCypher

Installation

Currently Pharo 8 and Pharo 9 are supported.

Metacello new
  baseline: 'SCypherGraph';
  repository: 'github://mumez/SCypherGraph:main/src';
  load.

Examples

Please see Interacting with Neo4j from Pharo Smalltalk for details.

Basic

db := SgGraphDb default.
db settings username: 'neo4j'; password: 'neoneo'.
db allLabels. "Get all node labels"

"Print 'Movie' node properties"
(db nodesLabeled: 'Movie') 
   do: [ :each | self traceCr: each properties ].

Get node with where:

matrix := (db nodesLabeled: 'Movie' where: [:each | each @ 'title' = 'The Matrix']) first.
matrix properties.

Get relationships

matrix inRelationships.
matrix outRelationships.

(matrix inRelationshipsTyped: 'ACTED_IN')
  collect: [:each | each endNode @ 'name']. 

Get relationships with where:

(matrix inRelationshipsTyped: 'ACTED_IN' where: [ :start :rel :end | (rel @ 'roles') = #('Neo') ])
  collect: [ :each | each endNode properties ].

Create nodes

sf := db mergeNodeLabeled: 'Genre' properties: {'name'->'SF'. 'description'->'Science Fiction'}.
action := db mergeNodeLabeled: 'Genre' properties: {'name'->'Action'. 'description'->'Exciting Actions'}. 

Create relationships

matrixToSf := matrix relateOneTo: sf typed: 'HAS_GENRE' properties: {'score'-> 6}.
matrixToAction := matrix relateTo: action typed: 'HAS_GENRE' properties: {'score'-> 7}. 

Execute Cypher directly

db runCypher: 'UNWIND range(1, 10) AS n RETURN n*n'. "inspect it"

db runCypher: 'UNWIND range($from, $to) AS n RETURN n*n' 
   arguments: {'from'->2. 'to'->5}. "inspect it"

Execute dynamically generated Cypher

m := 'm' asCypherObject. "Movie"
p := 'p' asCypherObject. "Person"
o := 'o' asCypherObject. "Other person"

pathPattern := (p node: 'Person') - ('act1' asCypherObject rel: 'ACTED_IN' ) -> (m node: 'Movie' props: {'released'->2000})  <- ('act2' asCypherObject rel: 'ACTED_IN' ) - (o node: 'Person').

actorNameParam := 'actorName' asCypherParameter.
where := (p @ 'name') starts: actorNameParam.
"where := ((p @ 'born') > 1970) and: ((o @ 'born') > 1970). " " <= try changing"

return := (p @ 'name'), (o @ 'name'), (m @ 'title').

query := CyQuery match: pathPattern where: where return: return orderBy: (p @ 'name') skip: 0 limit: 100. "print it"

result := db runCypher: query arguments: { actorNameParam -> 'Tom' }.
(result fieldValues groupedBy: [ :each | each at: 1 ]). "inspect it"

Changing IP and port

db settings targetUri: 'bolt://127.0.0.1:7687'.

About

Object wrapper of Neo4j graph database using SmallBolt and SCypher

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published