我从这里的文档和帖子了解到,在为neo4j中的节点属性启用自动索引后,必须为每个节点再次设置属性以将属性添加到索引。neo4j重建自动索引
Neo4j的版本1.9.M05
使用DrWho数据库这个Groovy代码的目的是通过设置属性,添加医生字符自动索引字符属性。此代码不起作用。在运行后自动节点索引为空
你能看到我在做什么错吗?
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()
}
并且您应该记录异常,以防在更新密钥时失败,您不会看到它。 –