fetch path based upon relationship propertty in NEO4j - neo4j

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)

Related

Neo4j - Match then merge statement with a relationship not creating new nodes

When I try to add new nodes with labels (Phone, Name) that don't exist yet with a relationship to an existing node the nodes and relationships are not being created.
This is the before and after state with one existing relationship:
MATCH (n:Identity)-[a:ATTR]->(attr) RETURN *
And this is the mutation query:
MATCH (n:Identity {id:'4a028061-8dde-4f64-80c9-ae048e3f81fc'})
MERGE (n)-[na:ATTR]->(name:Name {val: 'John Smith'})
MERGE (n)-[pa:ATTR]->(p:Phone {val:2326410083})
RETURN *
I know this question is similar to Neo4J - Merge statement not creating new nodes with a relationship but, in my case, I am using filters on the nodes. What's am I missing?
Looks like the :Identity node you're trying to match to doesn't exist in the graph.
From your query, you're looking for an identity node with id:'4a028061-8dde-4f64-80c9-ae048e3f81fc', but in the graph image you supplied, if we assume the cut off caption is the id of the identity node, we can tell it starts with:'44b7'
Now it could be that the node you're trying to match on does exist, but doesn't have any outgoing :ATTR relationship (that would explain why it wouldn't be returned by your query), but that's unconfirmed. Does a node with id:'4a028061-8dde-4f64-80c9-ae048e3f81fc' exist in your graph? You haven't shown us that, and if no such node exists then the match would fail, and then there are no rows left for the remaining MERGEs to execute upon.

Get nodes with or without relationship in neo4j 3.0

Like I have a node by label person
Then there are some alternate nodes attached to it by some relation for the time being I don't know the relation. What I want through report is to know the orphaned nodes in neo4j i.e those that don't have relationship with the Person node or if their relation status property is inactive so its of no use.
I want to create that report in order to remove the orphaned/unused nodes
I have to create a report that should include the following types of nodes.
1. Get All active Alternate Nodes means nodes that have status as Confirmed.
a. Which do not have any incoming relationships.
Or
b. No active incoming relationships means the status property of relationship is not confirmed.
The case is I don't know what relationship is between them I have to just check with or without relationship
I tried cypher query but didn't worked for unknown relationship
I don't see your graph so I created a sample. See below.
If you want to collect all Alona and Inactive nodes, then you can write your query as:
match (n)
where not (n) <--()
and (n.Status = 'Confirmed' or n.Status != 'Confirmed')
return n
which simplifies into:
match (n)
where not (n) <--()
return n
Result:

MERGE Nodes by a Property field

I have different nodes that share one same property field, i need to merge these nodes into one and in the same time copy all the rest of the other properties in the merge node.
example:
(n1,g,p1) (n2,g,p2) (n3,g,p3) =>(n,g,p1,p2,p3)
Important to Note that i don't need the apoc solutions since user defined functions dosen't work in CAPS that i m working at
update :
geohash is the field that have a repeated values, so i want to merge the nodes by this field .
The CAPS team gave me this cypher query to have distinct geohash nodes from the intial graph:
CATALOG CREATE GRAPH temp {
FROM GRAPH session.inputGraph
MATCH (n)
WITH DISTINCT n.geohash AS geohash
CONSTRUCT
CREATE (:HashNode {geohash: geohash})
RETURN GRAPH
}
, however it still missing is the collect of the rest of the properties on the merged nodes.
I haven't a problem for the relationship ,cause we can copy them later from the intial graph:
FROM GRAPH inputGraph
MATCH (from)-[via]->(to)
FROM GRAPH temp
MATCH (n), (m)
WHERE from.geohash = n. AND AND to.geohash = m.geohash
CONSTRUCT
CREATE (n)-[COPY OF via]->(m)
RETURN GRAPH
It's not 100% possible in pure cypher, that's why there is an APOC procedure for that.
To merge two nodes , you have to :
create the merge node with all the properties
to create all the relationship of the nodes on the merge one
For the first part it's possible in cypher. Example :
MATCH (n) WHERE id(n) IN [106, 68]
WITH collect(n) AS nodes
CREATE (new:MyNode)
with nodes, new
UNWIND nodes as node
SET new += properties(node)
RETURN new
But for the second part, you need to be able to create relationship with a dynamic type and dynamic direction, and this is not allowed in cypher ...

NEO4J nodes under a node filter by relationship

How can we add a relationship to the query.
Say A-[C01]-B-[C02]-D and A-[C01]-B-[C03]-E
C01 C02 C03 are relationship codes I want to get output
B E
because I want only nodes that can be reached unbroken by C01 or C03
How can I get this result in Cypher?
You may want to clarify, what you're asking for seems like a very simple case of matching. You may want to provide some more info, such as node labels and how you're matching to your start nodes, since without these we have to make things up for example code.
MATCH (a:Thing)
WHERE a.ID = 123
WITH a
MATCH (a)-[:C01|C03*]->(b:Thing)
RETURN b
The key here is specifying multiple relationship types to traverse, using * for multiplicity, so it will match on all nodes that can be reached by any chain of those relationships.

return nodes with selected properties

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.

Resources