4

I need the result of the first query to pass it as an input parameter to my second query. and also want to know to write multi queries. In my use case, the second query can be traversed only using the result of the first query and that too using loop(which is similar to for loop)

const query1 =g.V().hasLabel('Province').has('code',market').inE('partOf').outV().has('type',state).values('code').as('state') After executing query1,the result is

res=[{id1},{id2},........]

query2 = select('state').repeat(has('code',res[0]).inE('partOf').outV().has('type',city).value('name')).times(${res.length-1}).as('city')

Prashanth
  • 134
  • 7
  • 1
    I don't quite understand your question. what is `res`in the second code block? I assume it's your "result" from the first code block, but where is that assignment? you mention that you're interested in "multi-queries" but I'm not sure what you mean by that. Please add more code/context to help clarify what you're asking. – stephen mallette Aug 07 '19 at 10:42
  • I see you edited your question and I tried to answer but I still had to make many assumptions about what you wanted to do here. If my answer is not fully what you wanted I suggest that you when you next clarify your answer you provide a Gremlin script that creates some sample data - here is an example https://stackoverflow.com/questions/51388315/gremlin-choose-one-item-at-random – stephen mallette Aug 08 '19 at 10:44

1 Answers1

2

I made the assumptions that your first query tries to finds "states by market" where the market is a variable you intend to pass to your query. If that is correct then your first query simplifies to:

g.V().hasLabel('Province').has('code',market).
  in('partOf').
  has('type','state').values('code')

so, prefer in() to inE().outV() when no filtering on edge properties is needed.

Your second query doesn't look like valid Gremlin, but maybe you were just trying to provide an example of what you wanted to do. You wrote:

select('state').
repeat(has('code',res[0]).
       inE('partOf').outV().
       has('type',city).value('name')).
  times(${res.length-1}).as('city')

and I assume that means you want to use the states found in the first query to find their cities. If that's what you're after you can simplify this to a single query of:

g.V().hasLabel('Province').has('code',market).
  in('partOf').has('type','state').
  in('partOf').has('type','city').
  values('name')

If you need data about the state and the city as part of the result then consider project():

g.V().hasLabel('Province').has('code',market).
  in('partOf').has('type','state').
  project('state','cities').
    by('code').
    by(__.in('partOf').has('type','city').
       values('name').
       fold())
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • your assumptions are correct, but not only the properties of the city province but I need state data also. – Prashanth Aug 08 '19 at 12:00