Question

I'm writing a blog post on using graphs to identify credit card thieves. My data model is this : people are connected to merchants through transactions. Transactions have a time and a status ("disputed" or "undisputed"). A fraudulent transaction is a transaction that has the property "disputed". Simple stuff.

  • People have a "Person" label and the following properties : id, code, name, label, gender, age
  • Merchants have a "Merchant" label and the following properties : id, code, name, street, address
  • Only one kind of relationship : "HAS_BOUGHT_AT". It has the following properties : amount, time, status

I'm trying to write a Cypher query that identifies the fraudulent transactions in a graph and grabs the last 5 transactions the customer did before the fraudulent transaction.

Here is what I've got so far :

MATCH (victim:person)-[r:HAS_BOUGHT_AT]->(merchant)
WHERE r.status = "Disputed"
MATCH victim-[t:HAS_BOUGHT_AT]->(othermerchants)
WHERE t.status = "Undisputed" AND t.time < r.time
RETURN DISTINCT victim.name as customer_name, othermerchants.name as store_name, t.amount as amount, t.time as transaction_time
ORDER BY t.time DESC

I have two problems : - the query returns transactions involving people who are not victims - I don't know how to limit the results to the 5 latest transactions

Any one knows what I could do?

Best,

Jean

Was it helpful?

Solution

RE your first question, can you give more insight/detail as to your actual data model and what's in the graph? How do you define what a "victim" is? Based on your current query, a "victim" is someone who has had a "disputed" transaction. When you say your query is returning people who are not victims, what kind of people (or "person"s) are being returned?

As for your second question, try this:

MATCH (victim:person)-[r:HAS_BOUGHT_AT]->(merchant)
WHERE r.status = "Disputed"
MATCH victim-[t:HAS_BOUGHT_AT]->(othermerchants)
WHERE t.status = "Undisputed" AND t.time < r.time
WITH victim, othermerchants, t ORDER BY t.time DESC LIMIT 5
RETURN DISTINCT victim.name as customer_name, othermerchants.name as store_name, t.amount    as amount, t.time as transaction_time
ORDER BY t.time DESC

(Note the "WITH" and the "LIMIT". The second "ORDER BY" may become moot.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top