我从这里的文档和帖子中了解到,在为 neo4j 中的节点属性启用自动索引后,必须为每个节点再次设置属性才能将属性添加到索引中。
Neo4j 版本 1.9.M05
使用 DrWho 数据库,这个 groovy 代码旨在通过设置属性将 Doctor 字符添加到自动索引字符属性中。此代码不起作用。运行后自动节点索引为空
你能看到我做错了什么吗?
import org.neo4j.graphdb.*
import org.neo4j.graphdb.factory.*
import org.neo4j.graphdb.index.*
db_path = '/Users/mike/Documents/code/neo4j/dbs/drwho.db'
// use Builder to initialize settings for embedded db
// include autoindexing
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder( db_path ).
setConfig(GraphDatabaseSettings.node_auto_indexing, "true" ).
setConfig(GraphDatabaseSettings.node_keys_indexable, "character" ).
newGraphDatabase()
DoctorKey="character"
DoctorValue="Doctor"
Node Doctor = graphDb.getNodeById( 1 )
assert Doctor.hasProperty( DoctorKey )
assert Doctor.getProperty( DoctorKey ).equals( DoctorValue )
// drop and add character property to add it to auto_index
Transaction tx = graphDb.beginTx()
try
{
Doctor.removeProperty( DoctorKey )
Doctor.setProperty( DoctoryKey, DoctorValue )
tx.success()
} catch ( Exception e ) { tx.failure()
} finally { tx.finish() }
assert Doctor.hasProperty( DoctorKey )
assert Doctor.getProperty( DoctorKey ).equals( DoctorValue )
// query index
ReadableIndex<Node> autoNodeIndex = graphDb.index().
getNodeAutoIndexer().
getAutoIndex()
// DoctorAgain is NULL
Node DoctorAgain = autoNodeIndex.get( DoctorKey , DoctorValue ).getSingle()
assert DoctorAgain == Doctor
addShutdownHook {
graphDb.shutdown()
}