How do I create a cypher query for the following use case?

I have a graph that has nodes for each kind of location. I want to create a relationship whenever a person visits a location. The relationship would look something like this.


All the location nodes are already present in the graph. The person node would have a unique id and I would have a few properties on the Visits node too.The thing I want to do is if the graph has never seen this person create a new node for this person and the required relationships , if the graph has already seen this Person earlier I just need to create one more relationship between the Person and the Location node through a visits node.
I know i am supposed to use merge here but the thing is it creates a new person node everytime even if the person node for that id is already present in the graph.

Sounds like you're trying to do a MERGE of the entire pattern at once, which won't work for your use case.

Please review the knowledge base article on understanding how MERGE works, this explains some of the subtle behaviors you need to be aware of when using MERGE.

In this particular case, you need to first MERGE on just the person node, and after that, when you're guaranteed that both the person and location nodes exist, it's a question of whether you always want to create the rest of the pattern (even if the person visited the location in some earlier instance, like at some earlier time) or whether you want to potentially match on a previous pattern based upon the properties present in your pattern.

If you always want to create the pattern, then you would follow the MERGE of the person with a CREATE of the pattern. If you want to match to an existing pattern if it already exists, then follow the MERGE of the person with a MERGE of the rest of the pattern.

thank you very much . not merging the entire pattern at once did the trick for me
thank you very much