Search code examples
orientdbgraph-databasesrecommendation-engine

Find most common shared vertices in OrientDB


I'm currently evaluating OrientDB (2.1.16) as a possible solution to building a similarity recommender. To that end, I'd love some help writing an initial query that accomplishes the following:

Vertex:Maker -(Edge:Produced)-> Vertex:Item -(Edge:TaggedBy)-> Vertex:Tag
  1. I'd like to select a particular Item (V1) and get a list back of other Items (Vn) ordered by the number of Tags shared in common with V1;
  2. By extension, I'd like to take a selected Maker (V2) and traverse through Items to get an ordered list of Makers (and the traversed Items, if possible) who share Tags.

There isn't an awful lot of detailed documentation on the application of intersect in this way. No unusual constraints in particular. There would be thousands of Items and Makers and probably 10x that many Tags.


Solution

  • I tried with this little graph example

    enter image description here

    I used this query

    select item.name, count(tag)from (
        select from (
            MATCH {
                CLASS:Item, AS:item, WHERE: (name<>'v1')
            } 
            .out("TaggedBy"){AS:tag}
            return item, tag
        ) where tag in (
            select expand(tag) from (
                MATCH {
                    CLASS:Item, AS:item, WHERE: (name='v1')
                }.out("TaggedBy"){AS:tag}
                return tag
            )
         )
    ) group by item order by count desc
    

    and I got this result

    enter code here

    Hope it helps.