I am using neo4j, I have nodes with two properties: name and id. I have an index on id.
I have relationships "CALL" with a property: "by_test". This property can take different value (id of any node).
Two nodes can have multiple CALL relationships with different by_test property value.
So let's say I have 1..N nodes linked by the same CALL.by_test property value.
Node1 -> Node2 -> Node3 -> .. -> Node N
How can I get all these nodes?
Do I need to put an Index on the relationship?
Do I have to create dynamic relationship? Instead of CALL.by_test=value, use value has a relationship.
Thanks!
Using Cypher, you could query for that list like this:
START n=node:node_auto_index(name="one")
MATCH p=(n)-[r:CALL*1..]->(m)
WHERE ALL(x in r WHERE x.by_test = 3)
RETURN n,m
In the MATCH you bind a term r to the CALL relationships, which you then use in the WHERE clause to check the by_test property of each.
As Michael Hunger noted, the r is a collection of relationships, so the WHERE needs to use ALL to check each of the relationships.
Related
I have the following query that returns about 6000 nodes:
MATCH (:Term {ontology_id:"CO_330"})<-[*]-(op:Term) RETURN op
It basically returns all the nodes for ontology CO_330. Each node has also a property called term_id which is unique. Nodes are related to each other in different ways through term_id.
How can I include in the result of my query for each node an array of the IDs of its direct parents?
To get the ids of the Terms with an incoming relationship to the "CO_330" Term(s):
MATCH (t:Term {ontology_id:"CO_330"})<--(op:Term)
RETURN t, COLLECT(op.term_id) AS parentIds
Direct parents could be added with a construct similar to this
RETURN op,
[(op)-[:HAS_PARENT]->(n) | n.term_id] AS arrayOfIds
I want to find all those nodes that are connected to specific node based upon certain relationship label. This is what I have got so far,
Match (n:UMLSConcepts{ConceptID: 'C3254924'})-[:NDFRT {RelationLabel : "RO_may_be_treated_by"}]-> (m:UMLSConcepts) RETURN (n), (m)
where (n) is a specific node and (m) are all other nodes that are connected with this node. Problem with above statement is that it is dependent upon a specific relation type in this case(NDFRT), but I want all relation type that contains certain relationship properties in this case("RO_may_be_treated_by") because there could be path/edge in other relation type as well.
If I I fully understood your question, you can simply omit the relationship type in your Cypher query, this way:
Match (n:UMLSConcepts{ConceptID: 'C3254924'})-[{RelationLabel : "RO_may_be_treated_by"}]-> (m:UMLSConcepts)
RETURN (n), (m)
Lets say i have these nodes with these relations (A,B) are nodes and Rs are Relation Names:
A-R1->B
A-R2->B
A-R3->B
Now i do not actually know if one or any R has relation between these 2 nodes. how can i specify if any relation exists between these 2 nodes regardless of knowing what relation is it?
Also if there are any relation exists between these two nodes is it possible to know what relation is it?
how can i specify if any relation exists between these 2 nodes
regardless of knowing what relation is it?
I believe that a simple MATCH will be sufficient. The below query returns all relationships between a node named "A" and a node named "B", if exists.
MATCH ({name : "A"})-[r]->({name : "B"})
RETURN r
Also if there are any relation exists between these two nodes is it
possible to know what relation is it?
The type() function returns a string representation of the relationship type. Then the below query will return the string representing the type of each relationship between A and B.
MATCH ({name : "A"})-[r]->({name : "B"})
RETURN type(r) as type
Say I have matched a collection of relationships:
MATCH a-[r:BELONGS_TO]->b
How can I iterate through each relationship and assign it an index? In pseudocode:
for i in range(0, # of r's)
r.order = i
This should work:
MATCH (a)-[r:BELONGS_TO]->(b)
WITH collect(r) as rels
WITH rels, range(0, size(rels)) AS is
UNWIND is AS i
WITH rels[i] as rel, i
SET rel.order = i
You can hack it a bit :
MATCH (a)-[r:BELONGS_TO]->(b)
WITH collect(r) as relationships
UNWIND range(0, size(relationships)-1) as x
RETURN relationships[x]
Depending on your requirements, you may be able to use the "internal" ID that the neo4j DB automatically assigns to every relationship (and node). Although every internal ID is unique, after a relationship (or node) is deleted, its internal ID can be reused by a new relationship (or node). Over time, the set of active internal IDs for relationships (and nodes) may no longer have a 0 origin and may not have contiguous integer values.
If the internal ID is adequate for your needs, then there is no need to add your own id or index property.
You can use the ID() function to get the internal ID whenever you need it. For example, this is how you'd get the internal ID of every BELONGS_TO relationship:
MATCH ()-[r:BELONGS_TO]->()
RETURN ID(r);
I am using a graph database having many nodes connected to a super node by a relationship TYPE. And each of those sub nodes have properties via a FROM relationship.
How should i access the properties of individual nodes in cypher query?
I tried this but its not working.
start a=node(2) match (a)<-[:TYPE]-(node) match (node)<-[:FROM]-(prop) where prop.name="ABC" return node;
Here i have to return a node with a property name whose value is ABC!?
How should i correct it?
START a=node(2)
MATCH (a)<-[:TYPE]-(node)<-[:FROM]-(prop)
WHERE prop.name="ABC"
RETURN node;
You could also do this using indexes:
START a=node:properties('name:ABC')
MATCH (node)<-[:FROM]-(a)
Where 'properties' is your index which indexes name as 'ABC'. Realize that your above query is only interested in the node related with :FROM, therefore searching in the first example could be very long compared to simply using indexing.
If you are using Neo4j 2.x, then you are far better off with labels.
MATCH (a:Property(name='ABC'))-[:FROM]->(node)
RETURN node
Where you would set the label 'Property' when you create/update a property node.