Re: [Neo4j] How to limit Traverser

232 views
Skip to first unread message

Peter Neubauer

unread,
Feb 8, 2012, 5:16:47 AM2/8/12
to Neo4j user discussions
Emil,
there are some good docs at
http://docs.neo4j.org/chunked/snapshot/tutorials-java-embedded-traversal.html#_new_traversal_frameworkthat
you can use.

In your case, you are stopping the traversal but not excluding the nodes
you find from being part of the result. It seems you are using the first
version of the Traversal framework, where there is not only a
StopEvaluator, but even a ReturnEvaluator that determines what to return.

Alternatively, have a look at Cypher (
http://docs.neo4j.org/chunked/snapshot/tutorials-cypher-java.html from
Java) with the LIMIT keyword, see
http://docs.neo4j.org/chunked/snapshot/query-limit.html)

HTH

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Neo4j 1.6 released - dzone.com/6S4K
The Neo4j Heroku Challenge - http://neo4j-challenge.herokuapp.com/


On Wed, Feb 8, 2012 at 10:58 AM, Emil Dombagolla <
em...@hsenidoutsourcing.com> wrote:

> Hi all,
>
> Please help me.
>
> I user Traverse API . I want to get node list from the database and that
> should be limited to 2 nodes.(similar Sql limit query). How can i stop
> the traverse when it found 2 nodes.
>
> i try something as follows but still returns all the nodes found.
>
> return getRefereneNode().getUnderlyingNode().traverse(Order.BREADTH_FIRST,
> new LimitEvaluator(),
> ReturnableEvaluator.ALL_BUT_START_NODE, RelTypes.TEMP_EMAIL,
> Direction.OUTGOING);
>
> private class LimitEvaluator implements StopEvaluator{
> @Override
> public boolean isStopNode(TraversalPosition currentPos) {
> return currentPos.returnedNodesCount()>=2;
> }
> }
> Please help on this.
> Thanks
> Emil Dombagolla,
> _______________________________________________
> NOTICE: THIS MAILING LIST IS BEING SWITCHED TO GOOGLE GROUPS, please
> register and consider posting at
> https://groups.google.com/forum/#!forum/neo4j
>
> Neo4j mailing list
> Us...@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>
_______________________________________________
NOTICE: THIS MAILING LIST IS BEING SWITCHED TO GOOGLE GROUPS, please register and consider posting at https://groups.google.com/forum/#!forum/neo4j

Neo4j mailing list
Us...@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

Emil Dombagolla

unread,
Feb 8, 2012, 6:38:49 AM2/8/12
to ne...@googlegroups.com

Thank you so much your help.

You are correct i am using neo 1.4.2 embeded mode , with traverser api.

Is it not possible to limit with traverse api?

Is it possible to use both traverser and cypher in embeded mode same time?

If i am upgrading 1.4 to  lastest stable versiong will my old traverser queries break?

Thanks a lot.
Emil
--
Emil Dombagolla,
Software Engineer
hSenid Software International

Phone : +94-11-2699754  
Fax     : +94-11-2673845
mobile : +94-71-9433348

Web    : http://www.hSenid.com
"Make it Happen"

Web    : http://www.hSenidOutsourcing.com
"Making you Competitive"

Disclaimer: This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to which they are addressed. The content and opinions contained in this email are not necessarily those of hSenid Software International. If you have received this email in error please contact the sender.




Peter Neubauer

unread,
Feb 8, 2012, 6:45:07 AM2/8/12
to ne...@googlegroups.com
Emil,
your old traverser queries should be fine even if you upgrade, these
APIs have not changed AFAIK. You can limit the traversal queries, as
you have done, by returning (in the newer variant,
http://api.neo4j.org/1.6/org/neo4j/graphdb/traversal/TraversalDescription.html#evaluator(org.neo4j.graphdb.traversal.Evaluator)
 Evaluation.INCLUDE_AND_PRUNE from an Evaluator. Then, subsequent
branches on that path will not be traversed further.


HTH,


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.6 released                 - dzone.com/6S4K
The Neo4j Heroku Challenge   - http://neo4j-challenge.herokuapp.com/

Michael Hunger

unread,
Feb 8, 2012, 6:48:42 AM2/8/12
to ne...@googlegroups.com
You can also limit max_depth if you want to.

Michael

Emil Dombagolla

unread,
Feb 8, 2012, 7:03:18 AM2/8/12
to ne...@googlegroups.com
where i can apply max_depth ? and my node structure is , one to many parent child relation. so i don't think depth will help me. 
still i could not think what to do and where to start , i am dreaming if could have a sample code , i googled but no luck.
Thanks
Emil

Peter Neubauer

unread,
Feb 8, 2012, 7:06:13 AM2/8/12
to ne...@googlegroups.com
http://docs.neo4j.org/chunked/snapshot/rest-api-traverse.html#rest-api-traversal-returning-nodes-below-a-certain-depth
for a javascript depth in the evaluator,
http://docs.neo4j.org/chunked/snapshot/rest-api-traverse.html#rest-api-traversal-using-a-return-filter
for specification of the max_depth parameter .

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.6 released                 - dzone.com/6S4K
The Neo4j Heroku Challenge   - http://neo4j-challenge.herokuapp.com/

On Wed, Feb 8, 2012 at 1:03 PM, Emil Dombagolla

Emil Dombagolla

unread,
Feb 8, 2012, 7:21:59 AM2/8/12
to ne...@googlegroups.com
Sorry to bother you guys , what is meaning of following code. it returns every node as a normal traverse.Stop evaluator means it should stop the traverse when the condition met.Why it is not happening.

       private class LimitEvaluator implements StopEvaluator{
@Override
public boolean isStopNode(TraversalPosition currentPos) {
return currentPos.returnedNodesCount()==2;
}
}

thanks
Emil

Peter Neubauer

unread,
Feb 8, 2012, 7:24:24 AM2/8/12
to ne...@googlegroups.com
If you do a system.out, it might be that there are already more than 2
nodes in the result set when you get there, thus it never stops?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.6 released                 - dzone.com/6S4K
The Neo4j Heroku Challenge   - http://neo4j-challenge.herokuapp.com/

On Wed, Feb 8, 2012 at 1:21 PM, Emil Dombagolla

Mattias Persson

unread,
Feb 9, 2012, 5:50:30 AM2/9/12
to ne...@googlegroups.com
StopEvaluator doesn't stop the traversal as a whole, it stops going further down the "branch".

2012/2/8 Emil Dombagolla <em...@hsenidoutsourcing.com>



--
Mattias Persson, [mat...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com

Emil Dombagolla

unread,
Feb 9, 2012, 7:51:56 AM2/9/12
to ne...@googlegroups.com
Thanks mattias, i got the picture.
Thanks all you guys , i upgraded to 1.6 and wrote Cypher query to limit my results.

Emil dombagolla.

Emil Dombagolla

unread,
Feb 8, 2012, 4:58:16 AM2/8/12
to us...@lists.neo4j.org
Reply all
Reply to author
Forward
0 new messages