Neo4j .NET Client Wrap transactions - neo4jclient

Is it possible to wrap several transactions as one using NEO4J client .NET? My problem is that I need to delete one node, all relationships and all END nodes attached to these relationships as one ACID transaction. I understand that using REST batch is possible. Can I do it with NEO4j .NET Client?
Thank you so much for your support!

Do it all in a single Cypher call:
START n=node(123)
MATCH n-[r]->m
DELETE r, m, n
In C#:
graphClient.Cypher
.Start(new { n = (NodeReference)123 })
.Match("n-[r]->m")
.Delete("r, m, n")
.ExecuteWithoutResults();

Transaction support will come when Neo4j 2.0, Cypher, and this update to Neo4jClient all align.

Related

Getting relationships from all node's in Neo4j

I am trying to query using Neo4j.
I would like to print result of obtaining information while AUTO-COMPLETE is ON in Neo4j.
For example, suppose query that creating 3 nodes as shown below.
create (david:Person {name: 'david'}), (mike:Person {name: 'mike'}), (book:Book {title:'book'}), (david)-[:KNOWS]->(mike), (david)-[:WRITE]->(book), (mike)-[:WRITE]->(book)
Here are 2 images:
Auto-complete on
Auto-complete off
Figure is shown after query, and I would like to obtain all relating node’s relationships based on starting node ('book' node).
I used this query as shown below.
match (book:Book)-[r]-(person) return book, r, person
Whether AUTO-COMPLETE is ON or OFF, I expect to obtain all node’s relationships including “David knows Mike”, but system says otherwise.
I studied a lot of Syntax structure at neo4j website, and somehow it is very difficult for me. So, I upload this post to acquire assistance for you.
You have to return all the data that you need yourself explicitly. It would be bad for Neo4j to automatically return all the relationships for a super node with thousands of relationships for example, as it would mean lots of I/O, possibly for nothing.
MATCH (book:Book)-[r]-(person)-[r2]-()
RETURN book, r, person, collect(r2) AS r2
Thanks to InverseFalcon, this is my query that works.
MATCH p = (book:Book)-[r]-(person:Person)
UNWIND nodes(p) as allnodes WITH COLLECT(ID(allnodes)) AS ALLID
MATCH (a)-[r2]-(b)
WHERE ID(a) IN ALLID AND ID(b) IN ALLID
WITH DISTINCT r2
RETURN startNode(r2), r2, endNode(r2)

Find Nodes with the same properties in Neo4J

I have two datasets in Neo4J. I would like to find all nodes within these two datasets that have the same particular property. This is using Cypher code.
I am currently using:
MATCH n=node(*), m=node(*)
WHERE (n.name) AND (m.name) AND
n.name=m.name
RETURN n, m
In the hope to get a result showing all nodes with the same name.
I am aware of this old 2013 post here: neo4j find all nodes with matching properties
But the Cypher code has been significantly updated since this date.
Any help would be great thanks.
There are no tables in Neo4j
create index on :LabelA(propertyA);
create index on :LabelB(propertyB);
MATCH (a:LabelA)
MATCH (b:LabelB)
WHERE b.propertyB = a.propertyA
RETURN a,b;

neo4j merge 2 or multiple duplicate nodes

I am feeding my neo4j db manually using cypher, so prone to error like creating duplicate nodes:
The duplicate nodes will have each relationships to other nodes.
Is there a built-in function to merge these nodes? Or should I do it manually?
Sounds possible, but complicated with cypher script:
Get the relationships of each duplicate node
Recreate them (with their properties) with the correct node (given node id)
Remove relationships to the duplicate nodes
and finally remove the duplicate nodes.
To avoid this situation in the future, please look at the MERGE keyword in Cypher.
Unfortunately, as far as I know, there is nothing in Cypher (yet) like:
MATCH (n:MyNode),(m:MyNode)
WHERE ID(n) <> ID(m) AND
PROPS(n) IN PROPS(m) AND PROPS(m) IN PROPS(n)
(...) DELETE (...)
The fictional function PROPS of the third line is not part of Cypher language and User-Defined functions have not made it yet into Neo4j.
If you're not working with production instances, the easiest is probably to back up your data folder and try to start the insertion over (with MERGE).
Otherwise, you can also try writing a traversal to collect the duplicates and delete them in batch (here is an example with the REST API).
Try this:
MATCH (n:MyNode),(m:MyNode),(o:OtherNode {id:123})
WHERE n <> m
MATCH (m)-[r:FOO]->()
CREATE (n)-[r2:FOO]->(o)
SET r2 = r
DELETE r,m
I think you can try:
apoc.refactor.mergeNodes(nodes, options)
For relations:
apoc.refactor.mergeRelationships(rels, options)
Or:
apoc.periodic.iterate(query, options)

How to delete large amount of nodes in cypher

I'm trying to delete 1 million nodes in cyphper at one query using web admin(i.e localhost:7474/browser).
These nodes is labeled as User. I ran following query, then returned Unknown error after waiting about 1minutes.
match (u:User) delete u
This query returned Unknown error every time. and I confirm my PC resources didn't lack.
I'm using Neo4j version 2.0.0 RC1 community edition. and Neo4j Hosted on local.
Is My trying way for deletion nodes wrong?
Thanks
You should do write operations with a reasonable transaction size of ~10-50k atomic operations. Therefore you can use limit and run the statement until all users are gone:
match (u:User) with u limit 1000 delete u
With Neo4j 3.x and forward you can run large delete transactions using APOC too:
call apoc.periodic.iterate("MATCH (u:User) return u", "DETACH DELETE u", {batchSize:1000})
yield batches, total return batches, total
I've found that just removing the neo4j/data folder is the fastest way to delete the db.

How do I find disconnected nodes on neo4j with Cypher?

I am toying with neo4j and noticed that all Cypher queries need a starting point in the START clause.
I was wondering how can I find all disconnected nodes using Cypher ?
thanks
If all your nodes are indexed (e.g. via auto-indexing) you could use an index query as a start point and then find those nodes that have no outgoing relationships.
start n=node:node_auto_index("id:*")
match n-[r?]->m
where r is null
return n
Nowadays I would rather use:
start n=node:node_auto_index("id:*")
where not (n-->m)
return n
With Neo4j v3.0+ I just use;
MATCH (n)
WHERE NOT (n)--()
RETURN n
(or variations thereof). The query is reasonably fast.
I use something like this, but only when I'm using spring-data-neo4j:
start n = node:__types__(className="com.app.entity.Model")
// match, where...
return n
Hope that helps!
You can't. Graph global queries are not possible with todays Cypher.

Resources