2013-03-29 168 views
0

我从这里的文档和帖子了解到,在为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() 
} 
+0

并且您应该记录异常,以防在更新密钥时失败,您不会看到它。 –

回答

4

这不是一个Neo4j相关的问题,你只是在你的代码中有一个错字。如果更换

Doctor.setProperty(DoctoryKey, DoctorValue) 

Doctor.setProperty(DoctorKey, DoctorValue) 

它的工作原理。

+0

是的,它的工作原理。谢谢。 –

+0

@MichaelWest,你可以点击接受的答案吗? –