|
|
Customer Order Graph - Detecting Purchase Patterns with Cypher
Author: Venkata Sudhakar
ShopMax India processes thousands of orders daily across Mumbai, Bangalore, Delhi, Hyderabad, and Chennai. Understanding purchase patterns - which products are bought together, which customers are repeat high-value buyers, and which product combinations signal upsell opportunities - is critical for the marketing and sales teams. A graph database models customers, orders, and products as nodes connected by PLACED and CONTAINS relationships, making pattern queries like 'find all customers who bought a Samsung TV and then ordered a wall bracket within 30 days' a natural graph traversal instead of a multi-table SQL join.
The order graph has three node labels: Customer (name, city, tier), Order (orderId, date, total), and Product (sku, name, category). Relationships include PLACED (customer to order) and CONTAINS (order to product, with quantity and price properties). To find cross-sell patterns, traverse from a product through CONTAINS edges to orders, then to other products in those same orders. Repeat buyers are found by counting PLACED edges per customer. High-value pairs are ranked by co-occurrence frequency across the order history.
The example below creates a ShopMax India order graph with 5 customers and 8 orders, then queries for the most common product pairs bought together, identifies repeat buyers, and finds customers who bought a TV but have not yet ordered a compatible accessory.
It gives the following output,
Frequently bought together:
Samsung 65 QLED + Samsung Wall Bracket (2 orders)
Samsung 65 QLED + 4K HDMI Cable (1 orders)
Samsung Wall Bracket + 4K HDMI Cable (1 orders)
TV buyers with no accessory - upsell targets:
Priya Nair (Bangalore)
In production, build ALSO_BOUGHT relationships as a pre-computed summary by running the pairs query nightly and storing co-occurrence counts as edge weights - this lets the recommendation API read direct graph edges without recomputing joins. Segment upsell targets further by customer tier: Gold customers get a WhatsApp nudge from the ShopMax India sales team while Silver customers get an automated email. Use the neo4j Graph Data Science library's link prediction algorithms (gds.linkPrediction.adamicAdar) to score likely future purchases and surface them before the customer even searches.
|
|