neo4j get nodes in order of connections - neo4j

http://console.neo4j.org/r/z1iafh is it possible to return n.name in order so it would be CREATE (node_name); without adding new properties to nodes? I see that there is a sequence in which nodes in this test database are connected to each other, so i am interested is it possible to somehow het this sequence.
START n=node(*) MATCH (n)-[r:CREATE_NODE_COMMAND]->(m) RETURN n

First you need to declare a path identifier, then add depth and lastly you can order by path length :
START n=node(*) MATCH p=(n)-[r:CREATE_NODE_COMMAND*..10]->(m)
ORDER BY length(p)
LIMIT 1
RETURN nodes(p)

Related

find maximum weight edge connected to any specific node in neo4j

MATCH (d:domain)
WITH COLLECT(d) AS domains
UNWIND domains AS d1
UNWIND domains AS d2
WITH d1,d2
WHERE id(d1) < id(d2) and d1.name='google'
MATCH (d1)-[r:domain_join]-(d2)
//where r.weight is max // I want something like this (I am stuck at this line)
return d1.name,d2.name,r.weight;
The output I am getting is
The output I want is the single row having the maximum weight
You should be able to do:
return d1.name,d2.name,r.weight
ORDER BY r.weight DESC LIMIT 1;
I suspect you may be able to simplify your query (especially if you have an index on the name property for your domain nodes). By the way, labels usually start with an uppercase letter and types for relationships are all uppercased.
MATCH (google:domain {name: 'google'})
MATCH (google)-[r:domain_join]-(d2:domain)
RETURN d2.name, r.weight
ORDER BY r.weight DESC LIMIT 1

How Many Nodes Are Involved in a Match

How can I know how many nodes and edges are involved in a MATCH? Is there another way besides Explain / Profile Match?
If you mean how many nodes are matched in a path, such as a variable-length path, then you can assign a path variable for this:
MATCH p = (k:Person {name:'Keanu Reeves'})-[*..8]-(t:Person {name:'Tom Hanks'})
WITH p LIMIT 1
RETURN p, length(p) as pathLength, length(p) + 1 as numberOfNodesInPath
You can also use nodes(p) and relationships(p) to get the collection of nodes and relationships that make up the path, and you can use size() on those collections to get their size.
There exists the COUNT() function of Cypher that allows you to count the number of elements. As for example in this query:
MATCH (n)
RETURN COUNT(n);
This query will count all nodes in your database.
You can find more information in the cypher manual, under the aggregating functions. Check it out.
The following Cypher snippet should return the number of distinct nodes and relationships found by any given MATCH clause. Just replace <your code here> with your MATCH pattern.
MATCH <your code here>
WITH COLLECT(NODES(p)) AS ns, SUM(SIZE(RELATIONSHIPS(p))) AS relCount
UNWIND ns AS nodeList
UNWIND nodeList AS node
RETURN COUNT(DISTINCT node) AS nodeCount, relCount;

Neo4j: get all relations between queried nodes

I want to make a cypher query that do below tasks:
there is a given start node, and I want to get all related nodes in 2 hops
sort queried nodes by hops asc, and limit it with given number
and get all relations between result of 1.
I tried tons of queries, and I made below query for step 1, 2
MATCH path=((start {eid:12018})-[r:REAL_CALL*1..2]-(end))
WITH start, end, path
ORDER BY length(path) ASC
RETURN start, collect(distinct end)[..10]
But when I try to get relationships in queried path with below query, it returns all relationships in the path :
MATCH path=((start {eid:12018})-[r:REAL_CALL*1..2]-(end))
WITH start, end, path
ORDER BY length(path) ASC
RETURN start, collect(distinct end)[..10], relationships(path)
I think I have to match again with result of first match instead of get relationships from path directly, but all of my attempts have failed.
How can I get all relationships between queried nodes?
Any helps appreciate, thanks a lot.
[EDITED]
Something like this may work for you:
MATCH (start {eid:12018})-[rels:REAL_CALL*..2]-(end)
RETURN start, end, COLLECT(rels) AS rels_collection
ORDER BY
REDUCE(s = 2, rs in rels_collection | CASE WHEN SIZE(rs) < s THEN SIZE(rs) ELSE s END)
LIMIT 10;
The COLLECT aggregation function will generate a collection (of relationship collections) for each distinct start/end pair. The LIMIT clause limits the returned results to the first 10 start/end pairs, based on the ORDER BY clause. The ORDER BY clause uses REDCUE to calculate the minimum size of each path to a given end node.

neo4j how to use count(distinct()) over the nodes of path

I search the longest path of my graph and I want to count the number of distinct nodes of this longest path.
I want to use count(distinct())
I tried two queries.
First is
match p=(primero)-[:ResponseTo*]-(segundo)
with max(length(p)) as lengthPath
match p1=(primero)-[:ResponseTo*]-(segundo)
where length(p1) = lengthPath
return nodes(p1)
The query result is a graph with the path nodes.
But if I tried the query
match p=(primero)-[:ResponseTo*]-(segundo)
with max(length(p)) as lengthPath
match p1=(primero)-[:ResponseTo*]-(segundo)
where length(p1) = lengthPath
return count(distinct(primero))
The result is
count(distinct(primero))
2
How can I use count(distinct()) over the node primero.
Node Primero has a field called id.
You should bind at least one of those nodes, add a direction and also consider a path-limit otherwise this is an extremely expensive query.
match p=(primero)-[:ResponseTo*..30]-(segundo)
with p order by length(p) desc limit 1
unwind nodes(p) as n
return distinct n;

Determine the Longest Unidirectional Path in a Directed Disconnected Graph

I have a directed disconnected graph in which some vertices (nodes) can be connected by multiple unidirectional relationships of different types. It is possible that the graph has loops.
How can I determine the length of the longest unidirectional path in my graph? I have been trying the following query with no success:
START n=node(*)
MATCH p=n<-[rels]-m
WITH COLLECT(p) AS paths, MAX(length(p)) AS maxLength
RETURN FILTER(path IN paths
WHERE length(path)= maxLength) AS longestPaths
Thanks in advance,
What about:
MATCH p=(n)<-[:RELTYPE*]-(m)
RETURN length(p)
ORDER BY LENGTH(p) DESC
LIMIT 1
Be aware that this kind of query might be expensive depending the structure and size of your graph.
For Neo4j 1.9 use:
START n=node(*)
MATCH p=(n)<-[:RELTYPE*]-(m)
RETURN length(p)
ORDER BY LENGTH(p) DESC
LIMIT 1

Resources