AT
Apache TinkerPopMadeInChimpanzee

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').
M
MadeInChimpanzee388d ago
It's just a little while... can someone tell me...
S
spmallette387d 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]
G
gdotv387d ago
(psst:there's an export as query feature on the graph view in gdotv that should help with that)
M
MadeInChimpanzee387d 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!
M
MadeInChimpanzee382d 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()
M
MadeInChimpanzee382d ago
attach the data
S
spmallette382d 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.
M
MadeInChimpanzee381d 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
S
spmallette381d 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.
M
MadeInChimpanzee380d 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()
S
spmallette380d 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.
M
MadeInChimpanzee379d 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()
S
spmallette379d 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>
M
MadeInChimpanzee378d 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
More Posts
Version Update differencesHi guys, We are upgrading from gremlin_python 3.4.11 to 3.6.2. We had some functions break and it What is the ordering of group?For instance (on Tinkerpop modern) g.V().group().by('age').by('name') gives [{32=[josh], 35=[peter],Export to graphml/graphson with PythonHello everyone, I hope this question has not been asked yet, if so I'm sorry for redundant content.Publish all Q&A on the webTo my best knowledge, Discord contents are not indexed by search engine crawlers. If we can export aOut edge of vertex is slowWhen i run this below query: "g.V().has("guid", "6203620951906330066").limit(10).out("matching").proTranslating bytecode into JupyterLabs compatible script.I've found that the gremlin syntax in jupyter is different from every other language, including pythQuery if else in gremlinI have a query and that return a list vertex. I want to do a query from those vertexes like this (ifDoes Gremlin do DFS or BFS?Does Gremlin perform a Depth First Search (DFS) or Breadth First Search (BFS) when traversing?Self-service roles for providers and areas of expertiseWe have roles for providers right now as listed in #roles - they are handed out by moderators. So faMore elaborate sack examples in the docsThere are a few examples for the `sack` step, but it would be good to have a more concrete one. For Isolated vertices vs connected vertices with no join benefitIs there any downside to storing an isolated vertex with references to other nodes? Creating relatioSubgraph Strategy with vertexProperties + project().by("field name") = crashRunning the following query: `g.withStrategies(new SubgraphStrategy(vertexProperties: constant(true)Custom MutationListener on TransactionHello everyone, I'm a beginner regarding tinkerpop and i'm trying to fire my custom listener after Gremlin Query to give all related items in versioned graphI am working on a requirement where I need to store all version of a record in a Graph Database say Order group count result alphabeticallyHi! Given the following query and results in the enclosed image: how would I sort the labels alphabTransactions - tx.commit vs tx.closeI have a question related to transactions. I'm having issues with tx.commit() hanging locally when rExtracting the ProjectStep of a GraphTraversal instance during unit testing**Tl;dr** Given an instance of `GraphTraversal<?, Map<String, Object>>`, is it possible to extract tWhen can we get a non-RC release for Python 3.11 support in Production Envs?There was a bug where the version of aiohttp was too strict and blocked Python 3.11 support. ( httpsSubgraph query returns String instead of TinkerGraph Object in Fluent Java APIHi guys. I am running the following query in the console and it works fine: `g.V().has('user', 'id'Multiple Graphs in Gremlin Server?How do I manage multiple graphs? Is there an option where I can select which graph to use for query