Sorting vertices based on edge attribute and save top k vertices in set accum in single query

Hi Team,

I am creating user based collaborative filtering model using GSQL. I have calculated similarity between users and stored the result in “similarity_score” attrbiute on edge “similar_customer”. Below is the sample design schema :

for those customers in test set, I need to find top 10 similar customers and then generate a recommendation score for items these top 10 neighbors have bought.

  test_customers = select s from start:s-(customer_product:e)-product:t 
                  where e.test_label==True; 
 # I know below code is incorrect, looking for a way to store top 10 similar customers with highest 
 #similarity score.
  test_customers = select s from test_customers:s-(similar_customer:e)-:t 
                              ACCUM t.@similarity_score+=e.similarity_score 
                              POST-ACCUM s.@top_10_neighbors+=t,
                              ORDER BY t.@similarity_score desc limit 10;

Could anyone please suggest an optimized way to do so?

1 Like

How about a HeapAccum ?

  TYPEDEF TUPLE<DOUBLE score, VERTEX member> sim;
  HeapAccum<sim>(10, score DESC) @topMatches;
  test_customers = select s from start:s-(customer_product:e)-product:t 
                  where e.test_label==True; 

  test_customers = select s from test_customers:s-(similar_customer:e)-:t 
                              ACCUM s.@topMatches+=sim(e.similarity_score , t);

3 Likes