module Mongoid::Mebla::ClassMethods
Defines class methods for Mongoid::Mebla
Public Instance Methods
Searches the model using Slingshot search DSL
@param [String] query a string representing the search query
@return [Mebla::Search]
- Search for all posts with a field ‘title’ of value ‘Testing Search’
-
Post.search “title: Testing Search”
# File lib/mebla/mongoid/mebla.rb, line 183 def search(query = "") ::Mebla.search(query, self.slingshot_type_name) end
Defines which fields should be indexed and searched @param [*opts] fields @return [nil]
- Defines a search index on a normal document with custom mappings on “body”
-
class Document
include Mongoid::Document include Mongoid::Mebla field :title field :body field :publish_date, :type => Date #... search_in :title, :publish_date, :body => { :boost => 2.0, :analyzer => 'snowball' }
end
- Defines a search index on a normal document with an index on a field inside a relation
-
class Document
include Mongoid::Document include Mongoid::Mebla field :title field :body field :publish_date, :type => Date referenced_in :blog #... # relations mappings are detected automatically search_in :title, :publish_date, :body => { :boost => 2.0, :analyzer => 'snowball' }, :search_relations => { :blog => [:author, :name] }
end
- Defines a search index on a normal document with an index on method “permalink”
-
class Document
include Mongoid::Document include Mongoid::Mebla field :title field :body field :publish_date, :type => Date def permalink self.title.gsub(/\s/, "-").downcase end #... # methods can also define custom mappings if needed search_in :title, :publish_date, :permalink, :body => { :boost => 2.0, :analyzer => 'snowball' }
end
- Defines a search index on an embedded document with a single parent and custom mappings on “body”
-
class Document
include Mongoid::Document include Mongoid::Mebla field :title field :body field :publish_date, :type => Date #... embedded_in :category search_in :title, :publish_date, :body => { :boost => 2.0, :analyzer => 'snowball' }, :embedded_in => :category
end
# File lib/mebla/mongoid/mebla.rb, line 104 def search_in(*opts) # Extract advanced indeces options = opts.extract_options!.symbolize_keys # Extract simple indeces attrs = opts.flatten # If this document is embedded check for the embedded_in option and raise an error if none is specified # Example:: # embedded in a regular class (e.g.: using the default convention for naming the foreign key) # :embedded_in => :parent if self.embedded? if (embedor = options.delete(:embedded_in)) relation = self.relations[embedor.to_s] # Infer the attributes of the relation self.embedded_parent = relation.class_name.constantize self.embedded_parent_foreign_key = relation.key.to_s self.embedded_as = relation[:inverse_of] || relation.inverse_setter.to_s.gsub(/=$/, '') if self.embedded_as.blank? raise ::Mebla::Errors::MeblaConfigurationException.new("Couldn't infer #{embedor.to_s} inverse relation, please set :inverse_of option on the relation.") end else raise ::Mebla::Errors::MeblaConfigurationException.new("#{self.name} is embedded: embedded_in option should be set to the parent class if the document is embedded.") end end self.search_relations = {} # Keep track of relational indecies unless (relations_inedcies = options.delete(:search_relations)).nil? relations_inedcies.each do |relation, index| self.search_relations[relation] = index end end # Keep track of searchable fields (for indexing) self.search_fields = attrs + options.keys # Generate simple indeces' mappings attrs_mappings = {} attrs.each do |attribute| unless (attr_field = self.fields[attribute.to_s]).nil? unless (field_type = attr_field.type.to_s) == "Array" # arrays don't need mappings attrs_mappings[attribute] = {:type => SLINGSHOT_TYPE_MAPPING[field_type] || "string"} end else attrs_mappings[attribute] = {:type => "string"} end end # Generate advanced indeces' mappings opts_mappings = {} options.each do |opt, properties| unless (attr_field = self.fields[opt.to_s]).nil? unless (field_type = attr_field.type.to_s) == "Array" opts_mappings[opt] = {:type => SLINGSHOT_TYPE_MAPPING[field_type] || "string" }.merge!(properties) end else opts_mappings[opt] = {:type => "string"}.merge!(properties) end end # Merge mappings self.slingshot_mappings = {}.merge!(attrs_mappings).merge!(opts_mappings) # Keep track of indexed models (for bulk indexing) ::Mebla.context.add_indexed_model(self, self.slingshot_type_name.to_sym => prepare_mappings) end
Retrieves the type name of the model (used to populate the _type variable while indexing) @return [String]
# File lib/mebla/mongoid/mebla.rb, line 190 def slingshot_type_name "#{self.name.underscore}" end
Checks if the class is a subclass @return [Boolean] true if class is a subclass
# File lib/mebla/mongoid/mebla.rb, line 212 def sub_class? self.superclass != Object end
Enables the modification of records without indexing @return [nil]
- Example
-
create record without it being indexed
Class.without_indexing do create :title => "This is not indexed", :body => "Nothing will be indexed within this block" end
@note you can skip indexing to create, update or delete records without affecting the index
# File lib/mebla/mongoid/mebla.rb, line 202 def without_indexing(&block) skip_callback(:save, :after, :add_to_index) skip_callback(:destroy, :before, :remove_from_index) yield set_callback(:save, :after, :add_to_index) set_callback(:destroy, :before, :remove_from_index) end
Private Instance Methods
Prepare the mappings required for this document @return [Hash]
# File lib/mebla/mongoid/mebla.rb, line 219 def prepare_mappings if self.embedded? mappings = { :_parent => { :type => self.embedded_parent.name.underscore }, :_routing => { :required => true, :path => self.embedded_parent_foreign_key + "_id" } } else mappings = {} end mappings.merge!({ :properties => self.slingshot_mappings }) end