Reputation: 179
I have been looking into different recomender scripts(movie,social).
I would like to fetch nodes that has a certain number of neighbors of the same type. Lets say:
(n:Person {name:Peter, job:Mechanic})
Do we find other mechanics that share the same type of friends as Peter.
Eg. Mechanic-TruckDriver, Mechanic-Priest,Mechanic-CarSalesPerson.
As far as i can see i can do it using Job as a Node as in the examples.
However my question is, is it possible with job as a property on the node Person?
Since I would like to be able to generate a list of lets say people and jobs, and if its not a property I would have to do an extra query for each person.
Upvotes: 0
Views: 954
Reputation: 11735
Sure, it's possible with nodes or properties, it works the same way:
MATCH (p1:Person {name: "Peter", job: "Mechanic"})-[:HAS_FRIEND]-(n),
(p2:Person {job: "Mechanic"})-[:HAS_FRIEND]-(n)
WHERE p1 <> p2
RETURN p2
LIMIT 10
vs
MATCH (job:Job {name: "Mechanic"}),
(job)<-[:HAS_JOB]-(p1:Person {name: "Peter")-[:HAS_FRIEND]-(n),
(job)<-[:HAS_JOB]-(p2:Person)-[:HAS_FRIEND]-(n)
WHERE p1 <> p2
RETURN p2
LIMIT 10
It's just a different way of specifying the link between the Person
and its job, either through a property or a relationship.
But either way, you'll have an "extra query" for each person: neither properties nor relationships are fetched by default when you access a node.
Update: the question is not about friends-of-friends that have the same job as the first person, but other people with the same job as the first person that have friends with the same jobs as the friends of the first person (!). So networks of friends that look the same professionally.
Going with the job
property, that would be:
MATCH (p1:Person {name: "Peter", job: "Mechanic"})-[:HAS_FRIEND]-(f)
WITH p1, collect(DISTINCT f.job) AS friendJobs
MATCH (p2:Person)-[:HAS_FRIEND]-(f)
WHERE p2 <> p1
AND p2.job = p1.job
AND f.job IN friendJobs
RETURN p2, count(DISTINCT f.job) AS jobs, count(f) AS friends
ORDER BY jobs DESC, friends DESC
LIMIT 10
I assumed you meant to sort primarily by the number of different jobs in the network of friends, not the count of friends themselves, which does not really seem relevant here; I included it as a secondary sort.
Upvotes: 1