tell me writing query this pattern

vartex(orange): question: {code:string, type: string } vartex(purple):LeaningElement What I want to do is Get the Vartex:Question with code 0001 and search for LearningElements associated with it. I want to search for Questions related to LearningElement in the search results other than code 0001 and with the same type as the question with code 0001. The query I wrote is below.
g.V().
  has('Question', 'code', '0001').as('a').
  out('has').
  in('has').
  where(neq('a'))
g.V().
  has('Question', 'code', '0001').as('a').
  out('has').
  in('has').
  where(neq('a'))
I don't know how to filter the last in('has').
14 Replies
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
It's just a little while... can someone tell me...
spmallette
spmallette2y ago
when you ask questions about Gremlin, pictures are nice, but it's always best to supply a small script to produce some sample data like:
g.addV('question').property([code:'0001',type:'A']).as('c1').
addV('question').property([code:'0002',type:'A']).as('c2').
addV('question').property([code:'0003',type:'B']).as('c3').
addV('learning-element').as('le1').
addE('has').from('c1').to('le1').
addE('has').from('c2').to('le1').
addE('has').from('c3').to('le1').iterate()
g.addV('question').property([code:'0001',type:'A']).as('c1').
addV('question').property([code:'0002',type:'A']).as('c2').
addV('question').property([code:'0003',type:'B']).as('c3').
addV('learning-element').as('le1').
addE('has').from('c1').to('le1').
addE('has').from('c2').to('le1').
addE('has').from('c3').to('le1').iterate()
If i understand your question properly, I think you just needed an additional where() :
gremlin> g.V().has('question','code','0001').as('q1').
......1> out('has').in('has').
......2> where(eq('q1')).by('type').
......3> where(neq('q1')).
......4> elementMap()
==>[id:3,label:question,code:0002,type:A]
gremlin> g.V().has('question','code','0001').as('q1').
......1> out('has').in('has').
......2> where(eq('q1')).by('type').
......3> where(neq('q1')).
......4> elementMap()
==>[id:3,label:question,code:0002,type:A]
gdotv
gdotv2y ago
(psst:there's an export as query feature on the graph view in gdotv that should help with that)
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
thank you! I was able to do what I want to do! We will also learn about the by step. Thanks for the nice tip! I'll attach the script next time! thank you! I always use it conveniently!
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
Thank you during this period. let me ask you one more question. Can the where in this query be a little simpler? ■query
g.V()
.has('Question','code','0003').as('q1')
.out('has')
.in('has')
.where(eq('q1')).by('pattern')
.where(neq('q1')).as('q2')
.where('q2',gt('q1')).by('difficultyInPattern').bothE()
g.V()
.has('Question','code','0003').as('q1')
.out('has')
.in('has')
.where(eq('q1')).by('pattern')
.where(neq('q1')).as('q2')
.where('q2',gt('q1')).by('difficultyInPattern').bothE()
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
attach the data
spmallette
spmallette2y ago
i guess it depends on what you mean by "simpler". you could start to combine P values if you think that makes it more readable:
gremlin> g.V().has('Question','code','0003').as('a').
......1> out('has').in('has').as('b').
......2> where('b', eq('a').and(neq('a'))).
......3> by('pattern').
......4> by('pattern').
......5> by().
......6> where('b', gt('a')).by('difficultyInPattern').
......7> bothE()
==>e[54][0-has->46]
==>e[55][4-has->46]
gremlin> g.V().has('Question','code','0003').as('a').
......1> out('has').in('has').as('b').
......2> where('b', eq('a').and(neq('a'))).
......3> by('pattern').
......4> by('pattern').
......5> by().
......6> where('b', gt('a')).by('difficultyInPattern').
......7> bothE()
==>e[54][0-has->46]
==>e[55][4-has->46]
i did have trouble combining all three predicates. i expected this to work:
g.V().has('Question','code','0003').as('a').
out('has').in('has').as('b').
where('b', eq('a').and(neq('a')).and(gt('a'))).
by('pattern').
by('pattern').
by().
by('difficultyInPattern').
bothE()
g.V().has('Question','code','0003').as('a').
out('has').in('has').as('b').
where('b', eq('a').and(neq('a')).and(gt('a'))).
by('pattern').
by('pattern').
by().
by('difficultyInPattern').
bothE()
but it returned no results for some reason. that is maybe "simpler", but the more complex the predicate i think the harder it gets to read. if i could get it work, i think the best readable version might be some combination of the two like this:
g.V().has('Question','code','0003').as('a').
out('has').in('has').as('b').
where('b', neq('a')).
where('b', eq('a').and(gt('a'))).
by('pattern').
by('pattern').
by('difficultyInPattern').
bothE()
g.V().has('Question','code','0003').as('a').
out('has').in('has').as('b').
where('b', neq('a')).
where('b', eq('a').and(gt('a'))).
by('pattern').
by('pattern').
by('difficultyInPattern').
bothE()
That one seems about as clear and uncluttered as you could get as it drops the empty by() and seeing where('b', neq('a')). is such a common pattern in Gremlin it immediately pops out as to what's being done there. Anyway, created: https://issues.apache.org/jira/browse/TINKERPOP-2923 @Kelvin Lawrence i think this is an example where using and() and or() of P is pretty useful (when it's working right). I don't get if I'm missing something? we actually have tests that validate this functionality so I'm not sure what's going on.
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
Thanks for answering. I was able to retrieve the desired data with the first query you wrote. But one question remains. It is that the behavior of by step cannot be understood intuitively. Is my by step understanding below correct?
1st by step => Specification to refer to the pattern value of b
2nd by step => Specification to refer to the pattern value of a (eq('a'))
3rd by step => Specification to refer to the vertexId of b (neq('a'))
4th by step => Omitted
1st by step => Specification to refer to the pattern value of b
2nd by step => Specification to refer to the pattern value of a (eq('a'))
3rd by step => Specification to refer to the vertexId of b (neq('a'))
4th by step => Omitted
spmallette
spmallette2y ago
so, by() typically applies to a step in a round-robin fashion. so if you have two items it is apply to it will use a single by() for both:
gremlin> g.V().project('a','b').by('name')
==>[a:marko,b:marko]
==>[a:vadas,b:vadas]
==>[a:lop,b:lop]
==>[a:josh,b:josh]
==>[a:ripple,b:ripple]
==>[a:peter,b:peter]
gremlin> g.V().project('a','b').by('name')
==>[a:marko,b:marko]
==>[a:vadas,b:vadas]
==>[a:lop,b:lop]
==>[a:josh,b:josh]
==>[a:ripple,b:ripple]
==>[a:peter,b:peter]
In your case you have 5 items where by() is applicable and the 4 by() you supplied apply in the order they are encountered for the first three items and a single by() round-robin for the last two items. That said, if you're referring to this traversal:
gremlin> g.V().has('Question','code','0003').as('a').
......1> out('has').in('has').as('b').
......2> where('b', eq('a').and(neq('a'))).
......3> by('pattern').
......4> by('pattern').
......5> by().
......6> where('b', gt('a')).by('difficultyInPattern').
......7> bothE()
==>e[54][0-has->46]
==>e[55][4-has->46]
gremlin> g.V().has('Question','code','0003').as('a').
......1> out('has').in('has').as('b').
......2> where('b', eq('a').and(neq('a'))).
......3> by('pattern').
......4> by('pattern').
......5> by().
......6> where('b', gt('a')).by('difficultyInPattern').
......7> bothE()
==>e[54][0-has->46]
==>e[55][4-has->46]
I'm not sure I understand the mapping you define. I would say: * first by('pattern') applies to "b" * second by('pattern') applies to eq('a') * the by() without an argument applies to the neq('a') where it's comparing a Vertex object (not its T.id, but that's essentially the same thing from an equality perspective) * the by('difficultyInPattern') applies round-robin to both "b" and gt('a') items.
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
thanks so much. I understood very well! By what you mean by item is the number of elements in the step that applies by, right? It would be even better if the following query you showed me before works, because the comparison conditions can be arranged simply.
g.V().has('Question','code','0003').as('a').
out('has').in('has').as('b').
where('b', eq('a').and(neq('a')).and(gt('a'))).
by('pattern').
by('pattern').
by().
by('difficultyInPattern').
bothE()
g.V().has('Question','code','0003').as('a').
out('has').in('has').as('b').
where('b', eq('a').and(neq('a')).and(gt('a'))).
by('pattern').
by('pattern').
by().
by('difficultyInPattern').
bothE()
spmallette
spmallette2y ago
yes...it would be nice if the by() could all apply to a single predicate and i'm not sure why that doesn't work. possibly a bug.
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
I'm a little confused by the by step way of thinking that I was taught before.
......2> where('b', eq('a').and(neq('a'))).
......3> by('pattern').
......4> by('pattern').
......5> by().
......2> where('b', eq('a').and(neq('a'))).
......3> by('pattern').
......4> by('pattern').
......5> by().
I would like to make the following comparisons: Step X also seems to require a by without an argument, but I have no choice but to understand this as a specification. I can't understand it intuitively. (sorry for my poor english...)
b.pattern[from 1st by step ('pattern')] = a.pattern[from 2nd by step ('pattern')]
b.vartex[from X by step] ≠ a.vartex[from 3rd by step()
b.pattern[from 1st by step ('pattern')] = a.pattern[from 2nd by step ('pattern')]
b.vartex[from X by step] ≠ a.vartex[from 3rd by step()
spmallette
spmallette2y ago
i'm sorry - i think i made a mistake. i think that mistake is why i thought there was a bug. i think your original revision with a few minor changes was the best way to write this:
gremlin> g.V().
......1> has('Question','code','0003').as('q1').
......2> out('has').in('has').
......3> where(eq('q1')).by('pattern').
......4> where(neq('q1')).
......5> where(gt('q1')).by('difficultyInPattern').
......6> bothE()
==>e[54][0-has->46]
==>e[55][4-has->46]
gremlin> g.V().
......1> has('Question','code','0003').as('q1').
......2> out('has').in('has').
......3> where(eq('q1')).by('pattern').
......4> where(neq('q1')).
......5> where(gt('q1')).by('difficultyInPattern').
......6> bothE()
==>e[54][0-has->46]
==>e[55][4-has->46]
sorry for the confusion. i think you understand the by() pattern just fine. <:gremlin_smile:1091719089807958067>
MadeInChimpanzee
MadeInChimpanzeeOP2y ago
thanks so much. Great tip by me as a Gremlin newbie. If I can learn a little more deeply, I would like to contribute to the project by adding documentation for beginners.
Want results from more Discord servers?
Add your server