@Deprecated @Description( "Add a relationship to an explicit index based on a specified key and value" ) @Procedure( name = "db.index.explicit.addRelationship", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION ) public Stream<BooleanResult> relationshipManualIndexAdd( @Name( "indexName" ) String explicitIndexName, @Name( "relationship" ) Relationship relationship, @Name( "key" ) String key, @Name( "value" ) Object value ) { graphDatabaseAPI.index().forRelationships( explicitIndexName ).add( relationship, key, value ); // Failures will be expressed as exceptions before the return return Stream.of( new BooleanResult( Boolean.TRUE ) ); }
Relationship rel1 = startNode.createRelationshipTo( endNode1, type ); Relationship rel2 = startNode.createRelationshipTo( endNode2, type ); index.add( rel1, "name", "something" ); index.add( rel2, "name", "something" ); assertThat( index.query( "name:something" ), Contains.contains( rel1, rel2 ) ); assertThat( index.query( "name:something", null, endNode1 ), Contains.contains( rel1 ) ); assertThat( index.query( "name:something", startNode, endNode2 ), Contains.contains( rel2 ) ); assertThat( index.query( null, startNode, endNode1 ), Contains.contains( rel1 ) ); assertThat( index.get( "name", "something", null, endNode1 ), Contains.contains( rel1 ) ); assertThat( index.get( "name", "something", startNode, endNode2 ), Contains.contains( rel2 ) ); assertThat( index.get( null, null, startNode, endNode1 ), Contains.contains( rel1 ) ); endNode1.delete(); endNode2.delete(); index.delete();
@Test public void makeSureYouCanRemoveFromRelationshipIndex() { Node n1 = graphDb.createNode(); Node n2 = graphDb.createNode(); Relationship r = n1.createRelationshipTo( n2, 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" ), emptyIterable() ); assertThat( index.get( key, "otherValue" ), Contains.contains( r ) ); restartTx(); } }
@Override public IndexHits<Relationship> query( Object queryOrQueryObjectOrNull, Node startNodeOrNull, Node endNodeOrNull ) { return delegate.query( queryOrQueryObjectOrNull, startNodeOrNull, endNodeOrNull ); } }
@Test public void question5346011() { GraphDatabaseService service = new TestGraphDatabaseFactory().newImpermanentDatabase(); try ( Transaction tx = service.beginTx() ) { RelationshipIndex index = service.index().forRelationships( "exact" ); // ...creation of the nodes and relationship Node node1 = service.createNode(); Node node2 = service.createNode(); String uuid = "xyz"; Relationship relationship = node1.createRelationshipTo( node2, RelationshipType.withName( "related" ) ); index.add( relationship, "uuid", uuid ); // query try ( IndexHits<Relationship> hits = index.get( "uuid", uuid, node1, node2 ) ) { assertEquals( 1, hits.size() ); } tx.success(); } service.shutdown(); } }
@Test public void shouldThrowIllegalArgumentChangingTypeOfFieldOnRelationshipIndex() { String indexName = "index"; createRelationshipExplicitIndexWithSingleRelationship( db, indexName ); long relId; try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); Relationship rel = node.createRelationshipTo( node, TYPE ); relId = rel.getId(); RelationshipIndex index = db.index().forRelationships( indexName ); index.add( rel, "key", "otherValue" ); tx.success(); } try ( Transaction tx = db.beginTx() ) { RelationshipIndex index = db.index().forRelationships( indexName ); index.remove( db.getRelationshipById( relId ), "key" ); tx.success(); } expectedException.expect( IllegalArgumentException.class ); try ( Transaction tx = db.beginTx() ) { RelationshipIndex index = db.index().forRelationships( indexName ); index.add( db.getRelationshipById( relId ), "key", ValueContext.numeric( 52 ) ); tx.success(); } }
public void removeRelationshipIndex( String indexName ) { if ( !graphDb.index().existsForRelationships( indexName ) ) { throw new NotFoundException( "No relationship index named '" + indexName + "'." ); } graphDb.index().forRelationships( indexName ).delete(); }
public void removeFromRelationshipIndexNoValue( String indexName, String key, long id ) { graphDb.index().forRelationships( indexName ).remove( graphDb.getRelationshipById( id ), key ); }
@Override public IndexHits<Relationship> get( String key, Object valueOrNull, Node startNodeOrNull, Node endNodeOrNull ) { return delegate.get( key, valueOrNull, startNodeOrNull, endNodeOrNull ); }
/** * {@inheritDoc} * <p/> * Note that this method will force a successful closing of the current * thread's transaction. As such, once the index is dropped, the operation * is committed. * * @param indexName the name of the index to drop */ public synchronized void dropIndex(final String indexName) { this.autoStartTransaction(true); if (this.rawGraph.index().existsForNodes(indexName)) { org.neo4j.graphdb.index.Index<Node> nodeIndex = this.rawGraph.index().forNodes(indexName); if (nodeIndex.isWriteable()) { nodeIndex.delete(); } } else if (this.rawGraph.index().existsForRelationships(indexName)) { RelationshipIndex relationshipIndex = this.rawGraph.index().forRelationships(indexName); if (relationshipIndex.isWriteable()) { relationshipIndex.delete(); } } this.commit(); }
@Override public IndexHits<Relationship> query( String key, Object queryOrQueryObjectOrNull, Node startNodeOrNull, Node endNodeOrNull ) { return delegate.query( key, queryOrQueryObjectOrNull, startNodeOrNull, endNodeOrNull ); }
@Deprecated @Description( "Remove an explicit index - YIELD type,name,config" ) @Procedure( name = "db.index.explicit.drop", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION ) public Stream<ExplicitIndexInfo> manualIndexDrop( @Name( "indexName" ) String explicitIndexName ) { IndexManager mgr = graphDatabaseAPI.index(); List<ExplicitIndexInfo> results = new ArrayList<>( 2 ); if ( mgr.existsForNodes( explicitIndexName ) ) { Index<Node> index = mgr.forNodes( explicitIndexName ); results.add( new ExplicitIndexInfo( "NODE", explicitIndexName, mgr.getConfiguration( index ) ) ); index.delete(); } if ( mgr.existsForRelationships( explicitIndexName ) ) { RelationshipIndex index = mgr.forRelationships( explicitIndexName ); results.add( new ExplicitIndexInfo( "RELATIONSHIP", explicitIndexName, mgr.getConfiguration( index ) ) ); index.delete(); } return results.stream(); }
public void removeFromRelationshipIndex( String indexName, String key, String value, long id ) { graphDb.index().forRelationships( indexName ).remove( graphDb.getRelationshipById( id ), key, value ); }
public Collection<Long> getIndexedRelationships( String indexName, String key, Object value ) { try ( Transaction tx = database.getGraph().beginTransaction( implicit, AnonymousContext.write() ) ) { Collection<Long> result = new ArrayList<>(); for ( Relationship relationship : database.getGraph().index().forRelationships( indexName ).get( key, value ) ) { result.add( relationship.getId() ); } tx.success(); return result; } }
@Test public void shouldNotBeAbleToAddNullValuesToRelationshipIndex() { // GIVEN RelationshipIndex index = relationshipIndex( EXACT_CONFIG ); // WHEN single null try { index.add( graphDb.createNode().createRelationshipTo( graphDb.createNode(), MyRelTypes.TEST ), "key", null ); fail( "Should have failed" ); } catch ( IllegalArgumentException e ) { // THEN Good } // WHEN null in array try { index.add( graphDb.createNode().createRelationshipTo( graphDb.createNode(), MyRelTypes.TEST ), "key", new String[] {"a", null, "c"} ); fail( "Should have failed" ); } catch ( IllegalArgumentException e ) { // THEN Good } }
private boolean relationshipExistsByQuery( RelationshipIndex index, Node startNode, Node endNode, boolean specifyStartNode ) { boolean found = false; try ( Transaction tx = db.beginTx(); IndexHits<Relationship> query = index .query( "key", QueryContext.numericRange( "key", 0, 3 ), specifyStartNode ? startNode : null, null ) ) { for ( Relationship relationship : query ) { if ( relationship.getStartNodeId() == startNode.getId() && relationship.getEndNodeId() == endNode.getId() ) { found = true; break; } } tx.success(); } return found; }
@Test public void shouldNotSeeDeletedRelationshipWhenQueryingWithStartAndEndNode() { // GIVEN RelationshipIndex index = relationshipIndex( EXACT_CONFIG ); Node start = graphDb.createNode(); Node end = graphDb.createNode(); RelationshipType type = withName( "REL" ); Relationship rel = start.createRelationshipTo( end, type ); index.add( rel, "Type", type.name() ); finishTx( true ); beginTx(); // WHEN IndexHits<Relationship> hits = index.get( "Type", type.name(), start, end ); assertEquals( 1, count( hits ) ); assertEquals( 1, hits.size() ); index.remove( rel ); // THEN hits = index.get( "Type", type.name(), start, end ); assertEquals( 0, count( hits ) ); assertEquals( 0, hits.size() ); }