Search code examples
sqlgraphorientdborientdb2.2

OrientDB graph query that match specific relationship


I am developing an application using OrientDB as a database. The database is already filled, and now I need to make some queries to obtain specific information.

I have 3 classes and 3 edges to be concerned. What I need to do is query the database to see if some specific relationship exists. The relationship is like this:

ParlamentarVertex --Realiza> TransacaoVertex --FornecidaPor> EmpresaFornecedoraVertex AND ParlamentarVertex --SocioDe> EmpresaFornecedoraVertex

The names with a vertex in it are a vertex of course, and the arrows are the edges between the two vertexes.

I've tried to do this:

SELECT TxNomeParlamentar, SgPartido, SgUF FROM Parlamentar where ...

SELECT EXPAND( out('RealizaTransacao').out('FornecidaPor') ) FROM Parlamentar

But I do not know how to specify the relationships after the where clause.

I've also tried to use match

MATCH {class: Parlamentar, as: p}  -Realiza-> {as:realiza}

But I am not sure how to specify the and clause that is really important for my query.

Does anyone have some tip, so I can go in the right direction? Thanks in advance!

EDIT 1

I've managed to use the query below:

SELECT EXPAND( out('RealizaTransacao').out('FornecidaPor').in('SocioDe') ) FROM Parlamentar

It almost works, but return some relationships incorrectly. It looks like a join that I did not bind the Pk and FK.


Solution

  • The easiest thing here is to use a MATCH as follows:

    MATCH 
      {class:ParlamentarVertex, as:p} -Realiza-> {class:TransacaoVertex, as:t} 
           -FornecidaPor-> {class:EmpresaFornecedoraVertex, as:e},
      {as:p} -SocioDe-> {as:e}
    RETURN p, p.TxNomeParlamentar, p.SgPartido, p.SgUF, t, e 
    

    (or RETURN whatever you need)

    As you can see, the AND is represented as the addition of multiple patterns, separated by a comma