MAYA
MAYA

Reputation: 1335

How to find nodes with subset of Labels in Neo4j Embeded

I'm using the Java API and I'm looking to find nodes with a subset of Labels. In cypher, I use this query: Match(n) Where n:label1 OR n:label2 return n So, Is there any method in api for that?

Thank you

Upvotes: 0

Views: 67

Answers (2)

Tom Geudens
Tom Geudens

Reputation: 2656

You can actually run a Cypher-query embedded, so why dance ?

try (
    Transaction vTx = graphdb.beginTx();
    Result vResult = graphdb.execute("your cypher query here");
) {

    while (vResult.hasNext()) {
        Map<String, Object> vRecord = vResult.next();

        // process vRecord here
    }
    vResult.close();
    vTx.success();
}

Hope this helps.

Regards, Tom

Upvotes: 2

szenyo
szenyo

Reputation: 522

I think it is efficient if you do this in two steps. Like this:

    ResourceIterator<Node> thingAs = graphDB.findNodes( Labels.label1 );
    ResourceIterator<Node> thingBs = graphDB.findNodes( Labels.label2 );

Otherwise the identical solution should be like this:

    ResourceIterable<Node> nodes = graphDB.getAllNodes();
    while( nodes.hasNext() )
        {
         Node node = nodes.next();
         if(node.hasLabel(Labels.label1 ) || node.hasLabel(Labels.label2 ))
            return true;
        }

Upvotes: 0

Related Questions