Search in sources :

Example 16 with RelationshipIndex

use of org.neo4j.graphdb.index.RelationshipIndex in project graphdb by neo4j-attic.

the class ImdbExampleTest method doQueriesForRelationships.

@Test
public void doQueriesForRelationships() {
    IndexManager index = graphDb.index();
    RelationshipIndex roles = index.forRelationships("roles");
    Index<Node> actors = graphDb.index().forNodes("actors");
    Index<Node> movies = index.forNodes("movies");
    Node reeves = actors.get("name", "Keanu Reeves").getSingle();
    Node theMatrix = movies.get("title", "The Matrix").getSingle();
    // START SNIPPET: queryForRelationships
    // find relationships filtering on start node
    // using exact matches
    IndexHits<Relationship> reevesAsNeoHits;
    reevesAsNeoHits = roles.get("name", "Neo", reeves, null);
    Relationship reevesAsNeo = reevesAsNeoHits.iterator().next();
    reevesAsNeoHits.close();
    // END SNIPPET: queryForRelationships
    assertEquals("Neo", reevesAsNeo.getProperty("name"));
    Node actor = reevesAsNeo.getStartNode();
    assertEquals(reeves, actor);
    // START SNIPPET: queryForRelationships
    // find relationships filtering on end node
    // using a query
    IndexHits<Relationship> matrixNeoHits;
    matrixNeoHits = roles.query("name", "*eo", null, theMatrix);
    Relationship matrixNeo = matrixNeoHits.iterator().next();
    matrixNeoHits.close();
    // END SNIPPET: queryForRelationships
    assertEquals("Neo", matrixNeo.getProperty("name"));
    actor = matrixNeo.getStartNode();
    assertEquals(reeves, actor);
    // START SNIPPET: queryForRelationshipType
    // find relationships filtering on end node
    // using a relationship type.
    // this is how to add it to the index:
    roles.add(reevesAsNeo, "type", reevesAsNeo.getType().name());
    // Note that to use a compound query, we can't combine committed
    // and uncommitted index entries, so we'll commit before querying:
    tx.success();
    tx.finish();
    // and now we can search for it:
    IndexHits<Relationship> typeHits;
    typeHits = roles.query("type:ACTS_IN AND name:Neo", null, theMatrix);
    Relationship typeNeo = typeHits.iterator().next();
    typeHits.close();
    // END SNIPPET: queryForRelationshipType
    assertEquals("Neo", typeNeo.getProperty("name"));
    actor = matrixNeo.getStartNode();
    assertEquals(reeves, actor);
}
Also used : IndexManager(org.neo4j.graphdb.index.IndexManager) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) Test(org.junit.Test)

Example 17 with RelationshipIndex

use of org.neo4j.graphdb.index.RelationshipIndex in project graphdb by neo4j-attic.

the class ImdbExampleTest method doGetForRelationships.

// @Test
// public void getSameFromDifferentValuesO
@Test
public void doGetForRelationships() {
    RelationshipIndex roles = graphDb.index().forRelationships("roles");
    // START SNIPPET: getSingleRelationship
    Relationship persephone = roles.get("name", "Persephone").getSingle();
    Node actor = persephone.getStartNode();
    Node movie = persephone.getEndNode();
    // END SNIPPET: getSingleRelationship
    assertEquals("Monica Bellucci", actor.getProperty("name"));
    assertEquals("The Matrix Reloaded", movie.getProperty("title"));
    @SuppressWarnings("serial") List<String> expectedActors = new ArrayList<String>() {

        {
            add("Keanu Reeves");
            add("Keanu Reeves");
        }
    };
    List<String> foundActors = new ArrayList<String>();
    // START SNIPPET: getRelationships
    for (Relationship role : roles.get("name", "Neo")) {
        // this will give us Reeves twice
        Node reeves = role.getStartNode();
        // END SNIPPET: getRelationships
        foundActors.add((String) reeves.getProperty("name"));
    // START SNIPPET: getRelationships
    }
    // END SNIPPET: getRelationships
    assertEquals(expectedActors, foundActors);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) Test(org.junit.Test)

Example 18 with RelationshipIndex

use of org.neo4j.graphdb.index.RelationshipIndex in project graphdb by neo4j-attic.

the class TestLuceneIndex method testNodeLocalRelationshipIndex.

