0

I'm writing a gremlin python query with the intention that

  1. Create an edge if it doesn't exist
  2. Update the edge properties if it does

I read some answers here already and got the first part figured out using coalesce but I'm not sure how to update the edge if it exists. I would imagine that would happen in the first part of the coalesce but I tried select edge and that didn't work

Here is what I have so far

  g.V().
  hasLabel('person').as_('p').
  V().
  hasLabel('house').as_('h').
  coalesce(
    __.inE('owns').where(__.outV().as_('p')),
    __.addE('owns').from_('p').to('h').
    property(Cardinality.set_, 'duration', 2)).
  iterate()
bandzbond
  • 5
  • 2

1 Answers1

1

Before writing the query, you need to correct your assumption on Edge property cardinality. From your query, it looks like you want to update the property of the edge with Set cardinality.

Tinkerpop does not support Set cardinality on Edge properties.

TLDR answer:

g.V().
  hasLabel('house').as('p').
  inE('owns').
  where(outV().hasLabel('person')).
  fold().
  coalesce(
    unfold().property('duration', 2),
    addE('owns').from(V().hasLabel('person')).to(V().hasLabel('house')))

Above query is based on assumption that you only have 2 vertices in the database, first with label 'house' and second with label 'person'

If above information is incorrect then you would need to update your vertices filter to point them to single Vertex (unless you are planning to add mass edges).

  • I forgot to mention that I'm using Amazon Neptune where Set cardinality is allowed. I have multiple houses and people. Can I not just use p and h in the end instead of the hasLabel again? – bandzbond Oct 10 '21 at 19:55
  • Also I would like to understad why I cant access the edge from my statement "__.inE('owns').where(__.outV().as_('p'))" in the coalesce call. Should I be able to just call property there? – bandzbond Oct 10 '21 at 20:03
  • Set cardinality is only allowed on Vertices. It is not allowed on Edges. – Kelvin Lawrence Oct 11 '21 at 00:55
  • In your original query you are going to do more work than likely expected as the second `V` will fan out multiple times so you will end up adding multiple edges for each person. Also in your original query the label `e` is not defined before it is referenced. – Kelvin Lawrence Oct 11 '21 at 01:05
  • yes that's my fault e should be p. I have 400 people and 100,000 houses so yeah there will be multiple edges (40,000,000 at max). – bandzbond Oct 11 '21 at 14:15
  • My question is there a way to access the edges in __.inE('owns').where(__.outV().as_('p')) my goal is to create an edge or update it if exists – bandzbond Oct 11 '21 at 14:15
  • Yes, edges will flow out of that such as `gremlin> g.V('3').as('p').out().inE().where(__.outV().as('p')).limit(1) ==>e[5375][3-route->151]` – Kelvin Lawrence Oct 11 '21 at 14:59
  • Somehow vertices are flowing out. First time I run the command when I have 0 edges it works, because it goes to the addE statement. Second time I run it, it recognizes these edges exist but then when it hits the .proprerty, it gives this error (below after the command) g.V().hasLabel('person').as_('p').V().hasLabel('home').as_('h').coalesce(__.inE('owns').where(__.outV().as_('p')).property(Cardinality.set_, 'someProperty',1),__.addE('owns').from_('p').to('h').property(Cardinality.set_, 'someProperty',0)).iterate() – bandzbond Oct 11 '21 at 21:47
  • GremlinServerError: 498: {"code":"UnsupportedOperationException","requestId":"a6ecb42d-5483-4d38-aa07-a8c2d0ad2d29","detailedMessage":"com.amazon.neptune.tinkerpop.structure.NeptuneEdge cannot be cast to org.apache.tinkerpop.gremlin.structure.Vertex"} – bandzbond Oct 11 '21 at 21:48
  • As stated above a few times you cannot add Set cardinality properties to an Edge. That is what the error is telling you. This is defined by ApacheTinkerPop as the desired behavior and what you see is Neptune enforcing that behavior. – Kelvin Lawrence Oct 12 '21 at 13:45