How to find all oldest sibling in all families - neo4j

If I have a lot of families represented like:
(parent:Person)<-[:CHILD_OF]-(child:Person {age:19})
then how would a query look like that finds the oldest child of all families?
I have the following suggestion, but this only returns 1 node:
match (parent:Person)<--(child:Person) return child order by child.age desc limit 1

Probably can be optimized, here's a quick go at it-
match (c:Person)-[:CHILD_OF]->(p)
with p, max(c.age) as maxAge, collect(c) as children
return p,filter (x in children where x.age=maxAge)

match (parent:Person)<--(child:Person) with parent, max(child.age) as maxAge
Match (parent)<--(child:Person) where child.age = maxAge
return *
It might return several childs if they have maximum same age, if you want to return one, you should use one more creteria like
max(id(child))
and you will have
Match (parent:Person)<--(child:Person) with parent, max(child.age) as maxAge
Match (parent)<--(child:Person) where child.age = maxAge with parent, max(id(child)) as maxId
Match (parent)<--(child:Person) where id(child) = maxId return *

Related

Bidirectional recursion in neo4j

Cannot find answer online for this. I want to do a recursive query upstream and downstream on protein interaction map. If user enters a protein (protein 'C') and depth N=2, I want to return 2 upstream and 2 downstream proteins in the interaction map and the regulation. However if its upstream then protein 'b' on right side of MATCH needs to come first in the return table and if its downstream direction then protein 'a' on left side of match needs to come first in return table. How can I do this?
For instance this is the bidirection but half of the rows are the wrong order in columns 1 and 3.
MATCH p = (a:Protein { name:'C' })<-[:REGULATES*1..2]->(b:Protein)
WITH *, relationships(p) as r
RETURN nodes(p)[length(p)-1].name AS Protein1, r[length(p)-1] as Regulates, b.name AS Protein2
I can only get what I want with two calls and switching order or RETURN columns.
MATCH p = (a:Protein { name:'C' })-[:REGULATES*1..2]->(b:Protein)
WITH *, relationships(p) as r
RETURN nodes(p)[length(p)-1].name AS Protein1, r[length(p)-1] as Regulates, length(p), b.name AS Protein2
MATCH p = (a:Protein { name:'C' })<-[:REGULATES*1..2]-(b:Protein)
WITH *, relationships(p) as r
RETURN b.name AS Protein1, r[length(p)-1] as Regulates, nodes(p)[length(p)-1].name AS Protein2
Figured it out using functions startNode and endNode. The last() and head() functions are also handy.
MATCH p = (n:Protein { name:'C' })<-[:REGULATES*1..3]->(b:Protein)
WITH *, relationships(p) as rs
RETURN startNode(last(rs)).name as Protein1, last(rs).direction as Regulates, endNode(last(rs)).name as Protein2, length(p)

How to query with two conditions in Neo4j

I am new with Neo4j and I am stucked trying to get a query with two conditions, where I want to get all the "Autors" related to "Pixar" and "Fox". So far I have tried the following two ways:
MATCH (a:Autor)- [:AUTOR_DE]-> (t:Título) -[:PRODUCIDO_POR] ->( p:Productora {Nombre: "Pixar"}),
and
MATCH (a:Autor)- [:AUTOR_DE]-> (t:Título) -[:PRODUCIDO_POR] ->( p:Productora {Nombre: "Fox"}),
return a,p
and
MATCH (a:Autor)- [:AUTOR_DE]-> (t:Título) -[:PRODUCIDO_POR] ->( p:Productora)
WHERE ( (p:Productora) = "Fox" OR (p:Productora) = "Pixar")
return a,p
Thanks in advance
Assuming that a Productora node stores its name in a name property, and that every Productora node has a unique name, this should work:
MATCH (a:Autor)-[:AUTOR_DE]->(:Título)-[:PRODUCIDO_POR]->(p:Productora)
WHERE p.name = "Fox" OR p.name = "Pixar"
WITH a, COLLECT(DISTINCT p) AS ps
WHERE SIZE(ps) = 2
return a, ps
And this should also work:
MATCH (fox:Productora), (pixar:Productora), (a:Autor)
WHERE fox.name = "Fox" AND pixar.name = "Pixar" AND
(a)-[:AUTOR_DE]->(:Título)-[:PRODUCIDO_POR]->(fox) AND
(a)-[:AUTOR_DE]->(:Título)-[:PRODUCIDO_POR]->(pixar)
return a, fox, pixar

How to return distinct node properties?

