Module: Mongoid::Mebla::ClassMethods
- Defined in:
- lib/mebla/mongoid/mebla.rb
Overview
Defines class methods for Mongoid::Mebla
Instance Method Summary collapse
-
#search(query = "") ⇒ Mebla::Search
Searches the model using Slingshot search DSL Search for all posts with a field ‘title’ of value ‘Testing Search’::.
-
#search_in(*opts) ⇒ nil
Defines which fields should be indexed and searched Defines a search index on a normal document with custom mappings on “body”::.
-
#slingshot_type_name ⇒ String
Retrieves the type name of the model (used to populate the _type variable while indexing).
-
#sub_class? ⇒ Boolean
Checks if the class is a subclass.
-
#without_indexing(&block) ⇒ nil
- Enables the modification of records without indexing 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.
Instance Method Details
#search(query = "") ⇒ Mebla::Search
Searches the model using Slingshot search DSL
- Search for all posts with a field ‘title’ of value ‘Testing Search’
-
Post.search “title: Testing Search”
134 135 136 |
# File 'lib/mebla/mongoid/mebla.rb', line 134 def search(query = "") ::Mebla.search(query, self.slingshot_type_name) end |
#search_in(*opts) ⇒ nil
Defines which fields should be indexed and searched
- 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 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 #... :category search_in :title, :publish_date, :body => { :boost => 2.0, :analyzer => 'snowball' }, :embedded_in => :categoryend
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mebla/mongoid/mebla.rb', line 73 def search_in(*opts) # Extract advanced indeces = opts..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. if ( = .delete(:embedded_in)) relation = self.relations[.to_s] # Infer the attributes of the relation self. = relation.class_name.constantize self. = relation.key.to_s self. = relation[:inverse_of] || relation.inverse_setter.to_s.gsub(/=$/, '') if self..blank? raise ::Mebla::Errors::MeblaConfigurationException.new("Couldn't infer #{.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 # Keep track of searchable fields (for indexing) self.search_fields = attrs + .keys # Generate simple indeces' mappings attrs_mappings = {} attrs.each do |attribute| unless (field_type = self.fields[attribute.to_s].type.to_s) == "Array" # arrays don't need mappings attrs_mappings[attribute] = {:type => SLINGSHOT_TYPE_MAPPING[field_type] || "string"} end end # Generate advanced indeces' mappings opts_mappings = {} .each do |opt, properties| opts_mappings[opt] = {:type => SLINGSHOT_TYPE_MAPPING[self.fields[opt.to_s].type.to_s] || "string" }.merge!(properties) 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 |
#slingshot_type_name ⇒ String
Retrieves the type name of the model (used to populate the _type variable while indexing)
141 142 143 |
# File 'lib/mebla/mongoid/mebla.rb', line 141 def slingshot_type_name "#{self.name.underscore}" end |
#sub_class? ⇒ Boolean
Checks if the class is a subclass
163 164 165 |
# File 'lib/mebla/mongoid/mebla.rb', line 163 def sub_class? self.superclass != Object end |
#without_indexing(&block) ⇒ nil
you can skip indexing to create, update or delete records without affecting the index
Enables the modification of records without indexing
- 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
153 154 155 156 157 158 159 |
# File 'lib/mebla/mongoid/mebla.rb', line 153 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 |