Skip to content
Andreas Ronge edited this page Apr 26, 2012 · 5 revisions

Neo4j comes included with the lucene document database.
A common use case for using lucene is searching for one node and from that node traverse or use a cypher query.

Define an Index

The lucene integration uses the beforeCommit hook, see http://docs.neo4j.org/chunked/1.7/transactions-events.html. The method Neo4j::Node.trigger_on is used to tell neo4j.rb which nodes or relationship it will index.
The Neo4j::Node.index method is used to tell which properties are going to be index and which type of index should be used.

Example:

Neo4j::Node.trigger_on(:typex => 'MyTypeX')
Neo4j::Node.index :name
Neo4j::Node.index :description, :type => :fulltext
Neo4j::Node.index :age, :field_type => Fixnum

:trigger_on
The declaration above will tell neo4j.rb that only nodes which have the property typex value MyTypeX will be indexed. When the before commit hook finds a node with that property it will index each property declared with the Neo4j::Node.index method.

:type

The default type of index being used is exact. The example above declares index on property name and description with different types of indexes (exact and fulltext).

:field_type

All values are indexed as Strings in lucene by default.
If you want to index a value as a numeric value you can specify that with setting :field_type to Fixnum or Float.
By doing that allows you to use lucene range searches.

Notice, if you are using the Neo4j::NodeMixin or Neo4j::Rails::Model@ you define an index using the property method instead.

Custom Index

Instead of using the Neo4j::Node.index or Neo4j::Relationship.index methods you can define your own class for indexing. This allows you to have different lucene index files and configurations.

Example:

class MyIndex
  extend Neo4j::Core::Index::ClassMethods
  include Neo4j::Core::Index

  self.node_indexer do
    index_names :exact => 'myindex_exact', :fulltext => 'myindex_fulltext'
    trigger_on :myindex => true # trigger on all nodes having property myindex == true
  end

  index :name
end

This class is also used when searching, e.g. MyIndex.find(:name => 'andreas').first

For index on relationship use the rel_indexer method instead of the node_indexer method.

Search

Clone this wiki locally