MATCH (p:car)-[r]->(n:brand) where n.brandname = 'Nissan' RETURN p.sku_id
This query will return all 'sku_id' for the query. How can I only return unique 'sku_id' only for the same query?
You can use DISTINCT if many Ids are the same :
MATCH (p:car)-[r]->(n:brand)
WHERE n.brandname = 'Nissan'
RETURN DISTINCT p.sku_id
Maybe you have to be more precise :
MATCH (p:car {model:"Micra"})-[r]->(n:brand {brandname:"Nissan"})
RETURN DISTINCT p.sku_id

To get all nodes and path from edge to root using neo4j

I want to get all nodes information with paths from edge to root node.Using one of the edge property.
This is the three layer node structure.
MATCH (g:GrandChild{name:"C"})<-[:childToGrandChild]-(c:Child)<-[p:Parent*0..]-(c:Child) RETURN c,g,p
This will return only B,C nodes with relationship like this
cypher used
CREATE (p: Parent{name : '1'} ) RETURN p
MATCH (p:Parent) WHERE p.name = '1' CREATE (c: Child{name : '2'} )<-[:parentToChild]-(p) RETURN p
MATCH (c:Child) WHERE c.name = '3' CREATE (g: GrandChild {name : '2'} )<-[:childToGrandChild]-(c) RETURN c
Please help..
You have missed parentTochild relationship which will be like,
MATCH (g:GrandChild{name:"C"})<-[:childToGrandChild]-(c:Child)<-[parentToChild*0..]-(p:Parent)
RETURN c,g,p
Try this :
MATCH (g:GrandChild{name:"C"})<-[:childToGrandChild]-(c:Child)
MATCH (c)<-[p:Parent*0..]-(c2:Child)
RETURN c,c2,g,p

How to avoid duplications return distinct nodes and the relation ship using neo4j

I would like to return for a given node-id related nodes and their relationships props
For example:
-> defines a bi direction relationship with property timestamp
1234->777
777->1234
1234->999
999->1234
1234->888
888->1234
1234,777,888,999 are node-ids
When I execute this:
final PreparedStatement ps = conn.prepareStatement("start a = node(1234) match (a)-[k:nearby*]->(b) where a<>b return DISTINCT b, k");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Map result = (Map<String, Object>) rs.getObject("b");
System.out.println(result.toString());
}
} catch (SQLException e) {
e.printStackTrace();
logger.error("Error returning userId=" + userIdInput, e);
}
return null;
}
I get:
{userId=777}
{userId=999}
{userId=888}
{userId=888}
{userId=999}
{userId=999}
{userId=777}
{userId=888}
{userId=888}
{userId=777}
{userId=888}
{userId=777}
{userId=999}
{userId=999}
{userId=777}
How I do get distinct results only (777,888,999)
How to retrieve the relationship props of 1234 to the dest node? I expect to get the timestamp prop which defined on each relationship
Thank you,
ray.
I'm not sure what language you're using so I'll focus on the Cypher. Firstly I would replace the START query with a MATCH with a WHERE on ID(a):
MATCH (a)-[k:nearby*]->(b)
WHERE ID(a) = 1234 AND a<>b
RETURN DISTINCT b, k
Secondly I'm pretty sure you don't need the a<>b because Cypher paths won't loop back on the same nodes:
MATCH (a)-[k:nearby*]->(b)
WHERE ID(a) = 1234
RETURN DISTINCT b, k
Lastly, and to your question, I suspect the reason that you're getting duplicates is because you have multiple relationships. If so you can return the result node and an array of the relationships like so:
MATCH (a)-[k:nearby*]->(b)
WHERE ID(a) = 1234
RETURN collect(b), k
That should return you node/relationship objects (with properties on both). Depending on your language/library you might get Maps or you might get objects wrapping the data
If your library doesn't return the start/end nodes for relationships for you, you can do something like this:
MATCH (a)-[k:nearby*]->(b)
WHERE ID(a) = 1234
RETURN collect({rel: b, startnode: startnode(b), endnode: endnode(b)}), k
Hopefully that helps!
You get non distinct results, because you return both b and k
If you only want to get distinct b's use:
MATCH (a)-[k:nearby*]->(b)
WHERE ID(a) = 1234 AND a<>b
RETURN DISTINCT b
You should also use parameters!
MATCH (a)-[k:nearby*]->(b)
WHERE ID(a) = {1} AND a<>b
RETURN DISTINCT b
ps.setInt(1,1234);

Resources