ananono
ananono

Reputation: 1

How to find the the most popular product in graph database?

I have a graph database with the following schema made using Neo4j.

MATCH (customer{customerid})-[:made]->(purchased{Invoiceno, invoicedate})-[:includes{quantity, invoiceamount}]->(product{stockcode, unitprice})

I want the most popular product which is the most customers purchased but I am only able to get the most purchased product by quantity.

MATCH (a:customer)-[r1:MADE]->(b:purchase)-[r2:includes]->(c:product)
WITH SUM(r2.quantity) AS totalquantity, c.stockcode AS productcode
RETURN productcode, totalquantity  ORDER BY totalquantity DESC LIMIT 1

This was the result.

╒═════════════╤═══════════════╕
│"productcode"│"totalquantity"│
╞═════════════╪═══════════════╡
│"84077"      │3552           │
└─────────────┴───────────────┘

So, how do I get the most popular product based on most customer buying it ?

Upvotes: 0

Views: 122

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

If a purchase can contain multiple time the same product, you'll need to traverse the full path from the customer and just return the path counts :

MATCH (a:customer)-[r1:MADE]->(b:purchase)-[r2:includes]->(c:product)
RETURN c.id, count(*) AS score
ORDER BY score DESC
LIMIT 1

If a product can be included in a purchase only once, then you can just return the degree of the product for the includes relationship :

MATCH (c:product)
RETURN c.id, size((c)<-[:includes]-()) AS score
ORDER BY score DESC
LIMIT 1

Upvotes: 1

Related Questions