Search code examples
neo4jneo4jphp

neo4j traversal for related downloads


I'm using neo4jphp (https://github.com/jadell/neo4jphp). This is my data node and relationship. I'm trying to find "related downloads" for any one file using traversal and would like help with the code. This is my current code (it only returns who downloaded file_id = 1)

    $traversal = new Everyman\Neo4j\Traversal($this->client);
    $traversal->addRelationship('download', Relationship::DirectionIn)
            ->setPruneEvaluator(Traversal::PruneNone)
            ->setReturnFilter(Traversal::ReturnAllButStart) // ReturnAllButStart OR ReturnAll
            ->setMaxDepth(0);
    $pager = new Everyman\Neo4j\Pager($traversal, $startNode, Traversal::ReturnTypeNode);
    $pager->setPageSize(10)
            ->setLeaseTime(120);

    while ($results = $pager->getNextResults()) {
        foreach ($results as $node) {
            echo $node->getProperty('fi') . $node->getProperty('name')."\n";
        }
    }

For example if the startnode is file_id = 1, the most related download would be file_id = 3 because everyone who downloads file_id 1 also download file_id = 3. File_id 2 and 4 would be ranked 2nd.

Thank you in advance.

chart


Solution

  • When using Cypher, you'd save a lot of code:

    START file=node(1) // or startNode as named parameter
    MATCH p=file<-[:download]-()-[:download]->otherFile
    RETURN otherFile, count(*) order by count(*) desc
    

    If you want to limit to e.g. best 5 matches, amend limit 5.

    Check out https://github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries for how to use Cypher with neo4jphp.