Skip to content

Neo4j::Wrapper Lucene

Brian Underwood edited this page Jan 13, 2016 · 4 revisions

THIS PAGE IS OUT OF DATE. IT IS ONLY FOR VERSIONS OF THE NEO4J GEM BEFORE VERSION 3.0

Using lucene from NodeMixin or Rails::Model

In the neo4j-core gem you declare an index using the Neo4j::Node.index method, see Neo4j::Core-Lucene
When using Neo4j::NodeMixin, Neo4j::RelationshipMixin, Neo4j::Rails::Model and Neo4j::Rails::Relationships you declare index at the same time as you declare properties.

The neo4j-wrapper and neo4j gem lucene integration also automatically configure the index so that each class (and its subclasses) will share the same index configuration and lucene files.

Notice You declare index in the same way using the Neo4j::Rails::Model as in using the Neo4j::NodeMixin. However, the finder methods for the Neo4j::Rails::Model is different since it is more similar to how ActiveRecord finders work (it will generate finder methods).

Define an Index

In order to perform a query you must declare an index on the property.
A Lucene index can be declared on nodes and relationships.

Example:

class Person
  include Neo4j::NodeMixin
  property :name, :index => :exact
end

After each transaction finishes the Person will be indexed.

Neo4j::Transaction.run do
  Person.new :name => 'andreas'
end
Person.find('name: andreas').first #=> andreas

See Neo4j::Core-Lucene how the find method works.

Notice that only properties that have been set are added to the index. Thus a property with no set value and no default value will NOT be matched by a wildcard query (*)!

Neo4j::RelationshipMixin

Works as indexing on Neo4j::NodeMixin

Fulltext and Exact

By default indexes are of type :exact which is great for indexing keywords etc.
To index each word in a text you should use a fulltext index. Fulltext uses white-space tokenizer in its analyzer. Add the type :fulltext (:exact is default) when you declare the index and in the find method.

Example:

class Person
  include Neo4j::NodeMixin
  property :name, :index => :fulltext
end
Person.find('name: andreas', :type => :fulltext).first #=> andreas

Numerical Fields

In order to support sort order and range queries on Fixnum, Float and Date queries you need
to specify that the field will be indexed as a numerical field.

Example:

class Person
  include Neo4j::NodeMixin
  property :age, :type => Fixnum, :index => :exact

Date and DateTime Range Queries

Since the neo4j-wrapper and neo4j gem has support for type converters (see YARD docs for Neo4j::TypeConverter) you can also query on any type supported by the type converters, like Date and DateTime.

If you specify a :type of Date or DateTime it will be translated into an Fixnum.

class Person
  include Neo4j::NodeMixin
  property :born, :type => Date, :index => :exact
end

Person.find(:born => Date.today - 5 .. Date.today)

Date Queries

class Person
  property :born, :type => DateTime, :index => :exact
end
Person.find(:born).between(Date.today, 5.years.ago)

Both Date and DateTime are supported.
The DateTime values will automatically be converted into UTC timezone.

Type Conversion

Neo4j has support for converting DateTime and Date to Java long values.
You can create your own type conversion, read:
See TypeConverters method.

The type converters are also used in hash queries where the query parameters can be converted.

Clone this wiki locally