如何编写一个 gremlin 查询来查找其他顶点之间的公共顶点和 return 按重叠计数排序?

How to write a gremlin query that finds common vertices between other vertices and return sorted by the count of overlap?

我正在使用 python+gremlin 来实现我的图形查询,但还远远没有理解很多概念,并且遇到了一个我不知道该怎么做的有趣查询。

假设我们有许多标签为 Chef 的厨师顶点、标签为 Ingredient 的配料顶点和菜品顶点 Dish。在任何给定时间,厨师都可以使用手头的食材,用 ChefIngredient 之间的边表示,称为 has。菜肴有成分,用 DishIngredient 之间的边表示,称为 usesChefDish 之间也有一条边,表示 he/she 之前是否已经成功,称为 madeBefore

可能很明显,但有些菜肴是厨师从未做过的,并非所有菜肴都使用所有食材,而且厨师很可能没有所有食材。

我想创建一个执行以下操作的查询:

Get Dishes that the chef has never made, sorted by the dishes that contain the most ingredients that the chef has to make it (if can get the ratio too would be great). So the first dishes in the results are ones the chef has never made, and maybe has all the ingredients to make, somewhere in the middle of the results are dishes they have never made and have around half the ingredients needed to make it, and last would be dishes they have never made and also have pretty much none of the ingredients needed to make it.

以下查询将查找厨师从未做过的所有菜肴:

g.V()\
    .hasLabel("Dish")\
    .filter(
        __.not_(
            __.in_("madeBefore").has("Chef", "name", "chefbot1")
        ))\
    .valueMap(True)\
    .toList()

但从这里开始,我只是不知道从哪里开始才能根据厨师拥有的食材数量对这些菜肴进行分类。

我的另一个想法是查询成分并使用 project 来获取连接厨师和菜肴的边数,然后以某种方式过滤它们,但我不知道是什么之后要做的事:

g.V()\
    .hasLabel("Ingredient")\
    .filter(
        __.in_("has").has("Chef", "name", "chefbot1"))\
    .project("v", "dishesUsingIngredient")\
    .by(valueMap(True))\
    .by(inE().hasLabel("uses").count())\
    .order().by("dishesUsingIngredient", Order.desc)\
    .toList()

我现在对 Gremlin 的问题是了解如何将更复杂的查询链接在一起,是否有人可以阐明如何解决此类问题?

如果我理解你的描述,你可以这样做:

g.V().hasLabel('Dish').
  filter(__.not(__.in('madeBefore').
      has('Chef', 'name', 'chefbot1'))).
  group().by('name').
    by(out('uses').in('has').
      has('Chef', 'name', 'chefbot1').count())
  .order(local).by(values)

示例:https://gremlify.com/8w