[Neo4j] How to create an index

Andreas Ronge andreas.ronge at jayway.se
Tue Oct 26 18:41:05 CEST 2010


On Tue, Oct 26, 2010 at 3:55 PM, Mattias Persson
<mattias at neotechnology.com> wrote:
> Hi,
>
> I'm a bit torn about one aspect of the new index
> framework<http://wiki.neo4j.org/content/Index_Framework>:
> index creation.
>
> My initial though with it was do creation just like you do with an embedded
> graph database, i.e. there's no explicit "creation" phase for it, instead
> you just instantiate a:
>
>    new EmbeddedGraphDatabase( "my/dir" );
>  or
>    new EmbeddedGraphDatabase( "my/dir", myConfigMap );
>
> and it will be created if it doesn't exist... even taking some parameters
> from the map and storing permanently the first time so they cannot be
> changed as long as the database is there (f.ex. string block size and more).
> It's about usability IMO to not have to do:
>
>   if ( dbDoesntExist( "my/dir" ) )
>   {
>       createDb( "my/dir" );
>   }
>   else
>   {
>       openDb( "my/dir" );
>   }
>
> or similar... Now looking at index creation, it's done in a similar fashion:
>
>   // will create an index "persons" w/ default configuration if it doesn't
> exist
>   // else it will just return it w/ the config used when creating it.
>   graphDb.index().forNodes( "persons" );
>
>   // will create an index "persons" w supplied configuration if it doesn't
> exist.
>   // if it does exist then configuration must match the stored config.
>   // if it already existed it will be returned w/ the config used when
> created.
>   graphDb.index().forNodes( "persons", myConfigMap );

Just some quick thoughts:

I prefer something like that.
In neo4j.rb I have a (private) class called IndexRegistry where you
can register an index
I prefer APIs with many of simple methods.
registry = graphDb.nodeIndexRegistry()
registry.createOrOpen("person")  // hmm, create with default ? configuration
registry.createOrOpen("person", config)  // throws exception if it
already exist but with a different configuration.
registry.create("person") // throws exception if it already exist
registry.open("person")
registry.exist("person")


Cheers

Andreas




>
>
> Is it a bad thing or surprising that index creation happens as a side effect
> of requesting? Of course it could be. Should there instead be:
>
>   graphDb.index().forNodes( "persons" );
>   graphDb.index().createForNodes( "persons", myConfigMap );
>
> so that creation is explicit? Then your code would potentially have to do a:
>
>  Index<Node> personIndex = null;
>  if ( indexExists( "persons" ) )
>     personIndex = graphDb.index().forNodes( "persons" );
>  else
>     personIndex = graphDb.index().createForNodes( "persons", myConfigMap );
>  doStuffWith( personIndex );
>
> If you don't have a place where such initialization occurs for each startup,
> making sure all your needed indexes are created if they do not exist.
>
>
> Input on this, anyone?
>
> --
> Mattias Persson, [mattias at neotechnology.com]
> Hacker, Neo Technology
> www.neotechnology.com
> _______________________________________________
> Neo4j mailing list
> User at lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>


More information about the User mailing list