[Neo] Neo4j Traverse API

Johan Svensson johan at neotechnology.com
Thu Feb 25 12:24:24 CET 2010


Hi,

Would it be possible to just traverse the friends looking for the
"neo4j" text on their profile? Like this:

    Node user = // get the user;
    Traverser trav = user.traverse( Order.BREADTH_FIRST,
        new StopEvStopEvaluator.END_OF_GRAPH,
ReturnableEvaluator.ALL_BUT_START_NODE,
        RelTypes.FRIEND, Direction.BOTH );
    for ( Node node : trav )
    {
        int currentDepth = trav.currentPosition().depth();
        if ( currentDepth > 2 )
        {
            // we only check second level friends
            break;
        }
        if ( userHasTextOnProfile( node, "neo4j" ) )
        {
            addNodeToResult( node, currentDepth );
        }
    }


If that is too slow you could combine a lucene search (for fulltext
search see http://wiki.neo4j.org/content/Indexing_with_IndexService#Fulltext_indexing)
and a traversal like this:

    Set<Node> nodeSet = new HashSet<Node>();
    for ( Node node : index.getNodes( "profile_text", "neo4j" ) )
    {
        nodeSet.add( node );
    }
    Node user = // get the user;
    Traverser trav = user.traverse( Order.BREADTH_FIRST,
        StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL_BUT_START_NODE,
        RelTypes.FRIEND, Direction.BOTH );
    for ( Node node : trav )
    {
        int currentDepth = trav.currentPosition().depth();
        if ( currentDepth > 2 )
        {
            // we only check second level friends
            break;
        }
        if ( nodeSet.contains( node ) )
        {
            addNodeToResult( node, currentDepth );
        }
    }


If there are very few hits on the lucene search it is more efficient
to do a shortest path search using the neo4j-graph-algo component
(0.4-SNAPSHOT):

    Node user = // get the user;
    // search using lucene and for each node do a shortest path lookup to user
    for ( Node node : index.getNodes( "profile_text", "neo4j" ) )
    {
        List<Relationship> path = new FindSingleShortestPath( user,
            node, RelTypes.FRIEND, 2 ).getPathAsRelationships();
        int depth = path.size();
        if ( depth > 0 )
        {
            addNodeToResult( node, depth );
        }
    }

Regards,
-Johan

On Thu, Feb 25, 2010 at 8:32 AM, Nishith Shah <nishith at truesparrow.com> wrote:
> Hi Satish,
>
> Can you assign the keyword that you intend to search as the property
> of the node? For example, assign 'neo4j' as a property of the node. Of
> course, it won't be possible if it's a free form search that you
> intend to do. But if you can, than traversing and sorting would be so
> much easier.
>
> -nishith
>
>
> On Thu, Feb 25, 2010 at 12:56 PM, krum at oocs.de <krum at oocs.de> wrote:
>> Hi Satish,
>>
>> if I understand you correctly, you could do the traversal in a breath first
>> fashion with
>> ... node.traverse(Order.BREATH_FIRST, ...
>> You'll get the first degree Nodes before the second dgree nodes and so forth.
>>
>> Regards,
>> Ulf
>>
>>
>>
>> Satish Varma Dandu <dsvarma at gmail.com> hat am 24. Februar 2010 um 19:37
>> geschrieben:
>>
>>> Hi John,
>>>
>>>  Thanks for the reply.  Consider a scenario like LinkedIn:
>>> 1) I wanna search for all profiles in linkedin matching "Neo4J"
>>> 2) Now i get, lets say 20 people having Neo4J on their profiles. So far so
>>> good. But i wanna order these search results based on my order. Like first i
>>> wanna search results from my direct contacts followed by next order results.
>>>
>>> The worst case scenario is, once i get these search results, for each search
>>> result profile, i need to traverse & find the path. But this take a lot of
>>> time if i get 2 many search results. So somehow i wanna combine both Lucene
>>> & traverse. Is this doable with Neo4J?
>>>
>>> Hope i explained the problem. Any help would be great.
>>>
>>> Thanks,
>>> -Satish


More information about the User mailing list