How to use Gremlin to get both node properties and edge names in one query?

Viewed 162

I have been thrown into a pool of golang / gremlin / Neptune, and am able to get some things to work. Life is good - enough, but I am hoping there is a simple answer (which I have not been able to find) to what seems like a simple question.

I have 'obs' nodes with some properties, two of which are ('type','domain') and ('value','whitehouse.com).

Another set of nodes is 'attack' ('type','group') and ('value','Emotet'), along with other properties.

An observation node can have an edge pointing to one or more attack nodes. (and actually, other types of nodes as well.) These edges have a time-based property - when the observation was seen manifesting a certain type of attack.

I'm working in Go, using gremson to communicate with a Neptune db. In this environment you construct your query as a string and send it down the wire to Neptune, and get something called graphson back.

Thus, I construct this, and send it...

fmt.Sprintf("g.V().hasLabel('obs').has('value','%s').limit(1)", domain)

And I get back properties for a vector, in gremson. Were I using the console, all I would get back would be the id. Go figure.

Then I construct this, and send it...

fmt.Sprintf("g.V().hasLabel('obs').has('value','%s').limit(1).out()", domain)

and I get back the properties of the connected nodes, in graphson. Again, using the console I would only get back ids. No sweat.

What I would LIKE to do is to combine these two queries somehow so that I am not doing what seems to be like two almost identical lookups.

console-wise, assume both queries also have valueMap() or entityMap() tacked on the end. Is there any way to do them as one query?

1 Answers

There are many ways you could write this query. Here are a couple of options

g.V().hasLabel('obs').
      has('value','%s').
      limit(1).as('a').
      out().as('b').
      select('a','b')

or using project

g.V().hasLabel('obs').
      has('value','%s').
      limit(1).
      project('a','b').
        by().
        by(out().fold())

My preference is for the project example as you will get the connected vertices back in a list.

Related