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);
}
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);
}
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();
}
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();
}
}
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());
}
}
Aggregations