OrientDB 3.x - orientdb-gremlin - Edge Index Lookup - orientdb

I created an inout edge index. How can I now iterate over the edges if I know the in and out elementId?
With Tinkerpop 2.4 this was done via:
Iterable edges = orientBaseGraph.getEdges("e." + edgeLabel + "_inout", new OCompositeKey(fromId, toId));
What is the Tinkerpop 3.x / orientdb-gremlin way of doing this task?

Related

OrientDB 3.x - orientdb-gremlin - Vertex Index Lookup

What is the correct way to utilize a previously created vertex index within a traversal?
I want find the vertex with a specific key/value. I thus created a unique index on a specific vertex class.
My traversal works but I have the feeling index is not being utilized (I inspected the call tree)
Vertex v = getGraph().getRawTraversal().V().has("testkey", P.eq("testvalue")).next();
My main reference are orientdb-gremlin unit tests:
https://github.com/orientechnologies/orientdb-gremlin/blob/58e604b1b5f5d26e545f7a4beb9640b8a342d822/driver/src/test/java/org/apache/tinkerpop/gremlin/orientdb/OrientGraphIndexTest.java#L157
The main doc does not yet contain any references to index usage. (Presumable because 3.x has not yet been released)
https://orientdb.com/docs/3.0.x/tinkerpop3/OrientDB-TinkerPop3.html

Update edge property value using gremlin scala in titan + cassandra DB

I am trying to update an edge property value of outgoing Edges (outE) using gremlin scala.
Environment:
Titan + Cassandra
graph.V().outE().properties("Id","100").iterate()
Above is not working. Is there any other way to update the edge property value.
You have a minor error. Your traversal is doing a lookup. To add the property to all the edges you do the following:
graph.traversal().V().outE().property("Id","100").iterate();
property mutates. properties does a lookup. More info here

Can we UPSERT an EDGE in orientdb?

Is it possible to get an example to upsert an edge in orientdb. IF it does not exist is there a way to check if the edge exist, if it does then just update the edge else create a new edge. I am using Orientdb 2.1.13 version.
Thank you
via SQL you can use the basic UPDATE command
update written_by SET out = #9:2, in = #16:43, prop="gianni" UPSERT WHERE out = #9:2 and in = #16:43
http://orientdb.com/docs/last/SQL-Update.html
When you use "update written_by SET out = #9:2, in = #16:43, prop="gianni" UPSERT WHERE out = #9:2 and in = #16:43", it doesn't work properly for edges: it will create edge if it doesn't exist, but it will not create in and out properties in vertex, so, for example, you will not able to query MATCH. It works this way cos “The UPDATE/UPSERT works at document level, so it doesn't create the connections from the vertices. Using it, you will have a broken graph”, as authors said.
But you can use “upsert” for edges since version 3.0.1 and it will work properly – but you need to do the following:
Create unique index on edge_class (out, in) and – it's strange – The order is important!
To do this, you need to create in and out properties first, otherwise db can't create index and there will be an exception when you will try to run command “Create index”.
Then, use command CREATE EDGE UPSERT FROM TO .
In this case edge will be created only if it is not exists, and it will create in and out properties for vertex classes.

UPSERTing with TitanDB

I'm having my first steps as a TitanDB user. That being, I'd like to know how to make an upsert / conditionally insert a vertex inside a TitanTransaction (in the style of "get or create").
I have a unique index on the vertex/property I want to create/lookup.
Here's a one-liner "getOrCreate" for Titan 1.0 and TinkerPop 3:
getOrCreate = { id ->
g.V().has('userId', id).tryNext().orElseGet{ g.addV('userId', id).next() }
}
As taken from the new TinkerPop "Getting Started" Tutorial. Here is the same code translated to java:
public Vertex getOrCreate(Object id) {
return g.V().has('userId', id).tryNext().orElseGet(() -> g.addV('userId', id).next());
}
Roughly speaking, every Cassandra insert is an "upsert". If you look at the Titan representation of vertices and edges in a Cassandra-like model, you'll find vertices and edges each get their own rows. This means a blind write of an edge will have the given behavior you're looking for: what you write is what will win. Doing this with a vertex isn't supported directly by Titan.
But I don't think this is what you're looking for. If you're looking to enforce uniqueness, why not use the unique() modifier on a Titan composite index? (From the documentation):
mgmt.buildIndex('byNameUnique', Vertex.class).addKey(name).unique().buildCompositeIndex()
With a Cassandra storage backend, you need to enable consistency locking. As with any database, managing the overhead of uniqueness comes at a cost which you need to consider when writing your data. This way if you insert a vertex which violates your uniqueness requirement, the transaction will fail.

Make an index unique after creation in Titan

If I create an index according to the docs (http://s3.thinkaurelius.com/docs/titan/0.5.4/indexes.html) without making it unique is it possible to make it unique after? I have not added any vertices or edges to the graph, just created the index.
Something like:
index = mgmt.getGraphIndex('name')
index.unique()
I am using the Gremlin console to make these changes.
Is it possible to do this somehow?
This is a documented limitation of Titan.
Ref : http://s3.thinkaurelius.com/docs/titan/0.5.0/limitations.html
section - 14.2.1. Unable to Drop Indices
Since no vertices or edges are added to graph, try the below gremlin command.
g.V.remove() or g.V.each{g.removeVertex(it)}
g.commit()
Then try to create the indexes again with .unique().
If still unable to re-create the indices, try to clean storage-backend.
In case of cassandra "DROP Keyspace titan;"
This must definitely work,I have tried in Titan 0.4 and worked.