Database Reference
In-Depth Information
Products Purchased by Friends Nearby and Matches User's Tags
To find products that match with a specific user's tags and have been purchased by friends who live within
a set distance of the user, use the
friendsPurchaseTagSimilarityAndProximityToLocation
method,
which is easily world's longest method name and is located in
PurchaseService
class. The method uses the
MappedProductUserPurchase
ViewModel
, as shown in Listing 7-56.
Listing 7-56.
The
friendsPurchaseTagSimilarityAndProximityToLocation
method in
PurchaseService
public List<MappedProductUserPurchase> friendsPurchaseTagSimilarityAndProximityToLocation(double
lat, double lon, double distance, string userId)
{
var q = string.Format(distanceQueryAsString(lat, lon, distance));
return _graphClient.Cypher
.Start(new { n = Node.ByIndexQuery("geom", q) })
.With("n")
.Match("(u:User { userId : {userId} } )-[:USES]->(t)<-[:HAS]-p")
.WithParam("userId", userId)
.With("n,u,p,t")
.Match("u-[:FOLLOWS]->(f)-[:HAS]->(n) ")
.With("p,f,t")
.Match("f-[:MADE]->()-[:CONTAINS]->(p)")
.Return(() => Return.As<MappedProductUserPurchase>("{productId:p.productId,title:p.
title," +
"fullname:collect(f.firstname + ' ' + f.lastname),wordPhrase:t.wordPhrase,cfriends:
count(f)}"))
.OrderByDescending("count(f)")
.Results.ToList();
}
The query begins starts with a location search within a certain distance, then matching the current user's tags
to products. Next, the query matches friends based the location search. The resulting friends are matched against
products that are in the set of user tag matches. The result of the query is shown in Figure
7-17
.