Skip to content

Commit

Permalink
add reindex_immediately option (default: true), to suppress automatic…
Browse files Browse the repository at this point in the history
… reindexing
  • Loading branch information
joeyAghion committed Jan 12, 2012
1 parent 9375668 commit 2061088
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -243,6 +243,7 @@ Additional indexing/query options can be used as parameters to `fulltext_search_
* When set to a string, the string is evaluated within the document's instance.
* When set to a proc, the proc is called, and the document is given to the proc as the first arg.
* When set to any other type of object, the document's index will not be updated.
* `reindex_immediately`: whether models will be reindexed automatically upon saves or updates. Defaults to true. When set to false, the class-level `update_ngram_index` method can be called to perform reindexing.

Array filters
-------------
Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid_fulltext.rb
Expand Up @@ -32,6 +32,7 @@ def fulltext_search_in(*args)
:index_short_prefixes => false,
:max_candidate_set_size => 1000,
:remove_accents => true,
:reindex_immediately => true,
:stop_words => Hash[['i', 'a', 's', 't', 'me', 'my', 'we', 'he', 'it', 'am', 'is', 'be', 'do', 'an', 'if',
'or', 'as', 'of', 'at', 'by', 'to', 'up', 'in', 'on', 'no', 'so', 'our', 'you', 'him',
'his', 'she', 'her', 'its', 'who', 'are', 'was', 'has', 'had', 'did', 'the', 'and',
Expand All @@ -54,7 +55,7 @@ def fulltext_search_in(*args)
config[:word_separators] = Hash[config[:word_separators].split('').map{ |ch| [ch,ch] }]
self.mongoid_fulltext_config[index_name] = config

before_save :update_ngram_index
before_save(:update_ngram_index) if config[:reindex_immediately]
before_destroy :remove_from_ngram_index
end

Expand Down
8 changes: 8 additions & 0 deletions spec/models/delayed_artwork.rb
@@ -0,0 +1,8 @@
class DelayedArtwork
include Mongoid::Document
include Mongoid::FullTextSearch

field :title
fulltext_search_in :title, :reindex_immediately => false

end
11 changes: 11 additions & 0 deletions spec/mongoid/fulltext_spec.rb
Expand Up @@ -669,5 +669,16 @@ module Mongoid
end

end

context "batched reindexing" do
let!(:flowers1) { DelayedArtwork.create(:title => 'Flowers 1') }

it "should not rebuild index until explicitly invoked" do
DelayedArtwork.fulltext_search("flowers").length.should == 0
DelayedArtwork.update_ngram_index
DelayedArtwork.fulltext_search("flowers").length.should == 1
end
end

end
end

0 comments on commit 2061088

Please sign in to comment.