TinkerPop: общий запрос для объединения и фильтрации нескольких обходов - PullRequest
0 голосов
/ 12 сентября 2018

Пример данных: TinkerPop Modern Graph

Условия:

  1. vadas подключен к lop в 2 hops
  2. Is vadas подключен к peter в 3 hops
  3. Is vadas подключен к does-not-exists в 1 hops (поиск, который не даст никаких результатов)

Фиктивный поиск с ожидаемыми результатами

  1. Условия 1 И 2 => [vadas-marko-lop, vadas-marko-lop-peter]

  2. Условия 1 ИЛИ 3 => [vadas-marko-lop]


Что я смог получить

  1. Условия 1 И 2
gremlin> g.V().has("person", "name", "vadas").as("from")
.select("from").as("to1").repeat(both().as("to1")).times(2).emit().has("software", "name", "lop")
.select("from").as("to2").repeat(both().as("to2")).times(3).emit().has("person", "name", "peter")
.project("a", "b")
.by(select(all, "to1").unfold().values("name").fold())
.by(select(all, "to2").unfold().values("name").fold())
==>[a:[vadas,marko,lop],b:[vadas,marko,lop,peter]]
Условия 1 ИЛИ 2
gremlin> g.V().has("person", "name", "vadas").as("nodes")
.union(repeat(both().as("nodes")).times(2).emit().has("software", "name", "lop"),
out().has("x", "y", "does-not-exist").as("nodes"))
.project("a")
.by(select(all, "nodes").unfold().values("name").fold())
==>[a:[vadas,marko,lop]]

Итак, как этого добиться, у меня есть два разных формата запроса, есть ли способ написать формат запроса, который может сделатьоба?

И это не сработало, что-то здесь не так?Не возвращает узлы, которые были пройдены

g.V().has("person", "name", "vadas").as("nodes")
.or(
repeat(both().as("nodes")).times(2).emit().has("software", "name", "lop"), 
repeat(both().as("nodes")).times(3).emit().has("person", "name", "peter")
)
.project("a").by(select(all, "nodes").unfold().values("name").fold())
==>[a:[vadas]]
// Expect paths to be printed here vadas..lop, vadas...peter

1 Ответ

0 голосов
/ 16 сентября 2018

Я не знаю, понимаю ли я, что вам нужно, но если вам просто нужно что-то вроде шаблона запроса, то, возможно, это поможет:

gremlin> conditions = [
......1>   [filter: {has("software", "name", "lop")},  distance: 2],
......2>   [filter: {has("person", "name", "peter")},  distance: 3],
......3>   [filter: {has("x", "y", "does-not-exist")}, distance: 1]]
==>[filter:groovysh_evaluate$_run_closure1@378bd86d,distance:2]
==>[filter:groovysh_evaluate$_run_closure2@2189e7a7,distance:3]
==>[filter:groovysh_evaluate$_run_closure3@69b2f8e5,distance:1]

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has("person", "name", "vadas").
......1>   union(repeat(both().simplePath()).
......2>           times(conditions[0].distance).
......3>           emit().
......4>         filter(conditions[0].filter()).store("x"),
......5>         repeat(both().simplePath()).
......6>           times(conditions[1].distance).
......7>           emit().
......8>         filter(conditions[1].filter()).store("x")).
......9>   barrier().
.....10>   filter(select("x").
.....11>          and(unfold().filter(conditions[0].filter()),
.....12>              unfold().filter(conditions[1].filter()))).
.....13>   path().
.....14>     by("name")
==>[vadas,marko,lop]
==>[vadas,marko,lop,peter]

gremlin> g.V().has("person", "name", "vadas").
......1>   union(repeat(both().simplePath()).
......2>           times(conditions[0].distance).
......3>           emit().
......4>         filter(conditions[0].filter()).store("x"),
......5>         repeat(both().simplePath()).
......6>           times(conditions[2].distance).
......7>           emit().
......8>         filter(conditions[2].filter()).store("x")).
......9>   barrier().
.....10>   filter(select("x").
.....11>          or(unfold().filter(conditions[0].filter()),
.....12>             unfold().filter(conditions[2].filter()))).
.....13>   path().
.....14>     by("name")
==>[vadas,marko,lop]

И немного больше абстракции должно сделатьПонятно, что два запроса отличаются только на 1 шаг (and против or):

apply = { condition ->
  repeat(both().simplePath()).
    times(condition.distance).
    emit().
  filter(condition.filter()).store("x")
}

verify = { condition ->
  unfold().filter(condition.filter())
}

// condition 1 AND 2   
g.V().has("person", "name", "vadas").
  union(apply(conditions[0]),
        apply(conditions[1])).
  barrier().
  filter(select("x").
         and(verify(conditions[0]),
             verify(conditions[1]))).
  path().
    by("name")

// condition 1 OR 3   
g.V().has("person", "name", "vadas").
  union(apply(conditions[0]),
        apply(conditions[2])).
  barrier().
  filter(select("x").
         or(verify(conditions[0]),
            verify(conditions[2]))).
  path().
    by("name")
...