Class: Neo4j::Core::Index::Indexer
- Inherits:
-
Object
- Object
- Neo4j::Core::Index::Indexer
- Defined in:
- lib/neo4j-core/index/indexer.rb
Overview
This class is delegated from the Neo4j::Core::Index::ClassMethod
Instance Attribute Summary (collapse)
Instance Method Summary (collapse)
-
- (Object) add_index(entity, field, value)
Adds an index on the given entity This is normally not needed since you can instead declare an index which will automatically keep the lucene index in sync.
-
- (Java::OrgNeo4jGraphdb::Index) create_index_with(type, index_name)
Creates a new lucene index using the lucene configuration for the given index_name.
-
- (Neo4j::Core::Index::LuceneQuery) find(query, params = {})
Performs a Lucene Query.
-
- (true, false) has_index_type?(type)
If there is an index of the given type defined.
-
- (Object) index(*args)
Add an index on a field so that it will be automatically updated by neo4j transactional events.
-
- (true, false) index?(field)
If there is an index on the given field.
-
- (Java::OrgNeo4jGraphdb::Index) index_for_field(field)
For the given field.
-
- (Java::OrgNeo4jGraphdb::Index) index_for_type(type)
For the given index type.
-
- (String) index_name_for_type(type)
The name of the index which are stored on the filesystem.
-
- (Symbol) index_type(field)
The type of index for the given field (e.g. :exact or :fulltext).
-
- (Java::OrgNeo4jIndexLucene::ValueContext) indexed_value_for(field, value)
Creates a wrapped ValueContext for the given value.
-
- (Indexer) initialize(config)
constructor
A new instance of Indexer.
- - (Boolean) java_array?(value)
-
- (Hash) lucene_config(type)
The lucene config for the given index type.
-
- (Object) on_neo4j_shutdown
Called when the neo4j shutdown in order to release references to indexes.
-
- (nil, ...) put_if_absent(entity, key, value, index_type = :exact)
Add the entity to this index for the given key/value pair if this particular key/value pair doesn't already exist.
-
- (Object) remove_index_on(node, old_props)
Called from the event handler when deleting a property.
-
- (Object) rm_index(entity, field, value)
Removes an index on the given entity This is normally not needed since you can instead declare an index which will automatically keep the lucene index in sync.
-
- (Object) rm_index_config
Delete all index configuration.
-
- (Object) rm_index_type(type = nil)
delete the index, if no type is provided clear all types of indexes.
- - (Object) to_s
-
- (true, false) trigger_on?(props)
If the.
-
- (Object) update_index_on(node, field, old_val, new_val)
Called from the event handler when a new node or relationships is about to be committed.
Constructor Details
- (Indexer) initialize(config)
A new instance of Indexer
11 12 13 14 15 16 |
# File 'lib/neo4j-core/index/indexer.rb', line 11 def initialize(config) @config = config @indexes = {} # key = type, value = java neo4j index # to enable subclass indexing to work properly, store a list of parent indexers and # whenever an operation is performed on this one, perform it on all end |
Instance Attribute Details
- (Neo4j::Core::Index::IndexConfig) config (readonly)
9 10 11 |
# File 'lib/neo4j-core/index/indexer.rb', line 9 def config @config end |
Instance Method Details
- (Object) add_index(entity, field, value)
Adds an index on the given entity This is normally not needed since you can instead declare an index which will automatically keep the lucene index in sync.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/neo4j-core/index/indexer.rb', line 63 def add_index(entity, field, value) return false unless index?(field) if (java_array?(value)) conv_value = value.map{|x| indexed_value_for(field, x)}.to_java(Java::OrgNeo4jIndexLucene::ValueContext) else conv_value = indexed_value_for(field, value) end index = index_for_field(field.to_s) index.add(entity, field, conv_value) end |
- (Java::OrgNeo4jGraphdb::Index) create_index_with(type, index_name)
Creates a new lucene index using the lucene configuration for the given index_name
241 242 243 244 245 246 247 248 249 |
# File 'lib/neo4j-core/index/indexer.rb', line 241 def create_index_with(type, index_name) db = Neo4j.started_db index_config = lucene_config(type) if config.entity_type == :node db.lucene.for_nodes(index_name, index_config) else db.lucene.for_relationships(index_name, index_config) end end |
- (Neo4j::Core::Index::LuceneQuery) find(query, params = {})
You must specify the index type :fulltext<tt>) if the property is index using that index (default is <tt>:exact)
Performs a Lucene Query.
In order to use this you have to declare an index on the fields first, see #index. Notice that you should close the lucene query after the query has been executed. You can do that either by provide an block or calling the Neo4j::Core::Index::LuceneQuery#close method. When performing queries from Ruby on Rails you do not need this since it will be automatically closed (by Rack).
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/neo4j-core/index/indexer.rb', line 132 def find(query, params = {}) index = index_for_type(params[:type] || :exact) query.delete(:sort) if query.is_a?(Hash) && query.include?(:sort) query = (params[:wrapped].nil? || params[:wrapped]) ? LuceneQuery.new(index, @config, query, params) : index.query(query) if block_given? begin ret = yield query ensure query.close end ret else query end end |
- (true, false) has_index_type?(type)
If there is an index of the given type defined.
55 56 57 |
# File 'lib/neo4j-core/index/indexer.rb', line 55 def has_index_type?(type) @config.has_index_type?(type) end |
- (Object) index(*args)
Add an index on a field so that it will be automatically updated by neo4j transactional events. Notice that if you want to numerical range queries then you should specify a field_type of either Fixnum or Float. The index type will by default be :exact. Index on property arrays are supported.
35 36 37 |
# File 'lib/neo4j-core/index/indexer.rb', line 35 def index(*args) @config.index(args) end |
- (true, false) index?(field)
If there is an index on the given field.
40 41 42 |
# File 'lib/neo4j-core/index/indexer.rb', line 40 def index?(field) @config.index?(field) end |
- (Java::OrgNeo4jGraphdb::Index) index_for_field(field)
For the given field
212 213 214 215 216 |
# File 'lib/neo4j-core/index/indexer.rb', line 212 def index_for_field(field) type = @config.index_type(field) index_name = index_name_for_type(type) @indexes[index_name] ||= create_index_with(type, index_name) end |
- (Java::OrgNeo4jGraphdb::Index) index_for_type(type)
For the given index type
219 220 221 222 |
# File 'lib/neo4j-core/index/indexer.rb', line 219 def index_for_type(type) index_name = index_name_for_type(type) @indexes[index_name] ||= create_index_with(type, index_name) end |
- (String) index_name_for_type(type)
The name of the index which are stored on the filesystem
225 226 227 |
# File 'lib/neo4j-core/index/indexer.rb', line 225 def index_name_for_type(type) @config.index_name_for_type(type) end |
- (Symbol) index_type(field)
The type of index for the given field (e.g. :exact or :fulltext)
50 51 52 |
# File 'lib/neo4j-core/index/indexer.rb', line 50 def index_type(field) @config.index_type(field) end |
- (Java::OrgNeo4jIndexLucene::ValueContext) indexed_value_for(field, value)
Creates a wrapped ValueContext for the given value. Checks if it's numeric value in the configuration.
203 204 205 206 207 208 209 |
# File 'lib/neo4j-core/index/indexer.rb', line 203 def indexed_value_for(field, value) if @config.numeric?(field) Java::OrgNeo4jIndexLucene::ValueContext.new(value).indexNumeric else Java::OrgNeo4jIndexLucene::ValueContext.new(value) end end |
- (Boolean) java_array?(value)
74 75 76 |
# File 'lib/neo4j-core/index/indexer.rb', line 74 def java_array?(value) value.respond_to?(:java_class) && value.java_class.to_s[0..0] == '[' end |
- (Hash) lucene_config(type)
The lucene config for the given index type
230 231 232 233 234 |
# File 'lib/neo4j-core/index/indexer.rb', line 230 def lucene_config(type) conf = Neo4j::Config[:lucene][type.to_s] raise "unknown lucene type #{type}" unless conf conf end |
- (Object) on_neo4j_shutdown
Called when the neo4j shutdown in order to release references to indexes
184 185 186 |
# File 'lib/neo4j-core/index/indexer.rb', line 184 def on_neo4j_shutdown @indexes.clear end |
- (nil, ...) put_if_absent(entity, key, value, index_type = :exact)
Add the entity to this index for the given key/value pair if this particular key/value pair doesn't already exist. This ensures that only one entity will be associated with the key/value pair even if multiple transactions are trying to add it at the same time. One of those transactions will win and add it while the others will block, waiting for the winning transaction to finish. If the winning transaction was successful these other transactions will return the associated entity instead of adding it. If it wasn't successful the waiting transactions will begin a new race to add it.
161 162 163 164 |
# File 'lib/neo4j-core/index/indexer.rb', line 161 def put_if_absent(entity, key, value, index_type = :exact) index = index_for_type(index_type) index.put_if_absent(entity, key.to_s, value) end |
- (Object) remove_index_on(node, old_props)
Called from the event handler when deleting a property
197 198 199 |
# File 'lib/neo4j-core/index/indexer.rb', line 197 def remove_index_on(node, old_props) @config.fields.each { |field| rm_index(node, field, old_props[field]) if old_props[field] } end |
- (Object) rm_index(entity, field, value)
Removes an index on the given entity This is normally not needed since you can instead declare an index which will automatically keep the lucene index in sync.
82 83 84 85 86 |
# File 'lib/neo4j-core/index/indexer.rb', line 82 def rm_index(entity, field, value) return false unless index?(field) #return value.each {|x| rm_index(entity, field, x)} if value.respond_to?(:each) index_for_field(field).remove(entity, field, value) end |
- (Object) rm_index_config
Delete all index configuration. No more automatic indexing will be performed
167 168 169 |
# File 'lib/neo4j-core/index/indexer.rb', line 167 def rm_index_config @config.rm_index_config end |
- (Object) rm_index_type(type = nil)
delete the index, if no type is provided clear all types of indexes
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/neo4j-core/index/indexer.rb', line 172 def rm_index_type(type=nil) if type key = @config.index_name_for_type(type) @indexes[key] && @indexes[key].delete @indexes[key] = nil else @indexes.each_value { |index| index.delete } @indexes.clear end end |
- (Object) to_s
19 20 21 |
# File 'lib/neo4j-core/index/indexer.rb', line 19 def to_s "Indexer @#{object_id} index on: [#{@config.fields.map { |f| @config.numeric?(f) ? "#{f} (numeric)" : f }.join(', ')}]" end |
- (true, false) trigger_on?(props)
If the
45 46 47 |
# File 'lib/neo4j-core/index/indexer.rb', line 45 def trigger_on?(props) @config.trigger_on?(props) end |
- (Object) update_index_on(node, field, old_val, new_val)
Called from the event handler when a new node or relationships is about to be committed.
189 190 191 192 193 194 |
# File 'lib/neo4j-core/index/indexer.rb', line 189 def update_index_on(node, field, old_val, new_val) if index?(field) rm_index(node, field, old_val) if old_val add_index(node, field, new_val) if new_val end end |