I have a Neo4j Graph database which stores Twitter data.
Each Tweet is about a Topic and has got this kind of relationship:
(t:Tweet)-[:ABOUT]->(t1:Topic)
A User can be mentioned by a Tweet due to this relationship
(t:Tweet)-[:MENTIONS]->(u:User)
I'd like to know how many users are mentioned in tweets regarding a specific topic.
My query is:
match (n:Topic)<--()-[r:MENTIONS]->(u:User)
where n.name='politics'
return count(r)
Is this query right? Because I've got unexpected results.
Thanks a lot.
You can try using the following query.
match (n:Topic{name:'politics'})
RETURN size((n)<--()-[:MENTIONS]->(:User)) as count
Related
I am searching for a cypher query that returns me the current data-model, meaning every node plus relationship, but only once.
I did find the query already in the past via stackoverflow.com or google, but I did not find it now again making a research.
Thank you
I think what you're looking for is CALL db.schema.visualization
The output for the Movie Graph is:
I am working on a dating app where users can "like" or "dislike" other users and get matched.
As you can imagine the most important query of the app would be:
Give me a stack of nearby user profiles that I have NOT liked/disliked before.
I tried to work on this with a document database (Firestore) and figured it's simply not suitable for such kind of application and hence landed in the graph database world which is new and fascinating to me.
I understand that by nature a graph database retrieves data by tracing through the relationships and make relationships first-class citizens. My question now is that what if the nodes that I am trying to get are those with no relationship from the given node? What would the query look like? Can anyone provide an example query?
Edit:
- added nearby criteria to the query statement
This is definitely possible, here is a query example :
MATCH (me:Profile {name: "Chris"})
MATCH (other:Profile) WHERE NOT (other)-[:LIKES]->(me)
As stated in the comments of your original question, on a large dataset it might not scale well, that said it is pretty uncommon that you would use only one criteria for matching, for example, the list of possible profiles to match from can be grouped by :
geolocation
profiles in depth 2 ( who is liking me, then find who other people they like, do those people like me ? )
shared interests
age group
skin color
...
I have a database containing movies and users. Users can follow each other and rate movies. I have a user with the id 599. I want to find all the movies he hasn't watched that the other users he follows have watched. Here's what I tried so far. I do get a result and it works but the numbers don't really add up so I think there might be something off.
MATCH (u:user {id:599}),(m2:Movie), (u2:user),(u)-[r]->(u2),(u2)-->(m2)
WHERE not (u)-->(m2)
RETURN m2.title, m2.movieId,u2.id;
As #Inversefalcon stated, perhaps your query needs to be specific about the relationship types, in case there are multiple types of relationships between the same nodes.
For example (assuming your data model has the relationship types FOLLOWS and WATCHED):
MATCH (u:user {id:599})-[:FOLLOWS]->(u2:user)-[:WATCHED]->(m2:Movie)
WHERE not (u)-[:WATCHED]->(m2)
RETURN m2.title, m2.movieId, u2.id;
If this does not solve your issue, then please provide some sample data and what your expected number of results is.
I am using Neo4j 3.4 and am struggling with this particular query
MATCH (u:User)-[:IS_A_MEMBER_OF]->(c:Church)
RETURN size([(p:Post)<-[:POSTED]-(:User)-[:IS_A_MEMBER_OF]->(c) WHERE NOT (u)-[:ACKNOWLEDGED|POSTED]->(p) | p])
This query is designed to get the number of posts for the given Church that a user has not yet acknowledged and did not post themselves. In other words, it should retrieve all the posts by members of the church, then figure out which ones the user u has neither acknowledged or posted and return the count.
Unfortunately, I cannot figure out why Neo4j is not doing the check in the where clause. Is there something about pattern comprehensions that I am missing? Because the number returned is the same for all users, no matter whether they have acknowledged or posted any of the posts.
Thanks!
Here is a working example, I used count instead of size, size is for arrays and here you have a node row, count will aggregate all your row into a result.
MATCH (u:User)-[:IS_A_MEMBER_OF]->(c:Church),
(c)<-[:IS_A_MEMBER_OF]-(:User)-[:POSTED]->(p:Post)
WHERE NOT (u)-[:ACKNOWLEDGED|POSTED]->(p)
RETURN c, u, count(p)
This return for every church and members of this church, a number of unacknowledged posts, not posted by the member.
I submitted a similar bug report. This seems to be a problem starting with 3.3.6.
https://github.com/neo4j/neo4j/issues/11967
I have a graph database with people and cities. Is this query doing what I think it is doing
MATCH (c:City)-->(p:People)
RETURN c,count(p);
I want this query to be spitting back the city c with the count of the people in that city. However, the count is only one. This is either a problem with my loader or my query. Is the query giving me back what I think it is? Thanks
Your original query needs to change the direction.
MATCH (n:City)<--(p:Person)
RETURN n,count(p)
You need to have person going "into" city, and this returns the correct results for your example, as your example has 3 people "LIVES_IN" New York and 1 people "LIVES_IN" Boston, and that is what the query is returning in the console.