neo4j重建汽车指数

时间:2013-03-29 14:37:53

标签: neo4j

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

1 个答案:

答案 0 :(得分:4)

这不是与Neo4j相关的问题,您的代码中只有拼写错误。如果你替换

Doctor.setProperty( DoctoryKey, DoctorValue )

Doctor.setProperty( DoctorKey, DoctorValue )

它有效。