README.md in mongoid_search-0.3.2 vs README.md in mongoid_search-0.3.3
- old
+ new
@@ -24,15 +24,16 @@
class Product
include Mongoid::Document
include Mongoid::Search
field :brand
field :name
+ field :info, :type => Hash
has_many :tags
belongs_to :category
- search_in :brand, :name, :tags => :name, :category => :name
+ search_in :brand, :name, :tags => :name, :category => :name, :info => [:summary, :description]
end
class Tag
include Mongoid::Document
field :name
@@ -47,18 +48,18 @@
has_many :products
end
Now when you save a product, you get a _keywords field automatically:
- p = Product.new :brand => "Apple", :name => "iPhone"
+ p = Product.new :brand => "Apple", :name => "iPhone", :info => {:summary => "Info-summary", :description => "Info-description"}
p.tags << Tag.new(:name => "Amazing")
p.tags << Tag.new(:name => "Awesome")
p.tags << Tag.new(:name => "Superb")
p.save
=> true
p._keywords
- => ["amazing", "apple", "awesome", "iphone", "superb"]
+ => ["amazing", "apple", "awesome", "iphone", "superb", "Info-summary", "Info-description"]
Now you can run search, which will look in the _keywords field and return all matching results:
Product.full_text_search("apple iphone").size
=> 1
@@ -71,11 +72,14 @@
Assuming you have a category with multiple products you can use the following
code to search for 'iphone' in products cheaper than $499
@category.products.where(:price.lt => 499).full_text_search('iphone').asc(:price)
+To index or reindex all existing records, run this rake task
+ $ rake mongoid_search:index
+
Options
-------
match:
@@ -111,11 +115,11 @@
Default is _false_.
Product.full_text_search('amazing apple', relevant_search: true)
=> [#<Product _id: 5016e7d16af54efe1c000001, _type: nil, brand: "Apple", name: "iPhone", attrs: nil, info: nil, category_id: nil, _keywords: ["amazing", "apple", "awesome", "iphone", "superb"], relevance: 2.0>]
- Please note that relevant_search will return an Array and not a Criteria object. The search method shoud always be called in the end of the method chain.
+ Please note that relevant_search will return an Array and not a Criteria object. The search method should always be called in the end of the method chain.
Initializer
-----------
Alternatively, you can create an initializer to setup those options:
@@ -153,16 +157,15 @@
## Match partial words on both sides (slower)
config.regex = Proc.new { |query| /#{query}/ }
## Match partial words on the beginning or in the end (slightly faster)
- # config.regex = Proc.new { |query| /ˆ#{query}/ }
+ # config.regex = Proc.new { |query| /^#{query}/ }
# config.regex = Proc.new { |query| /#{query}$/ }
# Ligatures to be replaced
# http://en.wikipedia.org/wiki/Typographic_ligature
config.ligatures = { "œ"=>"oe", "æ"=>"ae" }
# Minimum word size. Words smaller than it won't be indexed
config.minimum_word_size = 2
end
-