@Test
public void testNodeLocalRelationshipIndex() {
    RelationshipIndex index = relationshipIndex("locality", LuceneIndexImplementation.EXACT_CONFIG);
    RelationshipType type = DynamicRelationshipType.withName("YO");
    Node startNode = graphDb.createNode();
    Node endNode1 = graphDb.createNode();
    Node endNode2 = graphDb.createNode();
    Relationship rel1 = startNode.createRelationshipTo(endNode1, type);
    Relationship rel2 = startNode.createRelationshipTo(endNode2, type);
    index.add(rel1, "name", "something");
    index.add(rel2, "name", "something");
    for (int i = 0; i < 2; i++) {
        assertThat(index.query("name:something"), contains(rel1, rel2));
        assertThat(index.query("name:something", null, endNode1), contains(rel1));
        assertThat(index.query("name:something", startNode, endNode2), contains(rel2));
        assertThat(index.query(null, startNode, endNode1), contains(rel1));
        assertThat(index.get("name", "something", null, endNode1), contains(rel1));
        assertThat(index.get("name", "something", startNode, endNode2), contains(rel2));
        assertThat(index.get(null, null, startNode, endNode1), contains(rel1));
        restartTx();
    }
    rel2.delete();
    rel1.delete();
    startNode.delete();
    endNode1.delete();
    endNode2.delete();
    index.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) DynamicRelationshipType(org.neo4j.graphdb.DynamicRelationshipType) RelationshipType(org.neo4j.graphdb.RelationshipType) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) Test(org.junit.Test)

Example 19 with RelationshipIndex

use of org.neo4j.graphdb.index.RelationshipIndex in project graphdb by neo4j-attic.

the class TestLuceneIndex method makeSureYouCanRemoveFromRelationshipIndex.

@Test
public void makeSureYouCanRemoveFromRelationshipIndex() {
    Node n1 = graphDb.createNode();
    Node n2 = graphDb.createNode();
    Relationship r = n1.createRelationshipTo(n2, DynamicRelationshipType.withName("foo"));
    RelationshipIndex index = graphDb.index().forRelationships("rel-index");
    String key = "bar";
    index.remove(r, key, "value");
    index.add(r, key, "otherValue");
    for (int i = 0; i < 2; i++) {
        assertThat(index.get(key, "value"), isEmpty());
        assertThat(index.get(key, "otherValue"), contains(r));
        restartTx();
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) Test(org.junit.Test)

Example 20 with RelationshipIndex

use of org.neo4j.graphdb.index.RelationshipIndex in project neo4j-mobile-android by neo4j-contrib.

the class DbWrapper method removeRelationshipKeyValueFromIndex.

@Override
public void removeRelationshipKeyValueFromIndex(String name, long relationshipId, String key, ParcelableIndexValue value, ParcelableError err) throws RemoteException {
    try {
        checkCallerHasWritePermission();
        resumeTrx();
        try {
            Relationship rel = mDb.getRelationshipById(relationshipId);
            // this
            RelationshipIndex index = mDb.index().forRelationships(name);
            // will
            // create
            // the
            // index
            index.remove(rel, key, value.get());
        } finally {
            suspendCurrentTrx("removeRelationshipKeyValueFromIndex");
        }
    } catch (Exception e) {
        Log.e(TAG, "Failed to add relationship to index '" + name + "'", e);
        err.setError(Errors.TRANSACTION, e.getMessage());
    }
}
Also used : ParcelableRelationship(org.neo4j.android.common.ParcelableRelationship) Relationship(org.neo4j.graphdb.Relationship) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) RemoteException(android.os.RemoteException) InvalidTransactionException(org.neo4j.javax.transaction.InvalidTransactionException) SystemException(org.neo4j.javax.transaction.SystemException)

Aggregations

RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)26 Relationship (org.neo4j.graphdb.Relationship)18 Test (org.junit.Test)13 Node (org.neo4j.graphdb.Node)13 RemoteException (android.os.RemoteException)7 Transaction (org.neo4j.graphdb.Transaction)7 InvalidTransactionException (org.neo4j.javax.transaction.InvalidTransactionException)7 SystemException (org.neo4j.javax.transaction.SystemException)7 ParcelableRelationship (org.neo4j.android.common.ParcelableRelationship)6 RelationshipType (org.neo4j.graphdb.RelationshipType)3 IndexManager (org.neo4j.graphdb.index.IndexManager)3 DynamicRelationshipType (org.neo4j.graphdb.DynamicRelationshipType)2 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)2 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 BeforeClass (org.junit.BeforeClass)1 LuceneTimeline (org.neo4j.index.lucene.LuceneTimeline)1 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)1 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)1