private void assertIsLuceneIndex( GraphDatabaseService db, Index<T> index ) { Map<String, String> config = db.index().getConfiguration( index ); if ( !config.get( IndexManager.PROVIDER ).equals( "lucene" ) ) // Not so hard coded please { throw new IllegalArgumentException( index + " isn't a Lucene index" ); } }
@Deprecated @Description( "List all explicit indexes - YIELD type,name,config" ) @Procedure( name = "db.index.explicit.list", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION ) public Stream<ExplicitIndexInfo> list() { IndexManager mgr = graphDatabaseAPI.index(); List<ExplicitIndexInfo> indexInfos = new ArrayList<>( 100 ); for ( String name : mgr.nodeIndexNames() ) { Index<Node> index = mgr.forNodes( name ); indexInfos.add( new ExplicitIndexInfo( "NODE", name, mgr.getConfiguration( index ) ) ); } for ( String name : mgr.relationshipIndexNames() ) { RelationshipIndex index = mgr.forRelationships( name ); indexInfos.add( new ExplicitIndexInfo( "RELATIONSHIP", name, mgr.getConfiguration( index ) ) ); } return indexInfos.stream(); }
@Override protected void serialize( MappingSerializer serializer ) { for ( String indexName : indexManager.relationshipIndexNames() ) { RelationshipIndex index = indexManager.forRelationships( indexName ); serializer.putMapping( indexName, new RelationshipIndexRepresentation( indexName, indexManager.getConfiguration( index ) ) ); } } }
@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(); }
@Deprecated @Description( "Get or create a relationship explicit index - YIELD type,name,config" ) @Procedure( name = "db.index.explicit.forRelationships", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION ) public Stream<ExplicitIndexInfo> relationshipManualIndex( @Name( "indexName" ) String explicitIndexName, @Name( value = "config", defaultValue = "" ) Map<String,String> config ) { IndexManager mgr = graphDatabaseAPI.index(); Index<Relationship> index; if ( config == null || config.isEmpty() ) { index = mgr.forRelationships( explicitIndexName ); } else { index = mgr.forRelationships( explicitIndexName, config ); } return Stream.of( new ExplicitIndexInfo( "RELATIONSHIP", explicitIndexName, mgr.getConfiguration( index ) ) ); }
@Deprecated @Description( "Get or create a node explicit index - YIELD type,name,config" ) @Procedure( name = "db.index.explicit.forNodes", mode = WRITE, deprecatedBy = EXPLICIT_INDEX_DEPRECATION ) public Stream<ExplicitIndexInfo> nodeManualIndex( @Name( "indexName" ) String explicitIndexName, @Name( value = "config", defaultValue = "" ) Map<String,String> config ) { IndexManager mgr = graphDatabaseAPI.index(); Index<Node> index; if ( config == null || config.isEmpty() ) { index = mgr.forNodes( explicitIndexName ); } else { index = mgr.forNodes( explicitIndexName, config ); } return Stream.of( new ExplicitIndexInfo( "NODE", explicitIndexName, mgr.getConfiguration( index ) ) ); }
@Override protected void serialize( final MappingSerializer serializer ) { indexManager.nodeIndexNames(); for ( String indexName : indexManager.nodeIndexNames() ) { Index<Node> index = indexManager.forNodes( indexName ); serializer.putMapping( indexName, new NodeIndexRepresentation( indexName, indexManager.getConfiguration( index ) ) ); } } }
@Test public void makeSureIndexNameAndConfigCanBeReachedFromIndex() { String indexName = "my-index-1"; Index<Node> nodeIndex = nodeIndex( indexName, LuceneIndexImplementation.EXACT_CONFIG ); assertEquals( indexName, nodeIndex.getName() ); assertEquals( LuceneIndexImplementation.EXACT_CONFIG, graphDb.index().getConfiguration( nodeIndex ) ); String indexName2 = "my-index-2"; Index<Relationship> relIndex = relationshipIndex( indexName2, LuceneIndexImplementation.FULLTEXT_CONFIG ); assertEquals( indexName2, relIndex.getName() ); assertEquals( LuceneIndexImplementation.FULLTEXT_CONFIG, graphDb.index().getConfiguration( relIndex ) ); }
@Test public void makeSureConfigurationCanBeModified() { Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); try { graphDb.index().setConfiguration( index, IndexManager.PROVIDER, "something" ); fail( "Shouldn't be able to modify provider" ); } catch ( IllegalArgumentException e ) { /* Good*/ } try { graphDb.index().removeConfiguration( index, IndexManager.PROVIDER ); fail( "Shouldn't be able to modify provider" ); } catch ( IllegalArgumentException e ) { /* Good*/ } String key = "my-key"; String value = "my-value"; String newValue = "my-new-value"; assertNull( graphDb.index().setConfiguration( index, key, value ) ); assertEquals( value, graphDb.index().getConfiguration( index ).get( key ) ); assertEquals( value, graphDb.index().setConfiguration( index, key, newValue ) ); assertEquals( newValue, graphDb.index().getConfiguration( index ).get( key ) ); assertEquals( newValue, graphDb.index().removeConfiguration( index, key ) ); assertNull( graphDb.index().getConfiguration( index ).get( key ) ); }
assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "default" ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "wo-provider", MapUtil.stringMap( "type", "exact" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "w-provider", MapUtil.stringMap( "type", "exact", IndexManager.PROVIDER, "lucene" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forRelationships( "default" ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forRelationships( "wo-provider", MapUtil.stringMap( "type", "exact" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index() .forRelationships( "w-provider", MapUtil.stringMap( "type", "exact", IndexManager.PROVIDER, "lucene" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "default" ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "wo-provider", MapUtil.stringMap( "type", "exact" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "w-provider", MapUtil.stringMap( "type", "exact", IndexManager.PROVIDER, "lucene" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forRelationships( "default" ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forRelationships( "wo-provider", MapUtil.stringMap( "type", "exact" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index() .forRelationships( "w-provider", MapUtil.stringMap( "type", "exact", IndexManager.PROVIDER, "lucene" ) ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "default" ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "wo-provider" ) ) ); assertEquals( correctConfig, graphDb.index().getConfiguration( graphDb.index().forNodes( "w-provider" ) ) );
assertEquals( config, db.index().getConfiguration( index ) ); try ( IndexHits<Node> indexHits = index.get( "key", "something else" ) ) assertEquals( config, db.index().getConfiguration( index ) ); tx.success();
private synchronized Map<String, Map<String, Collection<Index<Node>>>> initIndexConfiguration() { Map<String, Map<String, Collection<Index<Node>>>> indexesByLabelAndProperty = new HashMap<>(); try (Transaction tx = graphDatabaseService.beginTx() ) { final IndexManager indexManager = graphDatabaseService.index(); for (String indexName : indexManager.nodeIndexNames()) { final Index<Node> index = indexManager.forNodes(indexName); Map<String, String> indexConfig = indexManager.getConfiguration(index); if (Util.toBoolean(indexConfig.get("autoUpdate"))) { String labels = indexConfig.getOrDefault("labels", ""); for (String label : labels.split(":")) { Map<String, Collection<Index<Node>>> propertyKeyToIndexMap = indexesByLabelAndProperty.computeIfAbsent(label, s -> new HashMap<>()); String[] keysForLabel = indexConfig.getOrDefault("keysForLabel:" + label, "").split(":"); for (String property : keysForLabel) { propertyKeyToIndexMap.computeIfAbsent(property, s -> new ArrayList<>()).add(index); } } } } tx.success(); } return indexesByLabelAndProperty; }
@Description("apoc.index.list() - YIELD type,name,config - lists all manual indexes") @Procedure(mode = Mode.READ) public Stream<IndexInfo> list() { IndexManager mgr = db.index(); List<IndexInfo> indexInfos = new ArrayList<>(100); for (String name : mgr.nodeIndexNames()) { Index<Node> index = mgr.forNodes(name); indexInfos.add(new IndexInfo(NODE,name,mgr.getConfiguration(index))); } for (String name : mgr.relationshipIndexNames()) { RelationshipIndex index = mgr.forRelationships(name); indexInfos.add(new IndexInfo(RELATIONSHIP,name,mgr.getConfiguration(index))); } return indexInfos.stream(); }
@Description("apoc.index.remove('name') YIELD type,name,config - removes an manual index") @Procedure(mode = Mode.WRITE) public Stream<IndexInfo> remove(@Name("name") String name) { IndexManager mgr = db.index(); List<IndexInfo> indexInfos = new ArrayList<>(2); if (mgr.existsForNodes(name)) { Index<Node> index = mgr.forNodes(name); indexInfos.add(new IndexInfo(NODE, name, mgr.getConfiguration(index))); index.delete(); } if (mgr.existsForRelationships(name)) { RelationshipIndex index = mgr.forRelationships(name); indexInfos.add(new IndexInfo(RELATIONSHIP, name, mgr.getConfiguration(index))); index.delete(); } return indexInfos.stream(); }
@Description("apoc.index.forRelationships('name',{config}) YIELD type,name,config - gets or creates relationship index") @Procedure(mode = Mode.WRITE) public Stream<IndexInfo> forRelationships(@Name("name") String name, @Name(value="config",defaultValue="") Map<String,String> config) { RelationshipIndex index = getRelationshipIndex(name, config); return Stream.of(new IndexInfo(RELATIONSHIP, name, db.index().getConfiguration(index))); }
@Description("apoc.index.forNodes('name',{config}) YIELD type,name,config - gets or creates node index") @Procedure(mode = Mode.WRITE) public Stream<IndexInfo> forNodes(@Name("name") String name, @Name(value="config",defaultValue="") Map<String,String> config) { Index<Node> index = getNodeIndex(name, config); return Stream.of(new IndexInfo(NODE, name, db.index().getConfiguration(index))); }
private Index<Node> index(String index, Map<String, List<String>> structure, final Map<String,Object> options ) { Map<String, String> config = new HashMap<>(CONFIG); try (Transaction tx = db.beginTx()) { if (db.index().existsForNodes(index)) { Index<Node> old = db.index().forNodes(index); Map<String,String> oldConfig = new HashMap<>(db.index().getConfiguration(old)); log.info("Dropping existing index '%s', with config: %s", index, oldConfig); old.delete(); } tx.success(); } try (Transaction tx = db.beginTx()) { updateConfigFromParameters(config, structure); /* add options to the parameters */ options.forEach((k,v) -> { config.put(k, String.valueOf(v)); // explicit conversion to String } ); log.info("Creating or updating index '%s' with config '%s'", index, config ); Index<Node> nodeIndex = db.index().forNodes(index, config); resetIndexUpdateConfiguration(); tx.success(); return nodeIndex; } }
@Override public Map<String, String> getConfiguration( Index<? extends PropertyContainer> index ) { return actual.index().getConfiguration( index ); }
@Override protected void serialize( MappingSerializer serializer ) { for ( String indexName : indexManager.relationshipIndexNames() ) { RelationshipIndex index = indexManager.forRelationships( indexName ); serializer.putMapping( indexName, new RelationshipIndexRepresentation( indexName, indexManager.getConfiguration( index ) ) ); } } }
@Override protected void serialize( final MappingSerializer serializer ) { indexManager.nodeIndexNames(); for ( String indexName : indexManager.nodeIndexNames() ) { Index<Node> index = indexManager.forNodes( indexName ); serializer.putMapping( indexName, new NodeIndexRepresentation( indexName, indexManager.getConfiguration( index ) ) ); } } }