activesearch 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +6 -4
- data/activesearch.gemspec +1 -1
- data/lib/activesearch.rb +0 -3
- data/lib/activesearch/mongoid.rb +16 -13
- data/lib/activesearch/mongoid/model.rb +36 -0
- data/lib/activesearch/version.rb +2 -2
- data/spec/mongoid_spec.rb +20 -6
- metadata +3 -2
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# ActiveSearch
|
2
2
|
|
3
3
|
This gem allows any class to be indexed by the chosen fulltext search engine.
|
4
4
|
|
@@ -22,7 +22,8 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
This is not a fulltext search engine, but it's a good solution for those users that don't have access to anything else.
|
24
24
|
It works by storing keywords taken from the specified fields and storing them in an Array field, which would be indexed.
|
25
|
-
|
25
|
+
search() method will return a Mongod::Criteria, so you can chain it with further scopes, like pagination.
|
26
|
+
You won't get original documents though.
|
26
27
|
|
27
28
|
## Usage
|
28
29
|
|
@@ -30,10 +31,11 @@ fts() method will return a Mongod::Criteria, so you can chain it with further sc
|
|
30
31
|
# [...] field definitions if needed
|
31
32
|
include ActiveSearch::Engine # "Engine" being your chosen engine ie. "Mongoid"
|
32
33
|
|
33
|
-
search_on :title, :body
|
34
|
+
search_on :title, :body, store: [:title]
|
34
35
|
end
|
35
36
|
|
36
|
-
|
37
|
+
# Access the stored fields so you don't need to fetch the real document
|
38
|
+
SomeModel.search("some words").first.stored["title"]
|
37
39
|
|
38
40
|
|
39
41
|
## Contributing
|
data/activesearch.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'activesearch/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "activesearch"
|
8
|
-
gem.version =
|
8
|
+
gem.version = ActiveSearch::VERSION
|
9
9
|
gem.authors = ["Rodrigo Alvarez"]
|
10
10
|
gem.email = ["papipo@gmail.com"]
|
11
11
|
gem.description = %q{ORM agnostic full text search}
|
data/lib/activesearch.rb
CHANGED
data/lib/activesearch/mongoid.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
+
require 'activesearch/mongoid/model'
|
2
|
+
|
1
3
|
module ActiveSearch
|
4
|
+
|
5
|
+
# TODO: Wrap this so all engines behave consistently
|
6
|
+
def self.search(text)
|
7
|
+
Mongoid::Model.all_in(keywords: text.split)
|
8
|
+
end
|
9
|
+
|
2
10
|
module Mongoid
|
3
11
|
def self.included(base)
|
4
12
|
base.extend ClassMethods
|
@@ -6,20 +14,15 @@ module ActiveSearch
|
|
6
14
|
|
7
15
|
module ClassMethods
|
8
16
|
def search_on(*fields)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
self._keywords = self._keywords | self[f].downcase.split if self[f]
|
17
|
+
# TODO: Use inheritable class variables, so ActiveSearch::Mongoid::Model can get fields and options from there
|
18
|
+
self.class_eval <<-EOV
|
19
|
+
after_save do
|
20
|
+
fields = #{fields}
|
21
|
+
options = fields.pop if fields.last.is_a?(Hash)
|
22
|
+
return unless fields.any? { |f| self.send("\#{f}_changed?") }
|
23
|
+
ActiveSearch::Mongoid::Model.reindex(self, fields, options)
|
17
24
|
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
def fts(query)
|
22
|
-
all_in(_keywords: query.split)
|
25
|
+
EOV
|
23
26
|
end
|
24
27
|
end
|
25
28
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActiveSearch
|
2
|
+
module Mongoid
|
3
|
+
class Model
|
4
|
+
include ::Mongoid::Document
|
5
|
+
|
6
|
+
field :type, type: String
|
7
|
+
field :original_id, type: BSON::ObjectId
|
8
|
+
field :keywords
|
9
|
+
field :stored, type: Hash, default: {}
|
10
|
+
|
11
|
+
index :keywords
|
12
|
+
index [:type, :original_id], unique: true
|
13
|
+
|
14
|
+
def store_fields(original, fields, options)
|
15
|
+
if options && options[:store]
|
16
|
+
(fields & options[:store]).each do |f|
|
17
|
+
self.stored[f] = original[f] if original.send("#{f}_changed?")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def refresh_keywords(original, fields)
|
23
|
+
self.keywords = fields.inject([]) do |memo,f|
|
24
|
+
original[f] ? memo | original[f].downcase.split : memo
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.reindex(original, fields, options)
|
29
|
+
doc = self.find_or_initialize_by(type: original.class.to_s, original_id: original.id)
|
30
|
+
doc.store_fields(original, fields, options)
|
31
|
+
doc.refresh_keywords(original, fields)
|
32
|
+
doc.save
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/activesearch/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.0.
|
1
|
+
module ActiveSearch
|
2
|
+
VERSION = "0.0.2"
|
3
3
|
end
|
data/spec/mongoid_spec.rb
CHANGED
@@ -10,26 +10,40 @@ class MongoidModel
|
|
10
10
|
field :title, type: String
|
11
11
|
field :text, type: String
|
12
12
|
field :junk, type: String
|
13
|
-
search_on :title, :text
|
13
|
+
search_on :title, :text, store: [:title]
|
14
|
+
end
|
15
|
+
|
16
|
+
class AnotherMongoidModel
|
17
|
+
include Mongoid::Document
|
18
|
+
include ActiveSearch::Mongoid
|
19
|
+
|
20
|
+
field :title, type: String
|
21
|
+
search_on :title, :text, store: [:title]
|
14
22
|
end
|
15
23
|
|
16
24
|
describe ActiveSearch::Mongoid do
|
17
25
|
before do
|
18
|
-
|
26
|
+
Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)
|
27
|
+
|
19
28
|
@findable = MongoidModel.create!(title: "Findable")
|
20
29
|
@quite_findable = MongoidModel.create!(title: "Some title", text: "Findable text")
|
21
|
-
|
30
|
+
@another = AnotherMongoidModel.create!(title: "Another findable title")
|
31
|
+
@junk = MongoidModel.create!(title: "Junk", junk: "Findable junk")
|
22
32
|
end
|
23
33
|
|
24
34
|
it "should find the expected documents" do
|
25
|
-
|
35
|
+
ActiveSearch.search("findable").map { |r| r.stored["title"] }.should == ["Findable", "Some title", "Another findable title"]
|
26
36
|
end
|
27
37
|
|
28
38
|
it "should store the proper keywords" do
|
29
|
-
@quite_findable.
|
39
|
+
ActiveSearch::Mongoid::Model.where(type: "MongoidModel", original_id: @quite_findable.id).first.keywords.should == %w{some title findable text}
|
30
40
|
end
|
31
41
|
|
32
42
|
it "should be chainable" do
|
33
|
-
|
43
|
+
ActiveSearch.search("findable").should respond_to(:where)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should store the specified fields" do
|
47
|
+
ActiveSearch::Mongoid::Model.where(type: "MongoidModel", original_id: @findable.id).first.stored.should == {"title" => "Findable"}
|
34
48
|
end
|
35
49
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activesearch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- activesearch.gemspec
|
75
75
|
- lib/activesearch.rb
|
76
76
|
- lib/activesearch/mongoid.rb
|
77
|
+
- lib/activesearch/mongoid/model.rb
|
77
78
|
- lib/activesearch/version.rb
|
78
79
|
- spec/config/mongoid.yml
|
79
80
|
- spec/mongoid_spec.rb
|