orientdb sql api edge creation not visible in gremlin - orientdb

I have situation where I insert an edge via console.sh, but the edge does not seem to get attached to the vertex when I query from gremlin.sh.
First, create classes via sql / console.sh:
CREATE CLASS BaseV ABSTRACT EXTENDS V
CREATE PROPERTY BaseV.class STRING
CREATE PROPERTY BaseV.ID STRING
CREATE INDEX BaseV.class NOTUNIQUE
CREATE INDEX BaseV.ID NOTUNIQUE
CREATE INDEX BaseVIdx ON BaseV (class, ID) UNIQUE
CREATE CLASS BaseE ABSTRACT EXTENDS E
CREATE PROPERTY BaseE.class STRING
CREATE PROPERTY BaseE.ID STRING
CREATE INDEX BaseE.class NOTUNIQUE
CREATE INDEX BaseE.ID NOTUNIQUE
CREATE INDEX BaseEIdx ON BaseE (class, ID) UNIQUE
CREATE CLASS User EXTENDS BaseV
CREATE CLASS Comment EXTENDS BaseV
CREATE CLASS Liked EXTENDS BaseE
Create test vertices and an edge gremlin:
gremlin> g.addVertex('class:User', [class: 'User', ID: '1'])
==>v(testUser)[#-1:-2]
gremlin> g.addVertex('class:Comment', [class: 'Comment', ID: '1'])
==>v(testComment)[#-1:-2]
gremlin> g.commit()
==>null
gremlin> g.V()
==>v(testUser)[#12:0]
==>v(testComment)[#13:0]
gremlin> g.addEdge('class:Liked', g.v('#12:0'), g.v('#13:0'), 'Liked')
==>e[#-1:-2][#12:0-Liked->#13:0]
gremlin> g.commit()
gremlin> g.V()
==>v(testUser)[#12:0]
==>v(testComment)[#13:0]
gremlin> g.V().out
==>v(testComment)[#13:0]
The edge is there, and available via g.V().out. Now, insert an edge via the SQL update/upsert api (console.sh):
orientdb> select * from Liked
----+-----+------+-----+-----
# |#RID |#CLASS|out |in
----+-----+------+-----+-----
0 |#14:0|Liked |#12:0|#13:0
----+-----+------+-----+-----
1 item(s) found. Query executed in 0.08 sec(s).
orientdb> update liked set class = 'Liked', ID = '1234', out = #12:0, in = #13:0, extra = "updated data" UPSERT WHERE ID = "1234"
Updated record(s) '1' in 0.084000 sec(s).
orientdb> select * from Liked
----+-----+------+-----+-----+-----+----+-----
# |#RID |#CLASS|out |in |class|ID |extra
----+-----+------+-----+-----+-----+----+-----
0 |#14:0|Liked |#12:0|#13:0|null |null|null
1 |#14:1|Liked |#12:0|#13:0|Liked|1234|data
----+-----+------+-----+-----+-----+----+-----
2 item(s) found. Query executed in 0.085 sec(s).
Lastly, query from gremlin.sh:
gremlin> g.E()
==>e[#14:0][#12:0-Used->#13:0]
==>e[#14:1][#12:0-Used->#13:0]
The edge is clearly there, but when I traverse it:
gremlin> g.v('#16:0').outE
==>e[#14:0][#12:0-Liked->#13:0]
'#14:1' should be there, but it's not.

Related

OrientDB how to match multiple related classes

I am trying to write a MATCH statement to pull related data.
CREATE CLASS Member EXTENDS V;
CREATE CLASS DirectVolumes EXTENDS V;
CREATE SEQUENCE dvSequence TYPE ORDERED;
CREATE CLASS GroupVolumes EXTENDS V;
CREATE SEQUENCE groupVolumesSequence TYPE ORDERED;
CREATE CLASS GenerationVolumes EXTENDS V;
CREATE SEQUENCE genGroupVolumesSequence TYPE ORDERED;
CREATE SEQUENCE memberIdSeq TYPE ORDERED;
CREATE VERTEX Member SET id=sequence('memberIdSeq').next();
CREATE VERTEX DirectVolumes set id=sequence('dvSequence').next();
CREATE VERTEX GroupVolumes set id=sequence('groupVolumesSequence').next();
CREATE VERTEX GenerationVolumes set id=sequence('genGroupVolumesSequence').next();
CREATE EDGE OWNS_DV_CURRENT FROM (SELECT FROM Member WHERE id = 1) TO (select from DirectVolumes where id = 1);
CREATE EDGE OWNS_PG_CURRENT FROM (SELECT FROM Member WHERE id = 1) TO (select from GroupVolumes where id = 1);
CREATE EDGE OWNS_GG_CURRENT FROM (SELECT FROM Member WHERE id = 1) TO (select from GenerationVolumes where id = 1);
Now I want Member, andOWNS_DV_CURRENT, OWNS_PG_CURRENT, OWNS_GG_CURRENT edges data together. I have removed properties for simplicity.
What is the right way to do this with a MATCH? I didn't figure out how to fetch multiple related Classes. query seems ok but it returns 0 I think it is looking for OWNS_PG_CURRENT on top of OWNS_DV_CURRENT and not on Member itself.
select from (MATCH {class: Member, as:member}.out("OWNS_DV_CURRENT"){as:dv}.out("OWNS_PG_CURRENT") {as: pg} RETURN member, dv,pg)
Try this:
MATCH
{class: Member, as:member}.out("OWNS_DV_CURRENT"){as:dv},
{as:member}.out("OWNS_PG_CURRENT") {as: pg}
RETURN member, dv,pg

OrientDB Nested JSON Insert

I have a OrientDB question. For the statement
INSERT INTO Account CONTENT
{ name : 'Luca',
vehicles : {
#class : 'Vehicle',
type : 'Car',
model : 'Maserati',
isItTrue: false
}
}
I get a result (see screenshot), but the class/table 'Vehicle' does not contain any entries. Is this a valid result?
I expected that automatically some values are written into 'Vehicle' and connected to 'Account'. Only the column 'vehicles' from 'Account' contains JSON-Data.
Thank you for your response.
Best regards
Karo
Screenshot OrientDB
With your query you are inserting embedded data into Account, if you want to insert some data into Account and Vehicle, you should do it in different queries and then if you want you can connect them with an edge. See example:
create class Account extends v
create class Vehicle extends v
create class owns extends e
create property Account.name String
create property Vehicle.type String
create property Vehicle.model String
create property Vehicle.isItTrue BOOLEAN
insert into Account(name) values ("Luca")
insert into Vehicle(type, model, isItTrue) values ("Car", "Maserati", false)
create edge owns from (select from Account where name = "Luca") to (select from Vehicle where model = "Maserati")
Alternatively, if you are not looking to create edges and you just want a link from document to document, the below will give you desired results in single insert / transaction while still keeping your json structure for the linked record
create class Account
create class Vehicle
create property Account.vehicles embedded Vehicle
INSERT INTO Account set name = "luca", vehicles = [ {
"#type": "d",
"#class" : "Vehicle",
"type" : "Car",
"model" : "Maserati",
"isItTrue": false
}]

OrientDB Select query instead of join

I have an OrientDB document database. I executed the following commands via Studio:
DROP CLASS student;
DROP CLASS studyCourse;
CREATE CLASS student;
CREATE CLASS studyCourse;
CREATE PROPERTY student.Id INTEGER;
CREATE INDEX Student.Id UNIQUE;
CREATE PROPERTY student.surname STRING;
CREATE PROPERTY student.FK_studyCourse_abbreviation STRING;
CREATE PROPERTY studyCourse.abbreviation STRING;
CREATE INDEX studyCourse.abbreviation UNIQUE;
CREATE PROPERTY studyCourse.name STRING;
CREATE LINK student TYPE LINKSET FROM student.FK_studyCourse_abbreviation TO studyCourse.abbreviation INVERSE;
INSERT INTO studyCourse SET abbreviation = 'Inf', name = 'informatics';
INSERT INTO student SET Id = '11111', surname = 'Miller';
UPDATE studyCourse ADD student = (SELECT FROM student WHERE Id = '11111') WHERE abbreviation = 'Inf';
Now I want to select values as described in the manual ( http://orientdb.com/docs/2.1/SQL.html ):
SELECT * FROM studyCourse WHERE student.surname = 'Miller';
There are no records found.
try using contains instead of =
SELECT FROM studyCourse WHERE student.surname contains 'Miller'
this is working for me:
----+-----+-----------+------------+-----------+-------
# |#RID |#CLASS |abbreviation|name |student
----+-----+-----------+------------+-----------+-------
0 |#14:0|studyCourse|Inf |informatics|[1]
----+-----+-----------+------------+-----------+-------
Ivan

ArangoDB create Vertex REST API without knowing the vertex id's

Is there a way to create with ArangoDB an Edge with REST API without knowing the Vertex ids? With a query to find the vertexs and link them?
Like this with OrientDB: create edge Uses from (select from Module where name = 'm2') to (select from Project where name = 'p1')
I don't want to query via REST the two vertex before, and after create the Edge. I don't want to use Foxx also.
Perhaps with AQL?
Thanks.
Yes, it is doable with a single AQL query:
LET from = (FOR doc IN Module FILTER doc.name == 'm2' RETURN doc._id)
LET to = (FOR doc IN Project FILTER doc.name == 'p1' RETURN doc._id)
INSERT {
_from: from[0],
_to: to[0],
/* insert other edge attributes here as needed */
someOtherAttribute: "someValue"
}
INTO nameOfEdgeCollection

Transitive Clousure in OrientDB query language

Suppose I have a vertex class called PERSON and and an edge Class called father:
CREATE CLASS PERSON EXTENDS V
CREATE CLASS father EXTENDS E
Suppose that I populated PERSON with some records. I also populated father with some records that connect certain records in PERSON to some others records of PERSON (this simply model who is father of who)
I would like to know how the following query would look like in OrientDB?
Find all ancestors of the Person, say p1 (with rid=#10:1)?
create class Person extends V
create class Father extends E
create vertex Person set name = 'grandfather' #12:0
create vertex Person set name = 'father' #12:1
create vertex Person set name = 'person' #12:2
create edge Father from #12:0 to #12:1
create edge Father from #12:1 to #12:2
I believe this is the situation described above. You can:
select from (
traverse in('Father') from #12:2
) where #rid <> #12:2
This will return all the ancestors of the person (#12:2).