How can I display neo4j nodes and a unique relationship type linking them?
I have a Neo4j graph, the nodes are linked with multiple relationship types (DODAG, BE, etc..) and I want to display the nodes linked with a specific relationship type without displaying the other relationship links.
When I run
MATCH p=()-[r:DODAG]->() RETURN p
I get the nodes linked by the DODAG relationship type, but the graph also displays the other relationships that link these nodes (BE, etc..)
How could I return the same result, but without the other relationships?
Thank you!
Instead of returning path, return those specific nodes and relationship only:
MATCH (a)-[rel:DODAG]->(b)
RETURN a, rel, b
Related
I have a rather large and complex graph in Neo4j (millions of nodes and relationships in various types), I want to get all child nodes (in all depths) of a specific root node, but only with a specific type of relationship
I have tried: Match (n:NODE_TYPE)-[*:REL_TYPE]->(r:NODE_TYPE {id:SPECIFIC_ID}) return n
But I get a syntax error for specifying a label on the relationship
Querying the whole graph takes a really long time without specifying the relationship type, and nodes could go through paths that will eventually lead to the root node but will use other types of relationships (which is not good for my use case)
you need to change the order of the rel type and wildcard operator:
Match (n:NODE_TYPE)-[:REL_TYPE*]->(r:NODE_TYPE {id:SPECIFIC_ID})
return n
Let's say my data include nodes of the type Person, Company and Country.
A person WORKS_AT a company and a company IS_IN a country.
CREATE (Person {name:"Paul"});
CREATE (Company {brand:"BG"});
CREATE (Country {code:"UK"});
MATCH (person {name:"Paul"}),(company {brand:"BG"}) CREATE (person)-[worksat:WORKS_AT]->(company) return person,worksat,company
MATCH (company {brand:"BG"}),(country {code:"UK"}) CREATE (company)-[isin:IS_IN]->(country) return company,isin,country
So what i want is to be able to see the person->country data in a visual graph way, in the neo4j default browser, bypassing completely the company node in between (which should not be visible).
But without creating a direct permanent relationship between the Person node and the Country node.
Thanks in advance.
You can use virtual relationships in the graphical result using APOC Procedures (these are not saved to your graph data).
Here's how this would work, provided that the nodes are labeled accordingly (your above creation queries aren't adding labels, so definitely fix that):
MATCH (p:Person)-[:WORKS_AT]->()-[:IS_IN]->(c:Country)
CALL apoc.create.vRelationship(p,'WORKS_IN',{},c) yield rel
RETURN p, rel, c
I would like to input two specific nodes and return the quantity of relationships that are along the path that connect the specific nodes.
(There is only 1 path possible in every case)
In some cases, two specific nodes are related through two relationships like this:
(Tim)-[]-()-[]-(Bill)
Should return 2 (relationships).
In other cases there are more nodes between my specific start and end nodes. Like this:
(Tim)-[]-()-[]-()-[]-()-[]-(Bill)
Should return 4 (relationships).
I have two types of relationships that could exist between nodes, so I need to avoid being specific about the type of relationship if possible.
New to this and performed an extensive search before asking this question as no one seemed to discuss relationships between specific nodes...
Many thanks for your help!
This query should work:
match p = (:Person {name:'Tim'})-[*]->(:Person {name:'Bill'})
RETURN length(p)
That is: return the length() of path p.
I want to get list of all connected nodes starting from node 0 as shown in the diagram
Based on your comment:
I want to get a list of all the connected nodes. For example in the
above case when I search for connected nodes for 0, it should return
nodes- 1,2,3
This query will do what you want:
MATCH ({id : 0})-[*]-(connected)
RETURN connected
The above query will return all nodes connected with a node with id=0 (I'm considering that the numbers inside the nodes are values of an id property) in any depth, both directions and considering any relationship type. Take a look in the section Relationships in depth of the docs.
While this will work fine for small graphs note that this is a very expensive operation. It will go through the entire graph starting from the start point ({id : 0}) considering any relationship type. This is really not a good idea for production environments.
If you wish to match the nodes that have a relationship to another node, you can use this:
MATCH (n) MATCH (n)-[r]-() RETURN n,r
It will return you all the nodes that have a relationship to another node or nodes, irrespective of the direction of the relationship.
If you wish to add a constraint you can do it this way:
MATCH (n:Label {id:"id"}) MATCH (n)-[r]-() RETURN n,r
For larger or more heavily interconnected graphs, APOC Procedures offers a more efficient means of traversal that returns all nodes in a subgraph.
As others have already mentioned, it's best to use labels on your nodes, and add either an index or a unique constraint on the label+property for fast lookup of your starting node.
Using a label of "Label", and a parameter of idParam, a query to get nodes of the subgraph with APOC would be:
MATCH (n:Label {id:$idParam})
CALL apoc.path.subgraphNodes(n, {minLevel:1}) YIELD node
RETURN node
Nodes will be distinct, and the starting node will not be returned with the rest.
EDIT
There's currently a restriction preventing usage of minLevel in subgraphNodes(), you can use either filter out the starting node yourself, or use apoc.path.expandConfig() using uniqueness:'NODE_GLOBAL' to get the same effect.
I have a graph where I have chains of nodes that have a relationship [:LINKS_TO] and I can successfully get the shortestPath function to work.
For most of my users this level of detail is fine.
I have another set of users where there is a need for a richer set of information on the relationship. Given that properties on relationships are supposed to represent strengths or scores for the relationship I have created specific nodes to hold descriptive metadata.
This means I have a pattern that says (start)-[:PARTICIPATES]-(middle)-[:REFERENCES]->(end)
There can be any number of nodes between the start and end points in the chain.
I am struggling to get the shortestPath function to return any results for the more detailed chain. Is there a way to do this using Cypher?
You could also have kept your metadata information on the relationships.
For your needs, this should work:
MATCH p = shortestPath((start)-[:PARTICIPATES|:REFERENCES*]->(end))
RETURN nodes(p)