use of org.neo4j.graphdb.index.IndexManager in project neo4j-clean-remote-db-addon by jexp.
the class Neo4jDatabaseCleaner method clearIndex.
private void clearIndex(Map<String, Object> result) {
IndexManager indexManager = graph.index();
result.put("node-indexes", Arrays.asList(indexManager.nodeIndexNames()));
result.put("relationship-indexes", Arrays.asList(indexManager.relationshipIndexNames()));
try {
for (String ix : indexManager.nodeIndexNames()) {
final Index<Node> index = indexManager.forNodes(ix);
getMutableIndex(index).delete();
}
for (String ix : indexManager.relationshipIndexNames()) {
final RelationshipIndex index = indexManager.forRelationships(ix);
getMutableIndex(index).delete();
}
} catch (UnsupportedOperationException uoe) {
throw new RuntimeException("Implementation detail assumption failed for cleaning readonly indexes, please make sure that the version of this extension and the Neo4j server align");
}
}
use of org.neo4j.graphdb.index.IndexManager 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.IndexManager in project graphdb by neo4j-attic.
the class ImdbExampleTest method removeFromIndex.
@Test
public void removeFromIndex() {
IndexManager index = graphDb.index();
Index<Node> actors = index.forNodes("actors");
Node bellucci = actors.get("name", "Monica Bellucci").getSingle();
assertNotNull(bellucci);
// START SNIPPET: removeNodeFromIndex
// completely remove bellucci from the actors index
actors.remove(bellucci);
// END SNIPPET: removeNodeFromIndex
Node node = actors.get("name", "Monica Bellucci").getSingle();
assertEquals(null, node);
node = actors.get("name", "La Bellucci").getSingle();
assertEquals(null, node);
rollbackTx();
// START SNIPPET: removeNodeFromIndex
// remove any "name" entry of bellucci from the actors index
actors.remove(bellucci, "name");
// END SNIPPET: removeNodeFromIndex
node = actors.get("name", "Monica Bellucci").getSingle();
assertEquals(null, node);
node = actors.get("name", "La Bellucci").getSingle();
assertEquals(null, node);
rollbackTx();
// START SNIPPET: removeNodeFromIndex
// remove the "name" -> "La Bellucci" entry of bellucci
actors.remove(bellucci, "name", "La Bellucci");
// END SNIPPET: removeNodeFromIndex
node = actors.get("name", "La Bellucci").getSingle();
assertEquals(null, node);
node = actors.get("name", "Monica Bellucci").getSingle();
assertEquals(bellucci, node);
}
use of org.neo4j.graphdb.index.IndexManager in project graphdb by neo4j-attic.
the class ImdbExampleTest method checkIfIndexExists.
@Test
public void checkIfIndexExists() {
// START SNIPPET: checkIfExists
IndexManager index = graphDb.index();
boolean indexExists = index.existsForNodes("actors");
// END SNIPPET: checkIfExists
assertTrue(indexExists);
}
use of org.neo4j.graphdb.index.IndexManager in project graphdb by neo4j-attic.
the class ImdbExampleTest method doQueriesForNodes.
@Test
public void doQueriesForNodes() {
IndexManager index = graphDb.index();
Index<Node> actors = index.forNodes("actors");
Index<Node> movies = index.forNodes("movies");
Set<String> found = new HashSet<String>();
@SuppressWarnings("serial") Set<String> expectedActors = new HashSet<String>() {
{
add("Monica Bellucci");
add("Keanu Reeves");
}
};
@SuppressWarnings("serial") Set<String> expectedMovies = new HashSet<String>() {
{
add("The Matrix");
}
};
// START SNIPPET: actorsQuery
for (Node actor : actors.query("name", "*e*")) {
// This will return Reeves and Bellucci
// END SNIPPET: actorsQuery
found.add((String) actor.getProperty("name"));
// START SNIPPET: actorsQuery
}
// END SNIPPET: actorsQuery
assertEquals(expectedActors, found);
found.clear();
// START SNIPPET: matrixQuery
for (Node movie : movies.query("title:*Matrix* AND year:1999")) {
// This will return "The Matrix" from 1999 only.
// END SNIPPET: matrixQuery
found.add((String) movie.getProperty("title"));
// START SNIPPET: matrixQuery
}
// END SNIPPET: matrixQuery
assertEquals(expectedMovies, found);
// START SNIPPET: matrixSingleQuery
Node matrix = movies.query("title:*Matrix* AND year:2003").getSingle();
// END SNIPPET: matrixSingleQuery
assertEquals("The Matrix Reloaded", matrix.getProperty("title"));
// START SNIPPET: queryWithScore
IndexHits<Node> hits = movies.query("title", "The*");
for (Node movie : hits) {
System.out.println(movie.getProperty("title") + " " + hits.currentScore());
// END SNIPPET: queryWithScore
assertTrue(((String) movie.getProperty("title")).startsWith("The"));
// START SNIPPET: queryWithScore
}
// END SNIPPET: queryWithScore
assertEquals(2, hits.size());
// START SNIPPET: queryWithRelevance
hits = movies.query("title", new QueryContext("The*").sortByScore());
// END SNIPPET: queryWithRelevance
float previous = Float.MAX_VALUE;
// START SNIPPET: queryWithRelevance
for (Node movie : hits) {
// hits sorted by relevance (score)
// END SNIPPET: queryWithRelevance
assertTrue(hits.currentScore() <= previous);
previous = hits.currentScore();
// START SNIPPET: queryWithRelevance
}
// END SNIPPET: queryWithRelevance
assertEquals(2, hits.size());
// START SNIPPET: termQuery
// a TermQuery will give exact matches
Node actor = actors.query(new TermQuery(new Term("name", "Keanu Reeves"))).getSingle();
// END SNIPPET: termQuery
assertEquals("Keanu Reeves", actor.getProperty("name"));
Node theMatrix = movies.get("title", "The Matrix").getSingle();
Node theMatrixReloaded = movies.get("title", "The Matrix Reloaded").getSingle();
// START SNIPPET: wildcardTermQuery
hits = movies.query(new WildcardQuery(new Term("title", "The Matrix*")));
for (Node movie : hits) {
System.out.println(movie.getProperty("title"));
// END SNIPPET: wildcardTermQuery
assertTrue(((String) movie.getProperty("title")).startsWith("The Matrix"));
// START SNIPPET: wildcardTermQuery
}
// END SNIPPET: wildcardTermQuery
assertEquals(2, hits.size());
// START SNIPPET: numericRange
movies.add(theMatrix, "year-numeric", new ValueContext(1999L).indexNumeric());
movies.add(theMatrixReloaded, "year-numeric", new ValueContext(2003L).indexNumeric());
// Query for range
long startYear = 1997;
long endYear = 2001;
hits = movies.query(NumericRangeQuery.newLongRange("year-numeric", startYear, endYear, true, true));
// END SNIPPET: numericRange
assertEquals(theMatrix, hits.getSingle());
// START SNIPPET: compoundQueries
hits = movies.query("title:*Matrix* AND year:1999");
// END SNIPPET: compoundQueries
assertEquals(theMatrix, hits.getSingle());
// START SNIPPET: defaultOperator
QueryContext query = new QueryContext("title:*Matrix* year:1999").defaultOperator(Operator.AND);
hits = movies.query(query);
// END SNIPPET: defaultOperator
// with OR the result would be 2 hits
assertEquals(1, hits.size());
// START SNIPPET: sortedResult
hits = movies.query("title", new QueryContext("*").sort("title"));
for (Node hit : hits) {
// all movies with a title in the index, ordered by title
}
// END SNIPPET: sortedResult
assertEquals(3, hits.size());
// START SNIPPET: sortedResult
// or
hits = movies.query(new QueryContext("title:*").sort("year", "title"));
for (Node hit : hits) {
// all movies with a title in the index, ordered by year, then title
}
// END SNIPPET: sortedResult
assertEquals(3, hits.size());
}
Aggregations