0

Suppose I have 3 students (A,B,C) and having a major subject and marks respectievely but when I query the result shown in a uneven way.

Data

A -> Math -> 77

B -> History -> 70

C -> Science -> 97

Query

g.V('Class').has('name',within('A','B','C'))

Result

{"student_name":['A','B','C'], "major_subject":['Math','Science','History'], "marks":[70,77,97]}

The data displayed by querying the database is not in order according to the name of the student.

  • @stephenmallette Can you help me with this above problem ? –  Oct 27 '17 at 06:06
  • 1
    I think you may need to refine your question a bit. I don't follow how you get from that data to that query to that result. Could you replace the "data" section with an actual Gremlin script that creates the data as shown here: https://stackoverflow.com/a/46685888/1831717 ? Then update the "query" section to be closer to the actual traversal you are executing? maybe i can understand better then. thanks – stephen mallette Oct 27 '17 at 10:34

1 Answers1

0

I assume that your graph looks kinda like this:

g = TinkerGraph.open().traversal()
g.addV('student').property('name', 'A').
    addE('scored').to(addV('subject').property('name', 'Math')).
      property('mark', 77).
  addV('student').property('name', 'B').
    addE('scored').to(addV('subject').property('name', 'History')).
      property('mark', 70).
  addV('student').property('name', 'C').
    addE('scored').to(addV('subject').property('name', 'Science')).
      property('mark', 97).iterate()

Now the easiest way to gather the data is this:

gremlin> g.V().has('student', 'name', within('A', 'B', 'C')).as('student').
           outE('scored').as('mark').inV().as('major').
           select('student','major','mark').
             by('name').
             by('name').
             by('mark')
==>[student:A,major:Math,mark:77]
==>[student:B,major:History,mark:70]
==>[student:C,major:Science,mark:97]

But if you really depend on the format shown in your question, you can do this:

gremlin> g.V().has('student', 'name', within('A', 'B', 'C')).
           store('student').by('name').
           outE('scored').store('mark').by('mark').
           inV().store('major').by('name').
           cap('student','major','mark')
==>[major:[Math,History,Science],student:[A,B,C],mark:[77,70,97]]

If you want to get the cap'ed result to be ordered by marks, you'll need a mix of the 2 queries:

gremlin> g.V().has('student', 'name', within('A', 'B', 'C')).as('a').
           outE('scored').as('b').
           order().
             by('mark').
           inV().as('c').
           select('a','c','b').
             by('name').
             by('name').
             by('mark').
           aggregate('student').by(select('a')).
           aggregate('major').by(select('b')).
           aggregate('mark').by(select('c')).
           cap('student','major','mark')
==>[major:[History,Math,Science],student:[B,A,C],mark:[70,77,97]]

To order by the order of inputs:

gremlin> input = ['C', 'B', 'A']; []
gremlin> g.V().has('student', 'name', within(input)).as('a').
           order().
             by {input.indexOf(it.value('name'))}.
           outE('scored').as('b').
           inV().as('c').
           select('a','c','b').
             by('name').
             by('name').
             by('mark').
           aggregate('student').by(select('a')).
           aggregate('major').by(select('b')).
           aggregate('mark').by(select('c')).
           cap('student','major','mark')
==>[major:[97,70,77],student:[C,B,A],mark:[Science,History,Math]]
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • When I change the order inside " within('B','A','C') ", I'm still getting the answer as "==>[major:[Math,History,Science],student:[A,B,C],mark:[77,70,97]]" but I need the answer to be in order of B,A,C. @stephenmallette –  Nov 02 '17 at 10:08
  • What's the order criterion in your graph? Any numeric field? – Daniel Kuppitz Nov 02 '17 at 13:02
  • 1
    Added an example of how to order the cap'ed result by marks. – Daniel Kuppitz Nov 02 '17 at 16:50
  • Is there any way to order the result according to the order given in the 'within' element like 'within('C','A','B')' without depending on any order criteria –  Nov 03 '17 at 06:55
  • You can make it an external list and order by the "element" index; this would require a lambda though. – Daniel Kuppitz Nov 03 '17 at 13:02