【问题标题】:neo4j rebuild auto indexneo4j 重建自动索引
【发布时间】:2013-03-29 14:37:52
【问题描述】:

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

【问题讨论】:

  • 并且您可能应该记录异常,以防更新密钥失败,您将看不到它。

标签: neo4j


【解决方案1】:

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

Doctor.setProperty( DoctoryKey, DoctorValue )

Doctor.setProperty( DoctorKey, DoctorValue )

它有效。

【讨论】:

  • @MichaelWest,你能点击接受的答案吗?
猜你喜欢
  • 2012-10-04
  • 2019-04-25
  • 1970-01-01
  • 2015-10-13
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
相关资源
最近更新 更多