mongoid-xapian 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- = mongoid-xapian
1
+ # mongoid-xapian
2
2
 
3
3
  Xapian for mongoid
4
4
 
5
- == Usage
5
+ ## Usage
6
6
 
7
7
  Include MongoidXapian in your model and define what fields need to be
8
8
  indexed.
@@ -15,16 +15,21 @@ indexed.
15
15
  end
16
16
 
17
17
  now you have to run `MongoidXapian.index!` to index the changes into
18
- xapian.
19
- A rake task called `xapian:index` is also provided.
18
+ xapian. if you have a background job processor like delayed job you can
19
+ do something like:
20
20
 
21
- once you configure your model and index it you can search it using:
21
+ MongoidXapian.delay.index!
22
+
23
+ A rake task called `xapian:index` is also provided so you can setup a
24
+ cron task to run it periodically.
25
+
26
+ Once you configure your model and index the changes you can perform searches using:
22
27
 
23
28
  YourModel.search(query)
24
29
 
25
30
 
26
31
 
27
- == Contributing to mongoid-xapian
32
+ ## Contributing to mongoid-xapian
28
33
 
29
34
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
30
35
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
@@ -34,7 +39,7 @@ once you configure your model and index it you can search it using:
34
39
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
35
40
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
36
41
 
37
- == Copyright
42
+ ## Copyright
38
43
 
39
44
  Copyright (c) 2012 David A. Cuadrado. See LICENSE.txt for
40
45
  further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -17,6 +17,8 @@ module MongoidXapian
17
17
  attr_accessor :xapian_options, :xapian_fields
18
18
  end
19
19
 
20
+ MongoidXapian.indexable_models << self.to_s
21
+
20
22
  field :xapian_id, :type => Integer
21
23
 
22
24
  after_create do |doc|
@@ -26,9 +28,20 @@ module MongoidXapian
26
28
  end
27
29
 
28
30
  after_update do |doc|
29
- MongoidXapian::Trail.create(:action => :update,
30
- :doc_type => doc.class.to_s,
31
- :doc_id => doc.id)
31
+ needs_indexing = false
32
+ # see if at least one changed field is indexable
33
+ doc.changes.each do |key, value|
34
+ if doc.class.xapian_fields.include?(key.to_sym)
35
+ needs_indexing = true
36
+ break
37
+ end
38
+ end
39
+
40
+ if needs_indexing
41
+ MongoidXapian::Trail.create(:action => :update,
42
+ :doc_type => doc.class.to_s,
43
+ :doc_id => doc.id)
44
+ end
32
45
  end
33
46
 
34
47
  after_destroy do |doc|
@@ -57,6 +70,11 @@ module MongoidXapian
57
70
  MongoidXapian::Trail.index_all!
58
71
  end
59
72
 
73
+ # List of indexable models
74
+ def self.indexable_models
75
+ @indexable_models ||= Set.new
76
+ end
77
+
60
78
  module ClassMethods
61
79
  def fti(*args)
62
80
  # make sure id is not in the list
@@ -8,14 +8,20 @@ namespace :xapian do
8
8
  MongoidXapian::Trail.index_all!
9
9
  end
10
10
 
11
- desc "Reindex all documents in the given model. pass MODEL=ModelName to this task"
11
+ desc "Reindex all documents in the given model"
12
12
  task :reindex do
13
- model = ENV['MODEL'].constantize
14
- model.all.each do |doc|
15
- MongoidXapian::Trail.create!(:action => :update,
16
- :doc_id => doc.id,
17
- :doc_type => doc.class.to_s)
18
- MongoidXapian::Trail.index_all!
13
+ MongoidXapian.indexable_models.each do |mod|
14
+ model_class = mod.constantize rescue nil
15
+ if model_class
16
+ puts ">> Indexing #{model_class}..."
17
+
18
+ model_class.all.each do |doc|
19
+ MongoidXapian::Trail.create!(:action => :update,
20
+ :doc_id => doc.id,
21
+ :doc_type => doc.class.to_s)
22
+ MongoidXapian::Trail.index_all!
23
+ end
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -56,13 +56,15 @@ module MongoidXapian
56
56
 
57
57
  def update_document
58
58
  with_xapian_database do |db|
59
- if indexable.xapian_id.to_i > 0
60
- xapian_doc = XapianFu::XapianDoc.new(indexable.to_xapian.merge(:id => indexable.xapian_id), :xapian_db => db)
61
- xapian_doc.save
62
- else
63
- xapian_doc = db.add_doc(indexable.to_xapian)
64
- indexable.set(:xapian_id, xapian_doc.id)
65
- indexable.xapian_id = xapian_doc.id
59
+ if indexable.present?
60
+ if indexable.xapian_id.to_i > 0
61
+ xapian_doc = XapianFu::XapianDoc.new(indexable.to_xapian.merge(:id => indexable.xapian_id), :xapian_db => db)
62
+ xapian_doc.save
63
+ else
64
+ xapian_doc = db.add_doc(indexable.to_xapian)
65
+ indexable.set(:xapian_id, xapian_doc.id)
66
+ indexable.xapian_id = xapian_doc.id
67
+ end
66
68
  end
67
69
  end
68
70
  end
@@ -33,6 +33,7 @@ describe "MongoidXapian" do
33
33
  before do
34
34
  @blogpost = BlogPost.create(:title => "this is the title", :body => "this is body")
35
35
  MongoidXapian.index!
36
+ @blogpost.reload
36
37
  end
37
38
 
38
39
  it "should update the index" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-xapian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -211,7 +211,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
211
211
  version: '0'
212
212
  segments:
213
213
  - 0
214
- hash: 4374857612297856311
214
+ hash: -758691094896525050
215
215
  required_rubygems_version: !ruby/object:Gem::Requirement
216
216
  none: false
217
217
  requirements: