Search code examples
orientdb

orientdb stop traverse with some condition


Given this graph: enter image description here A third party may or not own a parameter.

A Parameter is represented by a context, code and value (EX: CREATE VERTEX Parameter SET context='val1', code='val2', value='val3'). A ThirdParty is represented by a type (Ex: CREATE VERTEX ThirdParty SET type=7)

Starting from vertex #40:0, i would like to retrieve the value of a given parameter(example, parameter with context='val1' and code='val2') attached to this node (a node can have different parameters). If the node doesn't own the given parameter, then i would like to traverse the parents of this node one by one till i find a value for the given parameter.

I tried using this query:

SELECT parameter.value FROM (MATCH {class:ThirdParty, 
          where: (type = 7)}.in('parent_of')
          {while: (out('owns').size() == 0), 
          where: (out('owns').size() > 0)}.out('owns')
          {as: parameter} RETURN parameter)

but the problem is, a ThirdPary can have multiple Parameters, with different context and code, and i can't find how to modify the while part in the query to compare with the given parameter (context='val1' and code='val2').


Solution

  • The query should be this one:

     SELECT parameter.value FROM (
       MATCH 
          {class:ThirdParty, where: (type = 7)}
          .in('parent_of')
          {
            while: (
               not (
                 out('owns').context contains "val1"
                 AND
                 out('owns').code contains "val2"
               )
            ), 
            where: (
              out('owns').context contains "val1"
              AND
              out('owns').code contains "val2"
            )
          }.out('owns')
          {as: parameter, where:(code = "val2" and context = "val1")} 
       RETURN parameter
     )
    

    BUT

    1. you cannot use "context" as a property name, because it refers to the query context and you won't have the result you expect

    2. I found a bug on 2.2.19 that breaks this query, I just fixed it and pushed the fix on 2.2.x branch. The fix will be released with 2.2.20. In the meantime, in a few hours you'll find the snapshot here https://oss.sonatype.org/content/repositories/snapshots/com/orientechnologies/orientdb-community/2.2.20-SNAPSHOT/