0
votes

I understand from the documentation and posts here that after enabling auto indexing for a node property in neo4j the properties must be set again for each node to add the properties to the index.

Neo4j version 1.9.M05

using the DrWho database this groovy code is intended to add the Doctor character to the auto indexed character property by setting the property. This code does not work. The auto node index is empty after this runs

Can you see what I am doing wrong?

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
and you should probably log exceptions, in case of a failure when updating the key, you wouldn't see it.Michael Hunger

1 Answers

4
votes

This is not a Neo4j related issue, you just have a typo in your code. If you replace

Doctor.setProperty( DoctoryKey, DoctorValue )

with

Doctor.setProperty( DoctorKey, DoctorValue )

it works.