I want to find method-pairs that read or write the same field, for this i wrote this query:
match (c:Class)-[:DECLARES]->(m1:Method), (c)-[:DECLARES]-(m2:Method), (c)-[:DECLARES]-(f:Field), (m1)-[:WRITES|READS]->(f), (m2)-[:WRITES|READS]->(f)
return m1.name, m2.name, f.name
Now i have the problem, that there are several duplicates in the results.
I want every "m1.name" and "m2.name" pair to be unique. Is there a way to filter out results that are swaped versions of other results?
If you enforce a specific ordering of the Method nodes' native IDs, that will produce distinct Method name pairs (assuming method names are unique):
MATCH
(c:Class)-[:DECLARES]->(m1:Method),
(c)-[:DECLARES]-(m2:Method),
(c)-[:DECLARES]-(f:Field),
(m1)-[:WRITES|READS]->(f),
(m2)-[:WRITES|READS]->(f)
WHERE ID(m1) < ID(m2)
RETURN m1.name, m2.name, f.name
Related
I am creating an app kind of like Facebook. It is an app where people can share products and collections of products. In the "create a post" popup, people can either select a product or a collection (group of products but consider it as a single object) or just text to create a post. I need to fetch the posts created by my followers.
Each post will have a property of type PRODUCT, COLLECTION, OR TEXT to indicate what type of post it is.
In my neo4j DB, there is a Post object, product object, collection object and user object.
When you create a post, relations will be created between them.
(post)-[:CREATED_BY]->(USER)
(post{type:"PRODUCT"})-[:INCLUDES]->(product)
(post{type:"COLLECTION})-[:INCLUDES]->(collection)
This is what I tried to get the posts of type "PRODUCT". IT shows an error. but just to give a basic idea of our properties.
MATCH (user:User{lastName: "mylastname"})-[:FOLLOWS {status: "accepted"}]->(following) WITH following
OPTIONAL MATCH (post:Post {type: "PRODUCT"})-[r:CREATED_BY]->(following) WITH post,user, r OPTIONAL
MATCH
(post)-[:INCLUDES]->(product:Product) WITH COLLECT({post:post, datetime: r.datetime,
type:"PRODUCT",product:product user: following}) as productPosts
UNWIND productPosts AS row
RETURN row
ORDER BY row.datetime DESC
SKIP 0
LIMIT 10
Your WITH clauses are not specifying all the variables that need to be carried forward to the remainder of the query. Also, there has at least one typo (a missing comma).
In fact, your query does not even need any WITH clauses. Nor does it need to COLLECT a list only to immediately UNWIND it.
This query should work better:
MATCH (user:User{lastName: "mylastname"})-[:FOLLOWS {status: "accepted"}]->(following)
OPTIONAL MATCH (post:Post {type: "PRODUCT"})-[r:CREATED_BY]->(following)
OPTIONAL MATCH (post)-[:INCLUDES]->(product:Product)
RETURN {post:post, datetime: r.datetime, type:"PRODUCT", product:product, user: following} AS row
ORDER BY row.datetime DESC
LIMIT 10
My current graph monitors board members at a company through time.
However, I'm only interested in currently employed directors. This can be observed because director nodes connect to company nodes through an employment path which includes an end date (r.to) when the director is no longer employed at the firm. If he is currently employed, there will be no end date(null as per below picture). Therefore, I would like to filter the path not containing an end date. I am not sure if the value is an empty string, a null value, or other types so I've been trying different ways without much success. Thanks for any tips!
Current formula
MATCH (c2:Company)-[r2:MANAGED]-(d:Director)-[r:MANAGED]-(c:Company {ticker:'COMS'})
WHERE r.to Is null
RETURN c,d,c2
Unless the response from the Neo4j browser was edited, it looks like the value of r.to is not null or empty, but the string None.
This query will help verify if this is the case:
MATCH (d:Director)-[r:MANAGED]-(c:Company {ticker:'COMS'})
RETURN DISTINCT r.to ORDER by r.to DESC
Absence of the property will show a null in the tabular response. Any other value is a real value of that property. If None shows up, then your query would be
MATCH (c2:Company)-[r2:MANAGED]-(d:Director)-[r:MANAGED]-(c:Company {ticker:'COMS'})
WHERE r.to="None"
RETURN c,d,c2
I am new to Neo4j and I have a relatively complex (but small) database which I have simplified to the following:
The first door has no key, all other doors have keys, the window doesn't require a key. The idea is that if a person has key:'A', I want to see all possible paths they could take.
Here is the code to generate the db
CREATE (r1:room {name:'room1'})-[:DOOR]->(r2:room {name:'room2'})-[:DOOR {key:'A'}]->(r3:room {name:'room3'})
CREATE (r2)-[:DOOR {key:'B'}]->(r4:room {name:'room4'})-[:DOOR {key:'A'}]->(r5:room {name:'room5'})
CREATE (r4)-[:DOOR {key:'C'}]->(r6:room {name:'room6'})
CREATE (r2)-[:WINDOW]->(r4)
Here is the query I have tried, expecting it to return everything except for room6, instead I have an error which means I really don't know how to construct the query.
with {key:'A'} as params
match (n:room {name:'room1'})-[r:DOOR*:WINDOW*]->(m)
where r.key=params.key or not exists(r.key)
return n,m
To be clear, I don't need my query debugged so much as help understanding how to write it correctly.
Thanks!
This should work for you:
WITH {key:'A'} AS params
MATCH p=(n:room {name:'room1'})-[:DOOR|WINDOW*]->(m)
WHERE ALL(r IN RELATIONSHIPS(p) WHERE NOT EXISTS(r.key) OR r.key=params.key)
RETURN n, m
With your sample data, the result is:
╒════════════════╤════════════════╕
│"n" │"m" │
╞════════════════╪════════════════╡
│{"name":"room1"}│{"name":"room2"}│
├────────────────┼────────────────┤
│{"name":"room1"}│{"name":"room3"}│
├────────────────┼────────────────┤
│{"name":"room1"}│{"name":"room4"}│
├────────────────┼────────────────┤
│{"name":"room1"}│{"name":"room5"}│
└────────────────┴────────────────┘
I have a possibly bone-headed question, but I'm just starting out with Neo4j, and I hope someone can help me out with learning Cypher syntax, which I've just started learning and evaluating.
I have two User nodes, and a single NewsPost node. Both users LIKE the NewsPost. I'm able to construct a Cypher query to count the likes for the post, but I'm wondering if it's also possible to check if the current user has liked the post in the same query.
What I have so far for a Cypher query is
match (p:NewsPost)<-[r:LIKES]-(u:User)
where id(p) = 1
return p, count(*)
Which returns the post and like count, but I can't figure out the other part of "has the current user liked this post". I know you're not supposed to filter on <id>, but I learned that after the fact and I'll go back and fix it later.
So first, is it possible to answer the "has the current user liked this post" question in the same query? And if so, how do I modify my query to do that?
The smallest change to your query that adds a true/false test for a particular user liking the news post would be
MATCH (p:NewsPost)<-[r:LIKES]-(u:User)
WHERE ID(p) = 1
RETURN p, count(r), 0 < size(p<-[:LIKES]-(:User {email:"michael#nero.com"}))
This returns, in addition to your query, the comparison of 0 being less than the size of the path from the news post node via an incoming likes relationship to a user node with email address michael#nero.com. If there is no such path you get false, if there is one or more such paths you get true.
If that does what you want you can go ahead and change the query a little, for instance use RETURN ... AS ... to get nicer result identifiers, and so on.
What you are looking for is Case.
In your database you should have something unique for each user (id property, email or maybe login, I don't know), so you have to match this user, and then match the relation to the post you want, using case you can return a boolean.
Example:
Optional Match (u:User{login:"Michael"})-[r:LIKES]-(p:newPost{id:1})
return CASE WHEN r IS NULL THEN false ELSE true END as userLikesTopic
If you want to get the relation directly (to get a property in it as example) you can remove the CASE part and directly return r, if it does not exist, null will be returned from the query.
I have a Cypher query that combines two result sets that I would like to then order as a combined result.
An example of what I am trying to do is here: http://console.neo4j.org/r/j2sotz
Which gives the error:
Cached(nf of type Collection) expected to be of type Map but it is of type Collection - maybe aggregation removed it?
Is there a way to collect multiple results into a single result that can be paged, ordered, etc?
There are many posts about combining results, but I can't find any that allow them to be treated as a map.
Thanks for any help.
You can collect into a single result like this:
Start n=node(1)match n-[r]->m
with m.name? as outf, n
match n<-[r]-m
with m.name? as inf, outf
return collect(outf) + collect(inf) as f
Unions are covered here: https://github.com/neo4j/neo4j/issues/125 (not available right now).
I haven't seen anything about specifically sorting a collection.