How connect Result nodes Neo4j - neo4j

Neo4j has a tick box option 'connect results nodes' which i gather runs a second query to connect nodes after your initial query.
eg
MATCH (n:User)
where n.Verified = 'false'
return n
order by n.followers DESC
Limit 40
This query returns 40 nodes which are connected to each other. While this works in the Neo4j browser, I cant quite get it to connect in Neo4j bloom. So question is whats the second query thats run to connect the result nodes under the hood?
Thanks

For anyone who falls into the same problem. The answer is a subquery which checks if node ids are in the original set. In the first query you return a list of node ids using the built in ID function, then collect the nodes. In the subquery you unwind the nodes and in the subquery where clause filter the using the list of IDs.
Match (b:User)
where b.Verified = 'false' and b.followers > 60
with collect(b) as users, collect(ID(b)) as listUsers
CALL{
with users,listUsers
unwind users as x
match(x)-[r]-(c:User)
where ID(c) in listUsers
return x,r,c
}
return x,r,c

Related

"Query Optimization" : Neo4j Query builds a cartesian product between disconnected patterns -

I’m supposed to have graph of multiple nodes(more than 2) with their relationships at 1st degree, second degree, third degree.
For that right now I am using this query
WITH ["1258311979208519680","3294971891","1176078684270333952",”117607868427845”] as ids
MATCH (n1:Target),(n2:Target) WHERE n1.id in ids and n2.id in ids and n1.id<>n2.id and n1.uid=103 and n2.uid=103
MATCH p = ((n1)-[*..3]-(n2)) RETURN p limit 30
In which 4 nodes Id’s are mention in WITH[ ] and next [*..3] it is used to draw 3rd degree graph between the selected nodes.
WHAT the ABOVE QUERY DOING
After running the above query it will return the mutual nodes in case of second degree [*..2] if any of the 2 selected nodes have mutual relation it’ll return.
WHAT I WANT
*1) First of all I want to optimize the query, as it is taking so much time and this query causing the Cartesian product which slow down the query process.
2) As in this above query if any 2 nodes have mutual relationship it will return the data, I WANT, the query will return mutual nodes attached with all selected nodes. Means if we have some nodes in return, these nodes must have relation to all selected target nodes.
Any suggestions to modify the query, to optimize the query.
If you are looking for to avoid the cartesian product issue with the given query
WITH ["1258311979208519680","3294971891","1176078684270333952",”117607868427845”] as ids
MATCH (n1:Target),(n2:Target) WHERE n1.id in ids and n2.id in ids and n1.id<>n2.id and n1.uid=103 and n2.uid=103
MATCH p = ((n1)-[*..3]-(n2)) RETURN p limit 30
I suggest to use this one below
MATCH (node1:Target) WHERE node1.id IN ["1258311979208519680","3294971891","1176078684270333952"]
MATCH (node2:Target) WHERE node2.id IN ["1258311979208519680","3294971891","1176078684270333952"]
and node1.id <> node2.id
MATCH p=(node1)-[*..2]-(node2)
RETURN p
It will remove the cartesian product issue.
Try this..

Finding nodes not matching query in cypher

We have build up a NEO4j database with sample network data in it. We are currently trying to find a query to find NOT matching nodes.
We have 10 nodes in database and we get 8 of them by using the following query:
match(n:nodeType)-[r:binded]-(m.nodetype2)-[z:binded]-(t:nodeType) where n.workingMode='Working' and t.workingMode='Protection' return n
What we want is finding the two nodes which does not satify above condition.
I have found some entires mentioning the usage of NOT x IN y and tried a few solutions including
match(a) where a.workingMode ='Working'
match(n:nodeType)-[r:binded]-(m.nodetype2)-[z:binded]-(t:nodeType) where n.workingMode='Working' and t.workingMode='Protection'
and NOT a.id IN n.id
return a
but that returns 10 and others i have tried provided either 10 or 0 results.
Thanks in advance
You can pass the result of your first statement to the second statement of the query as a list and then use NOT IN to exclude these nodes:
MATCH(n:nodeType)-[r:binded]-(m.nodetype2)-[z:binded]-(t:nodeType)
WHERE n.workingMode='Working' AND t.workingMode='Protection'
WITH collect(n) as ns
MATCH (a:nodeType)
WHERE a.workingMode ='Working' AND NOT a IN ns
RETURN a

