Find Nodes with the same properties in Neo4J - 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;

Related

How to get labels of nodes of neo4j network in cytoscape

I am using cytoscape pluggin (cypher queries) to visualize neo4j graph db. I am using following query to visualize it. The output shows the 37 nodes and connections but the nodes and connections do not have any labels.
I am using following cypher query:
match (g: Gene) where g.symbol = 'TMPRSS2' or g.symbol='ACE2'
with g
match path=(g)<-[:IS_VARIANT_OF_GENE|DISEASE_ASSOCIATES_GENE]-()
return path
How can I visualize the network with all the labels.
Any help is here highly appreciated
If you want the labels from everything in that path, you could so something like:
MATCH (g:Gene) where g.symbol = 'TMPRSS2' or g.symbol='ACE2'
WITH g
MATCH (g)<-[r:IS_VARIANT_OF_GENE|DISEASE_ASSOCIATES_GENE]-(n)
return g, r, n
That should return you the nodes, relationship, and their properties. I'm not familiar with Cytoscape but without knowing anything about it, that's how I would get the properties back.

Cypher query fails with variable length paths when trying to find all paths with unique node occurences

I have a highly interconnected graph where starting from a specific node
i want to find all nodes connected to it regardless of the relation type, direction or length. What i am trying to do is to filter out paths that include a node more than 1 times. But what i get is a
Neo.DatabaseError.General.UnknownError: key not found: UNNAMED27
I have managed to create a much simpler database
in neo4j sandbox and get the same message again using the following data:
CREATE (n1:Person { pid:1, name: 'User1'}),
(n2:Person { pid:2, name: 'User2'}),
(n3:Person { pid:3, name: 'User3'}),
(n4:Person { pid:4, name: 'User4'}),
(n5:Person { pid:5, name: 'User5'})
With the following relationships:
MATCH (n1{pid:1}),(n2{pid:2}),(n3{pid:3}),(n4{pid:4}),(n5{pid:5})
CREATE (n1)-[r1:RELATION]->(n2),
(n5)-[r2:RELATION]->(n2),
(n1)-[r3:RELATION]->(n3),
(n4)-[r4:RELATION]->(n3)
The Cypher Query that causes this issue in the above model is
MATCH p= (n:Person{pid:1})-[*0..]-(m)
WHERE ALL(c IN nodes(p) WHERE 1=size(filter(d in nodes(p) where c.pid = d.pid)) )
return m
Can anybody see what is wrong with this query?
The error seems like a bug to me. There is a closed neo4j issue that seems similar, but it was supposed to be fixed in version 3.2.1. You should probably create a new issue for it, since your comments state you are using 3.2.5.
Meanwhile, this query should get the results you seem to want:
MATCH p=(:Person{pid:1})-[*0..]-(m)
WITH m, NODES(p) AS ns
UNWIND ns AS n
WITH m, ns, COUNT(DISTINCT n) AS cns
WHERE SIZE(ns) = cns
return m
You should strongly consider putting a reasonable upper bound on your variable-length path search, though. If you do not do so, then with any reasonable DB size your query is likely to take a very long time and/or run out of memory.
When finding paths, Cypher will never visit the same node twice in a single path. So MATCH (a:Start)-[*]-(b) RETURN DISTINCT b will return all nodes connected to a. (DISTINCT here is redundant, but it can affect query performance. Use PROFILE on your version of Neo4j to see if it cares and which is better)
NOTE: This works starting with Neo4j 3.2 Cypher planner. For previous versions of
the Cypher planner, the only performant way to do this is with APOC, or add a -[:connected_to]-> relation from start node to all children so that path doesn't have to be explored.)

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)

Skipping a node in a variable length relationship in cypher

I am trying to figure out how to write a Cypher query for Neo4J. I have a linked list of nodes like this:
n-[FIRST_NODE]->n-[NEXT_NODE]->n-[NEXT_NODE]->.....
The FIRST_NODE relationship has a property that says how deep in the list we should retrieve nodes. I want to retrieve a list of nodes possibly skipping one based on a property in n and retrieve x amount of nodes where x is the depth we should traverse in the list. Does this make sense?
I have come up with the below query but it doesn't work!
MATCH (x)-[firstIssue:FIRST_NODE]->(y:Type1)
MATCH (z)-[:NEXT_NODE*1..{firstIssue.Count}]->(a:Type1)
RETURN x,y,z,a
Any help would be apprectiated!.
Cypher does not support dynamic bounds for variable length paths.
However, in neo4j 3.x, you can install the APOC plugin and use the apoc.path.expand procedure. For example:
MATCH (x)-[firstIssue:FIRST_NODE]->(y:Type1)
CALL apoc.path.expand(y, 'NEXT_NODE>', '+Type1', 1, firstIssue.Count) YIELD path
RETURN x, firstIssue, path;

How can I use neo4j / Cypher to identify a path that connects from one central node/label to 3+ other node/labels?

I'm trying to use neo4j / Cypher to query a particular pattern in my neo4j database, and I can't figure out how to do it.
I'm trying to output on one line, the Company name property, the Organization name property, the collection of category node name properties (via collect(tag.name)) and the sum of the investment amount properties.
I can easily trace to/from the company node and two other distinct types of nodes, but I can't figure out how to also trace the path of the third node.
For two nodes, I use:
MATCH (t)<-[]-(c:Company)-[]->(org:Organization)
RETURN org.name, c.name, collect (t.name)
I've tried using MERGE and UNION but haven't gotten them to work. How can I extend this to include the other leg?
Okay, I figured it out thanks to guidance from Neo4j GraphGists.
What worked was:
MATCH (t)<-[]-(c:Company)-[]->(org:Organization)
WHERE c.name = "MyCompany"
MATCH (c)<-[]-(f:Funding)
RETURN c.name, org.name, collect(t.name), sum(DISTINCT f.amount)
(and specifically on line 3 it needs to be (c) instead of (c:Company)

Resources