rmla 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
6
+ TODO.md
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'slingshot-rb', '0.0.8'
4
+ gem 'mongoid', '>= 2.1.8'
5
+
6
+ group :development do
7
+ gem "bson_ext", "~> 1.3.1"
8
+ gem "bundler", "1.2.0.pre"
9
+ gem "jeweler", "~> 1.6.4"
10
+ gem "rcov", ">= 0"
11
+ gem 'rspec', '~> 2.3.0'
12
+ gem 'yard', '~> 0.6.0'
13
+ gem 'mongoid-rspec', '~> 1.4.4'
14
+ gem 'database_cleaner', '0.6.4'
15
+ gem 'bluecloth', '~> 2.1.0'
16
+
17
+ #optional but handy
18
+ gem 'irbtools', :require => 'irbtools/configure'
19
+
20
+ gem "ruby-debug", :platform => :ruby_18
21
+ gem "ruby-debug19", :platform => :ruby_19
22
+ end
@@ -0,0 +1,139 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.10)
5
+ activesupport (= 3.0.10)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activesupport (3.0.10)
9
+ alias (0.2.2)
10
+ archive-tar-minitar (0.5.2)
11
+ awesome_print (0.4.0)
12
+ bluecloth (2.1.0)
13
+ boson (0.3.4)
14
+ alias (>= 0.2.2)
15
+ hirb (>= 0.5.0)
16
+ bson (1.3.1)
17
+ bson_ext (1.3.1)
18
+ builder (2.1.2)
19
+ clipboard (0.9.9)
20
+ coderay (1.0.5)
21
+ columnize (0.3.6)
22
+ database_cleaner (0.6.4)
23
+ diff-lcs (1.1.2)
24
+ every_day_irb (1.1.1)
25
+ fancy_irb (0.7.2)
26
+ paint (>= 0.8.1)
27
+ unicode-display_width (>= 0.1.1)
28
+ g (1.5.0)
29
+ ruby_gntp
30
+ git (1.2.5)
31
+ hirb (0.5.0)
32
+ i18n (0.5.0)
33
+ interactive_editor (0.0.10)
34
+ spoon (>= 0.0.1)
35
+ irbtools (1.1.1)
36
+ awesome_print (~> 0.4.0)
37
+ boson (>= 0.3.4)
38
+ clipboard (>= 0.9.9)
39
+ coderay (~> 1.0.0)
40
+ every_day_irb (>= 1.1.1)
41
+ fancy_irb (>= 0.7.2)
42
+ g (>= 1.4.0)
43
+ hirb (~> 0.5.0)
44
+ interactive_editor (>= 0.0.10)
45
+ looksee (~> 1.0.3)
46
+ method_locator (>= 0.0.4)
47
+ method_source (>= 0.6.7)
48
+ methodfinder (>= 1.2.3)
49
+ ori (~> 0.1.0)
50
+ paint (>= 0.8.3)
51
+ rvm_loader (>= 1.0.0)
52
+ sketches (>= 0.1.1)
53
+ wirb (>= 0.4.1)
54
+ zucker (>= 11)
55
+ jeweler (1.6.4)
56
+ bundler (1.2.0.pre)
57
+ git (>= 1.2.5)
58
+ rake
59
+ linecache (0.46)
60
+ rbx-require-relative (> 0.0.4)
61
+ linecache19 (0.5.12)
62
+ ruby_core_source (>= 0.1.4)
63
+ looksee (1.0.3)
64
+ method_locator (0.0.4)
65
+ method_source (0.7.0)
66
+ methodfinder (1.2.5)
67
+ mime-types (1.16)
68
+ mongo (1.3.1)
69
+ bson (>= 1.3.1)
70
+ mongoid (2.1.8)
71
+ activemodel (~> 3.0)
72
+ mongo (~> 1.3)
73
+ tzinfo (~> 0.3.22)
74
+ mongoid-rspec (1.4.4)
75
+ mongoid (~> 2.0)
76
+ rspec (~> 2)
77
+ ori (0.1.0)
78
+ paint (0.8.3)
79
+ rake (0.9.2)
80
+ rbx-require-relative (0.0.5)
81
+ rcov (0.9.9)
82
+ rest-client (1.6.3)
83
+ mime-types (>= 1.16)
84
+ rspec (2.3.0)
85
+ rspec-core (~> 2.3.0)
86
+ rspec-expectations (~> 2.3.0)
87
+ rspec-mocks (~> 2.3.0)
88
+ rspec-core (2.3.1)
89
+ rspec-expectations (2.3.0)
90
+ diff-lcs (~> 1.1.2)
91
+ rspec-mocks (2.3.0)
92
+ ruby-debug (0.10.4)
93
+ columnize (>= 0.1)
94
+ ruby-debug-base (~> 0.10.4.0)
95
+ ruby-debug-base (0.10.4)
96
+ linecache (>= 0.3)
97
+ ruby-debug-base19 (0.11.25)
98
+ columnize (>= 0.3.1)
99
+ linecache19 (>= 0.5.11)
100
+ ruby_core_source (>= 0.1.4)
101
+ ruby-debug19 (0.11.6)
102
+ columnize (>= 0.3.1)
103
+ linecache19 (>= 0.5.11)
104
+ ruby-debug-base19 (>= 0.11.19)
105
+ ruby_core_source (0.1.5)
106
+ archive-tar-minitar (>= 0.5.2)
107
+ ruby_gntp (0.3.4)
108
+ rvm_loader (1.0.0)
109
+ sketches (0.1.1)
110
+ slingshot-rb (0.0.8)
111
+ bundler (1.2.0.pre)
112
+ rest-client (~> 1.6.0)
113
+ yajl-ruby (> 0.7.9)
114
+ spoon (0.0.1)
115
+ tzinfo (0.3.29)
116
+ unicode-display_width (0.1.1)
117
+ wirb (0.4.1)
118
+ yajl-ruby (0.8.2)
119
+ yard (0.6.7)
120
+ zucker (11)
121
+
122
+ PLATFORMS
123
+ ruby
124
+
125
+ DEPENDENCIES
126
+ bluecloth (~> 2.1.0)
127
+ bson_ext (~> 1.3.1)
128
+ bundler (1.2.0.pre)
129
+ database_cleaner (= 0.6.4)
130
+ irbtools
131
+ jeweler (~> 1.6.4)
132
+ mongoid (>= 2.1.8)
133
+ mongoid-rspec (~> 1.4.4)
134
+ rcov
135
+ rspec (~> 2.3.0)
136
+ ruby-debug
137
+ ruby-debug19
138
+ slingshot-rb (= 0.0.8)
139
+ yard (~> 0.6.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Omar Mekky
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,255 @@
1
+ Mebla
2
+ ====
3
+
4
+ Mebla is an [elasticsearch](http://www.elasticsearch.org) wrapper for [Mongoid](http://mongoid.org) based on
5
+ [Slingshot](https://github.com/karmi/slingshot).
6
+
7
+ Name
8
+ ---------
9
+
10
+ Mebla is derived from the word "Nebla", which means slingshot in arabic.
11
+
12
+ Also since its a wrapper for mongoid ODM, the letter "N" is replaced with "M".
13
+
14
+ Installation
15
+ ---------------
16
+
17
+ ### Install elasticsearch
18
+
19
+ Mebla requires a running [elasticsearch](http://www.elasticsearch.org) installation.
20
+
21
+ To install elasticsearch follow the uptodate instructions [here](http://www.elasticsearch.org/guide/reference/setup/) or
22
+ simply copy and paste in your terminal window:
23
+
24
+ $ curl -k -L -o elasticsearch-0.15.0.tar.gz http://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.15.0.tar.gz
25
+ $ tar -zxvf elasticsearch-0.15.0.tar.gz
26
+ $ ./elasticsearch-0.15.0/bin/elasticsearch -f
27
+
28
+ ### Install Mebla
29
+
30
+ Once elasticsearch is installed, add Mebla to your gem file:
31
+
32
+ gem "mebla"
33
+
34
+ then run bundle in your application root to update your gems' bundle:
35
+
36
+ $ bundle install
37
+
38
+ next generate the configuration file:
39
+
40
+ $ rails generate mebla:install
41
+
42
+ finally index your data:
43
+
44
+ $ rake mebla:index_data
45
+
46
+ Usage
47
+ ---------
48
+
49
+ ### Defining indexed fields
50
+
51
+ To enable searching models, you first have to define which fields mebla should index:
52
+
53
+ class Post
54
+ include Mongoid::Document
55
+ include Mongoid::Mebla
56
+ field :title
57
+ field :author
58
+ field :body
59
+ field :publish_date, :type => Date
60
+ field :tags, :type => Array
61
+
62
+ embeds_many :comments
63
+ search_in :author, :body, :publish_date, :tags, :title => { :boost => 2.0, :analyzer => 'snowball' }
64
+ end
65
+
66
+ In the example above, mebla will index the author field, body field, publish_date field and finally indexes
67
+ the title field with some custom [mappings](http://www.elasticsearch.org/guide/reference/mapping).
68
+
69
+ #### Embedded documents
70
+
71
+ You can also index embedded documents as follows:
72
+
73
+ class Comment
74
+ include Mongoid::Document
75
+ include Mongoid::Mebla
76
+ field :comment
77
+ field :author
78
+
79
+ embedded_in :blog_post
80
+ search_in :comment, :author, :embedded_in => :blog_post
81
+ end
82
+
83
+ This will index all comments and make it available for searching directly through the Comment model.
84
+
85
+ #### Indexing methods
86
+
87
+ You can also index method results:
88
+
89
+ class Post
90
+ include Mongoid::Document
91
+ include Mongoid::Mebla
92
+ field :title
93
+ field :author
94
+ field :body
95
+ field :publish_date, :type => Date
96
+ field :tags, :type => Array
97
+
98
+ embeds_many :comments
99
+ search_in :author, :body, :publish_date, :tags, :permalink, :title => { :boost => 2.0, :analyzer => 'snowball' }
100
+
101
+ def permalink
102
+ self.title.gsub(/\s/, "-").downcase
103
+ end
104
+ end
105
+
106
+ This will index the result of the method permalink.
107
+
108
+ #### Indexing fields of relations
109
+
110
+ You can also index fields of relations:
111
+
112
+ class Post
113
+ include Mongoid::Document
114
+ include Mongoid::Mebla
115
+ field :title
116
+ field :author
117
+ field :body
118
+ field :publish_date, :type => Date
119
+ field :tags, :type => Array
120
+
121
+ embeds_many :comments
122
+ search_in :author, :body, :publish_date, :tags, :title => { :boost => 2.0, :analyzer => 'snowball' },
123
+ :search_relations => {:comments => :author}
124
+ end
125
+
126
+ This will index authors of all comments embedded with this Post.
127
+
128
+ ### Searching the index
129
+
130
+ Mebla supports two types of search, index search and model search; in index search Mebla searches
131
+ the index and returns all matching documents regardless of their types, in model search however
132
+ Mebla searches the index and returns matching documents of the model(s) type(s).
133
+
134
+ #### Index searching
135
+
136
+ Using the same models we defined above, we can search for all posts and comments with the author "cousine":
137
+
138
+ Mebla.search "author: cousine"
139
+
140
+ This will return all documents with an author set to "cousine" regardless of their type, if we however want to
141
+ search only Posts and Comments, we would explicitly tell Mebla:
142
+
143
+ Mebla.search "author: cousine", [:post, :comment]
144
+
145
+ #### Model searching
146
+
147
+ Instead of searching all models like index searching, we can search one model only:
148
+
149
+ Post.search("title: Testing Search").desc(:publish_date).only(
150
+ :author => ["cousine"],
151
+ :tags => ["ruby", "rails"]
152
+ ).facet('tags', :tags, :global => true).facet('authors', :author)
153
+
154
+ In the above example we are taking full advantage of slingshot's searching capabilities,
155
+ we are getting all posts with the title "Testing Search", filtering the results with author
156
+ "cousine", tagged "ruby" or "rails", and sorting the results with their publish_date fields.
157
+
158
+ One more feature we are using is "Faceted Search", from Slingshot's homepage:
159
+
160
+ > _Faceted Search_
161
+ >
162
+ > _ElasticSearch makes it trivial to retrieve complex aggregated data from the index/database, so called
163
+ [facets](http://www.lucidimagination.com/Community/Hear-from-the-Experts/Articles/Faceted-Search-Solr)._
164
+
165
+ In the example above we are retrieving two facets, "tags" and "authors"; "tags" are global
166
+ which means that we want to get the counts of posts for each tag over the whole index, "authors"
167
+ however will only get the count of posts matching the search query for each author.
168
+
169
+ #### Retrieving results
170
+
171
+ To retrieve the results of the model search we performed above we would simply:
172
+
173
+ hits = Post.search("title: Testing Search").desc(:publish_date).only(
174
+ :author => ["cousine"],
175
+ :tags => ["ruby", "rails"]
176
+ ).facet('tags', :tags, :global => true).facet('authors', :author)
177
+
178
+ hits.each do |hit|
179
+ puts hit.title
180
+ end
181
+
182
+ To retrieve the facets:
183
+
184
+ # Get the count of posts for each tag accross the index
185
+ hits.facets['tags']['terms'].each do |facet|
186
+ puts "#{facet['term']} : #{facet['count']}"
187
+ end
188
+
189
+ # Get the count of posts matching the query for each author
190
+ hits.facets['authors']['terms'].each do |facet|
191
+ puts "#{facet['term']} : #{facet['count']}"
192
+ end
193
+
194
+ ### Indexing data
195
+
196
+ #### Synchronizing data
197
+
198
+ By default Mebla synchronizes all changes done to your models with your index, if however
199
+ you would like to bypass this behavior:
200
+
201
+ Post.without_indexing do
202
+ Post.create :title => "This won't be indexed"
203
+ end
204
+
205
+ #### Indexing existing data
206
+
207
+ You can index existing data by using the "index" rake task:
208
+
209
+ $ rake mebla:index
210
+
211
+ This will create the index and index all the data in the database
212
+
213
+ #### Reindexing
214
+
215
+ Just like indexing, you can reindex your data using the "reindex" rake task:
216
+
217
+ $ rake mebla:reindex
218
+
219
+ This will rebuild the index and index all your data again, note that unlike other full-text
220
+ search engines, you don't need to reindex your data frequently (if ever) however you
221
+ might want to refresh the index so changes are reflected on the index.
222
+
223
+ #### Refreshing the index
224
+
225
+ Refreshing the index makes changes done to the index available for searching or modification.
226
+
227
+ Mebla automatically refreshes the index whenever a change is done, but just incase you
228
+ need to refresh the index:
229
+
230
+ $ rake mebla:refresh
231
+
232
+ ### Rake tasks
233
+
234
+ Mebla provides a number of rake tasks to perform various tasks on the index, you can
235
+ list all tasks using this command:
236
+
237
+ $ rake -T mebla
238
+
239
+ Contributing to Mebla
240
+ ----------------------------
241
+
242
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
243
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
244
+ * Fork the project
245
+ * Start a feature/bugfix branch
246
+ * Commit and push until you are happy with your contribution
247
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
248
+ * 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.
249
+
250
+ Copyright
251
+ -------------
252
+
253
+ Copyright (c) 2011 Omar Mekky. See LICENSE.txt for
254
+ further details.
255
+