mongoid-searchable 0.1.2 → 0.1.3
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/lib/mongoid/searchable/version.rb +1 -1
- data/lib/mongoid/searchable.rb +46 -1
- metadata +14 -14
data/lib/mongoid/searchable.rb
CHANGED
@@ -12,6 +12,27 @@ module Mongoid
|
|
12
12
|
|
13
13
|
module ClassMethods
|
14
14
|
|
15
|
+
# Defines the environment for searching this document,
|
16
|
+
# typically called from within your model declaration.
|
17
|
+
#
|
18
|
+
# fields - Array, mix of references to the fields
|
19
|
+
# you would like to index and any *options*.
|
20
|
+
#
|
21
|
+
# options - Hash, list of options for keyword indexing:
|
22
|
+
#
|
23
|
+
# :as - String, field to store keywords in.
|
24
|
+
# :index - Boolean, turn indexing on or off.
|
25
|
+
#
|
26
|
+
# Example:
|
27
|
+
#
|
28
|
+
# class Person
|
29
|
+
# include Mongoid::Document
|
30
|
+
# include Mongoid::Searchable
|
31
|
+
# field :name
|
32
|
+
# searchable :name
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# Returns nothing.
|
15
36
|
def searchable(*fields)
|
16
37
|
options = { :as => :keywords, :index => true }.merge(fields.extract_options!)
|
17
38
|
|
@@ -25,13 +46,26 @@ module Mongoid
|
|
25
46
|
|
26
47
|
end
|
27
48
|
|
49
|
+
# Search for documents matching your query, given the previously
|
50
|
+
# defined keyword fields.
|
51
|
+
#
|
52
|
+
# query - Integer, String, Array, Hash representing the query
|
53
|
+
# you wish to perform. This will be reduced to a string, sanitized
|
54
|
+
# and then split into keywords used for matching.
|
55
|
+
#
|
56
|
+
# options - Hash, containing options used for the query:
|
57
|
+
#
|
58
|
+
# :match - Symbol, :all or :any, how to match results.
|
59
|
+
# :exact - Boolean, require exact word match (or not).
|
60
|
+
#
|
61
|
+
# Returns Mongoid::Criteria.
|
28
62
|
def search(query, options={})
|
29
63
|
keywords = clean_keywords(query)
|
30
64
|
options[:match] ||= :all
|
31
65
|
options[:exact] ||= false
|
32
66
|
|
33
67
|
if options[:exact]
|
34
|
-
match = keywords
|
68
|
+
match = keywords
|
35
69
|
else
|
36
70
|
match = keywords.collect{|k| /#{Regexp.escape(k)}/ }
|
37
71
|
end
|
@@ -51,6 +85,12 @@ module Mongoid
|
|
51
85
|
end
|
52
86
|
end
|
53
87
|
|
88
|
+
# Takes a String, Numeric, Array or Hash and reduces it to a
|
89
|
+
# sanitized array of keywords, recursively if necessary.
|
90
|
+
#
|
91
|
+
# value - String, Numeric, Array or Hash of data to sanitize.
|
92
|
+
#
|
93
|
+
# Returns Array.
|
54
94
|
def clean_keywords(value)
|
55
95
|
words = []
|
56
96
|
|
@@ -73,6 +113,11 @@ module Mongoid
|
|
73
113
|
|
74
114
|
module InstanceMethods
|
75
115
|
|
116
|
+
# Builds a list of keywords contained in the document given the
|
117
|
+
# keyword fields previously declared and stores them in the
|
118
|
+
# keywords field
|
119
|
+
#
|
120
|
+
# Returns nothing.
|
76
121
|
def build_keywords
|
77
122
|
keywords = []
|
78
123
|
self.class.searchable_fields.each do |f|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-searchable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-29 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
16
|
-
requirement: &
|
16
|
+
requirement: &70275946078720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70275946078720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bson_ext
|
27
|
-
requirement: &
|
27
|
+
requirement: &70275946078200 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '1.3'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70275946078200
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: growl
|
38
|
-
requirement: &
|
38
|
+
requirement: &70275946077820 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70275946077820
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
|
-
requirement: &
|
49
|
+
requirement: &70275946077220 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 0.9.2
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70275946077220
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rspec
|
60
|
-
requirement: &
|
60
|
+
requirement: &70275946076700 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '2.6'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70275946076700
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: guard-rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &70275946076220 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: 0.4.3
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70275946076220
|
80
80
|
description: Mongoid Searchable allows you to easily perform full-text search your
|
81
81
|
Mongoid models.
|
82
82
|
email:
|