如果要对follow._dst进行排重,在nebula里查询语句该怎么写

产品的需求是要推荐10个我关注的关注,并显示我关注的xx也关注了TA。下面语句可以查出来,但是因为有挺多我关注的一些人都关注了同一个人的情况,所以同一个人会出现很多次重复,如果要对下面的follow._dst 作下排重,在nebula里查询语句该怎么写。

(user@nebula) [friend]> go 2 STEPS from 33630944 OVER follow YIELD follow._dst, follow._src, $$.user.fans_count as fans_count| order by $-.fans_count desc | limit 15
==========================================
| follow._dst | follow._src | fans_count |
==========================================
| 46721694    | 24669437    | 1432       |
------------------------------------------
| 46721694    | 31714474    | 1432       |
------------------------------------------
| 46721694    | 49716317    | 1432       |
------------------------------------------
| 46721694    | 68888763    | 1432       |
------------------------------------------
| 46721694    | 66431755    | 1432       |
------------------------------------------
| 46721694    | 33074810    | 1432       |
------------------------------------------
| 46721694    | 17768293    | 1432       |
------------------------------------------
| 46721694    | 72668224    | 1432       |
------------------------------------------
| 46721694    | 59754195    | 1432       |
------------------------------------------
| 30869676    | 62558486    | 1331       |
------------------------------------------
| 30869676    | 75646337    | 1331       |
------------------------------------------
| 30869676    | 71572974    | 1331       |
------------------------------------------
| 30869676    | 96605986    | 1331       |
------------------------------------------
| 30869676    | 18794320    | 1331       |
------------------------------------------
| 30869676    | 45966196    | 1331       |
------------------------------------------
Got 15 rows (Time spent: 42.624/89.009 ms)


这样的情况用group by 是不是可以做一个去重呢?https://docs.nebula-graph.io/1.1/manual-EN/2.query-language/2.functions-and-operators/order-by-function/

你好,加了 group by follow._dst 去重后,就获取不到follow._src了,怎么去重之后还能得到其中一个follow._src呢。

执行如下:

(user@nebula) [friend]> go 2 STEPS from 33630944 OVER follow YIELD follow._dst as uid, follow._src as follow_uid | group by $-.uid YIELD $-.uid as uid | order by $-.uid | limit 5
============
| uid      |
============
| 10000001 |
------------
| 10000005 |
------------
| 10011385 |
------------
| 10036270 |
------------
| 10079598 |
------------
Got 5 rows (Time spent: 25.422/72.018 ms)

Sun Jan 17 05:09:15 2021

哪个版本?

1.0的版本

利用得到的去重follow,用pipe结合go reversely来反查?(reversely文档:https://docs.nebula-graph.io/1.1/manual-EN/2.query-language/4.statement-syntax/2.data-query-and-manipulation-statements/go-syntax/#traverse_reversely)

不行哦,在上面基础上,用pipe结合go reversely来反查后,又把前面未排重重复的结点查出来了

看看下面执行后的 $-.uid 项

(user@nebula) [friend]> go 2 STEPS from 33630944 OVER follow YIELD follow._dst as uid, follow._src as follow_uid | group by $-.uid YIELD $-.uid as uid | order by $-.uid | limit 5 | go from $-.uid over follow REVERSELY YIELD $-.uid,follow._dst     
==========================
| $-.uid   | follow._dst |
==========================
| 10036270 | 45966196    |
--------------------------
| 10036270 | 23481550    |
--------------------------
| 10011385 | 76764760    |
--------------------------
| 10000001 | 67907342    |
--------------------------
| 10000001 | 18875683    |
--------------------------
| 10000001 | 12753701    |
--------------------------
| 10000001 | 62100520    |
--------------------------
| 10000001 | 55587369    |
--------------------------
| 10000001 | 90641712    |
--------------------------
| 10000001 | 47633975    |
--------------------------
| 10000001 | 76764760    |
--------------------------
| 10000001 | 60309343    |
--------------------------
| 10000001 | 44950377    |
--------------------------
| 10000001 | 16489870    |
--------------------------
| 10000001 | 30869676    |
--------------------------
| 10000001 | 26419690    |
--------------------------
| 10000001 | 56884727    |
--------------------------
| 10079598 | 49740693    |
--------------------------
| 10000005 | 47633975    |
--------------------------
Got 19 rows (Time spent: 28.401/310.47 ms)

直接用 agg 函数就可以啊,不需要反向查

agg 函数的使用文档在哪里呀,找了下没找到 :joy:

go 2 STEPS from 33630944 OVER follow where follow._dst!=1001
YIELD follow._dst as uid, follow._src as follow_uid 
| group by $-.uid YIELD $-.uid as uid, coalesce(collect($-.follow_uid)) as follow_uid
| order by $-.uid | limit 5 

1.0 还不支持上面的写法,你可以用这个:

go 2 STEPS from 33630944 OVER follow where follow._dst!=1001
YIELD follow._dst as uid, follow._src as follow_uid 
| group by $-.uid YIELD $-.uid as uid, min($-.follow_uid) as follow_uid
| order by $-.uid | limit 5 
2 个赞

谢谢, :+1: :+1: :+1: 这个用group by来排重并且获取src的可以了

在这个基础上怎么过滤掉一度的关注呢,或者用 minus 过滤掉一度后怎么获取到 src 呢

下面是取二度关注,并过滤掉我一度关注数据的语句,但是无法提取 dst 的 src

(user@nebula) [friend]> (go 2 STEPS from 33630944 OVER follow where follow._dst!=33630944 yield follow._dst as dst  minus go from 33630944 OVER follow ) | limit 5
==============
| dst        |
==============
| 85526278   |
--------------
| 23098204   |
--------------
| 1839767264 |
--------------
| 60309343   |
--------------
| 15851767   |
--------------
Got 5 rows (Time spent: 80.447/127.197 ms)

这个啥意思?自环?

GO 2 STEPS FROM 33630944 OVER follow where follow._dst!=follow._src 这个语句能过滤掉2度里的1度关注么?follow._dst 是二度关注,follow._src 虽然是一度关注,但是单条记录的 follow._src 好像不可能等于 follow._dst 吧,我查了下==的结果是空的

(user@nebula) [friend]> GO 2 STEPS FROM 33630944 OVER follow where follow._dst==follow._src | limit 5 
Execution succeeded (Time spent: 9.971/71.039 ms)

Mon Jan 18 09:13:08 2021

Minus 在新的2.0版本可以支持获取dst的src么么,做社交关系推荐基本都要用MINUS进行过滤,然后需要知道dst的src