问题描述
我正在尝试使用 math() 的数字结果来过滤遍历结果,但出现了我不明白的错误。我在网上看到的 math() 步骤的所有示例都是只返回数字结果的查询,但没有一个对它做任何事情。
这就是我所拥有的:
g.V()
.hasLabel("user")
.values("targetdistance")
.is(
lte(math(`abs(_ - ${searcherUser.locationLat}) * ${GPS_TO_KM}`).by("locationLat"))
)
"Cannot compare '51' (Integer) and '[MathStep(abs(_ - 5),[value(locationLat)])]' (DefaultGraphTraversal) as both need to be an instance of Number or Comparable (and of the same type)""
我想做什么:
这是一项根据配置的设置获取附近用户的功能:targetdistance
值(以公里为单位)。但我也想根据他们自己的targetdistance
排除太远的用户。换句话说:如果其中一个用户认为另一个用户太远,他们就不应该被对方看到。
/**
* Is inside the distance range the user wants
*/
traversal = traversal.has(
"locationLat",P.inside(
searcherUser.locationLat - searcherUser.targetdistance * KM_TO_GPS,searcherUser.locationLat + searcherUser.targetdistance * KM_TO_GPS,),);
traversal = traversal.has(
"locationLon",P.inside(
searcherUser.locationLon - searcherUser.targetdistance * KM_TO_GPS,searcherUser.locationLon + searcherUser.targetdistance * KM_TO_GPS,);
解决方法
像 lte
这样的谓词不能将遍历作为参数。您需要将查询重新表述为:
g.V().
hasLabel('user').as('a').
where(lte('a')).
by('targetDistance').
by(math(…..))
编辑添加...
举一个具体的例子,我们可以使用航线数据集:
gremlin> g.V().
......1> hasLabel('airport').as('a').
......2> where(gte('a')).
......3> by('runways').
......4> by(math('3+3')).
......5> valueMap('city','code','runways')
==>[code:[BOS],city:[Boston],runways:[6]]
==>[code:[DFW],city:[Dallas],runways:[7]]
==>[code:[ORD],city:[Chicago],runways:[7]]
==>[code:[DEN],city:[Denver],runways:[6]]
==>[code:[DTW],city:[Detroit],runways:[6]]
==>[code:[AMS],city:[Amsterdam],runways:[6]]
.