Visualize connected components in Neo4j

I can find the highest densely connected component in the graph using the code below:
CALL algo.unionFind.stream('', ':pnHours', {})
YIELD nodeId,setId
// groupBy setId, storing all node ids of the same set id into a list
MATCH (node) where id(node) = nodeId
WITH setId, collect(node) as nodes
// order by the size of nodes list descending
ORDER BY size(nodes) DESC
LIMIT 1 // limiting to 3
RETURN nodes;
But it does not help me visualize the topmost densely connected component (sub-graph) because the output graph it emits are disjoint nodes. Is it possible to visualize the densely connected component. If yes, then how
I tried this query but I am getting different the result.
I haven't used these algorithms and I don't know much about it, but I think you added an extra character (colon) in the query.
Can you check with pnHours instead of :pnHours.
I remove colon(:) from the query and I am getting the proper result (also I am able to get the relationships as well because Neo4j browser fetches it although it's not specified in the query).
If you still don't get check the following query:
CALL algo.unionFind.stream('', 'pnHours', {})
YIELD nodeId,setId
// groupBy setId, storing all node ids of the same set id into a list
MATCH (node) where id(node) = nodeId
WITH setId, collect(node) as nodes
// order by the size of nodes list descending
ORDER BY size(nodes) DESC
LIMIT 1 // limiting to 3
WITH nodes
UNWIND nodes AS node
MATCH (node)-[r:pnHours]-()
RETURN node,r;
If you want to visualize then in Neo4j browser then use:
CALL algo.unionFind.stream('', ':pnHours', {})
YIELD nodeId,setId
// groupBy setId, storing all node ids of the same set id into a list
MATCH p=(node)-->() where id(node) = nodeId
WITH setId, collect(p) as paths
// order by the size of nodes list descending
ORDER BY size(paths) DESC
LIMIT 1 // limiting to 3
// Maybe you need to unwind paths to be able to visualize in Neo4j browser
RETURN paths;
It is not the most optimized query but should do just fine on small datasets.
The following query should return all the single-step paths in the largest pnHours-connected component (i.e., the one having the most nodes). It only gets the paths for the largest component.
CALL algo.unionFind.stream(null, 'pnHours', {}) YIELD nodeId, setId
WITH setId, COLLECT(nodeId) as nodeIds
ORDER BY SIZE(nodeIds) DESC
LIMIT 1
UNWIND nodeIds AS nodeId
MATCH path = (n)-[:pnHours]->()
WHERE ID(n) = nodeId
RETURN path
The neo4j browser's Graph visualization of the results will show all the nodes in the component and their relationships.

How to do traversal in neo4j with cypher queries?

What I'm trying to do is simply start at a node and search for all connected nodes that are a certain label. However I don't want to return the start node. How would I do this?
Example:
...<-[:parent]<-anode<-[created]-user-[created]->anode-[:parent]->anode-....->nodes...
What I would like to do is start at the user node and return all relationships but excluding the user node.
This will return you a list of all nodes connected via created relationships of a distance of up to 10.
MATCH user-[:created*1..10]->(anode:CertainLabel)
RETURN DISTINCT anode
Depending on your graph, you may be able to get rid of the 10, but if it's large and complex removing the max value could cause your query to run very slowly
This is along the lines of what I was looking for.
START u = node(26)
MATCH (u)-[rels*1..10]->(node) unwind rels as r
RETURN DISTINCT id(startNode(r)),endNode(r)

Neo4j return nodes indirectly connected to each other with merged relationship

I have a dataset where the following query
returns the whole topology.
MATCH (na:node)-[ra:composition]-(ia:interface)-[rb:compound]-(ib:interface)-[rc:composition]-(nb:node)
RETURN na,ia,ib,nb
LIMIT 1000
I would like to merge ia and ib into only one relationship
so that I will only get nodes connected to each other and not their
intermediary interfaces.
Like this:
(na:node)-[r:CONNECTED_TO]-(nb:node)
Anyone know how?
I'm not completely sure if I correctly understand what you want to do, but shooting from the hip:
MATCH (na:node)-[ra:composition]-(ia:interface)-[rb:compound]-(ib:interface)-[rc:composition]-(nb:node)
WITH na,nb
LIMIT 1000
MERGE (na)-[:CONNECTED_TO]-(nb)
RETURN count(*)
Run this query until count equals 0

Resources