elasticsearch-rails 0.0.0 → 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt CHANGED
@@ -1,22 +1,13 @@
1
- Copyright (c) 2013 Karel Minarik
1
+ Copyright (c) 2014 Elasticsearch
2
2
 
3
- MIT License
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
4
6
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
7
+ http://www.apache.org/licenses/LICENSE-2.0
12
8
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md CHANGED
@@ -1,29 +1,71 @@
1
1
  # Elasticsearch::Rails
2
2
 
3
- TODO: Write a gem description
3
+ The `elasticsearch-rails` library is a companion for the
4
+ the [`elasticsearch-model`](https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-model)
5
+ library, providing features suitable for Ruby on Rails applications.
6
+
7
+ The library is compatible with Ruby 1.9.3 and higher.
4
8
 
5
9
  ## Installation
6
10
 
7
- Add this line to your application's Gemfile:
11
+ Install the package from [Rubygems](https://rubygems.org):
8
12
 
9
- gem 'elasticsearch-rails'
13
+ gem install elasticsearch-rails --pre
10
14
 
11
- And then execute:
15
+ To use an unreleased version, either add it to your `Gemfile` for [Bundler](http://bundler.io):
12
16
 
13
- $ bundle
17
+ gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
14
18
 
15
- Or install it yourself as:
19
+ or install it from a source code checkout:
16
20
 
17
- $ gem install elasticsearch-rails
21
+ git clone https://github.com/elasticsearch/elasticsearch-rails.git
22
+ cd elasticsearch-rails/elasticsearch-rails
23
+ bundle install
24
+ rake install
18
25
 
19
26
  ## Usage
20
27
 
21
- TODO: Write usage instructions here
28
+ You can generate a fully working example Ruby on Rails application, with an `Article` model and a search form,
29
+ to play with (it even downloads _Elasticsearch_ itself, generates the application skeleton and leaves you with
30
+ a _Git_ repository to explore the steps and the code):
31
+
32
+ ```bash
33
+ rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/01-basic.rb
34
+ ```
35
+
36
+ Run the same command with the `02-pretty` template to add features such as a custom `Article.search` method,
37
+ result highlighting and [_Bootstrap_](http://getbootstrap.com) integration:
38
+
39
+ ```bash
40
+ rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/02-pretty.rb
41
+ ```
42
+
43
+ NOTE: A third, much more complex template, demonstrating other features such as faceted navigation or
44
+ query suggestions is being worked on.
45
+
46
+ ## TODO
47
+
48
+ This is an initial release of the `elasticsearch-rails` library. Many more features are planned and/or
49
+ being worked on, such as:
50
+
51
+ * Rake tasks for convenient (re)indexing your models from the command line
52
+ * Hooking into Rails' notification system to display Elasticsearch related statistics in the application log
53
+ * Instrumentation support for NewRelic integration
54
+
55
+ ## License
56
+
57
+ This software is licensed under the Apache 2 license, quoted below.
58
+
59
+ Copyright (c) 2014 Elasticsearch <http://www.elasticsearch.org>
60
+
61
+ Licensed under the Apache License, Version 2.0 (the "License");
62
+ you may not use this file except in compliance with the License.
63
+ You may obtain a copy of the License at
22
64
 
23
- ## Contributing
65
+ http://www.apache.org/licenses/LICENSE-2.0
24
66
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
67
+ Unless required by applicable law or agreed to in writing, software
68
+ distributed under the License is distributed on an "AS IS" BASIS,
69
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
70
+ See the License for the specific language governing permissions and
71
+ limitations under the License.
data/Rakefile CHANGED
@@ -1 +1,53 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ desc "Run unit tests"
4
+ task :default => 'test:unit'
5
+ task :test => 'test:unit'
6
+
7
+ # ----- Test tasks ------------------------------------------------------------
8
+
9
+ require 'rake/testtask'
10
+ namespace :test do
11
+ task :ci_reporter do
12
+ ENV['CI_REPORTS'] ||= 'tmp/reports'
13
+ require 'ci/reporter/rake/minitest'
14
+ Rake::Task['ci:setup:minitest'].invoke
15
+ end
16
+
17
+ Rake::TestTask.new(:unit) do |test|
18
+ Rake::Task['test:ci_reporter'].invoke if ENV['CI']
19
+ test.libs << 'lib' << 'test'
20
+ test.test_files = FileList["test/unit/**/*_test.rb"]
21
+ # test.verbose = true
22
+ # test.warning = true
23
+ end
24
+
25
+ Rake::TestTask.new(:integration) do |test|
26
+ Rake::Task['test:ci_reporter'].invoke if ENV['CI']
27
+ test.libs << 'lib' << 'test'
28
+ test.test_files = FileList["test/integration/**/*_test.rb"]
29
+ end
30
+
31
+ Rake::TestTask.new(:all) do |test|
32
+ Rake::Task['test:ci_reporter'].invoke if ENV['CI']
33
+ test.libs << 'lib' << 'test'
34
+ test.test_files = FileList["test/unit/**/*_test.rb", "test/integration/**/*_test.rb"]
35
+ end
36
+ end
37
+
38
+ # ----- Documentation tasks ---------------------------------------------------
39
+
40
+ require 'yard'
41
+ YARD::Rake::YardocTask.new(:doc) do |t|
42
+ t.options = %w| --embed-mixins --markup=markdown |
43
+ end
44
+
45
+ # ----- Code analysis tasks ---------------------------------------------------
46
+
47
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
48
+ require 'cane/rake_task'
49
+ Cane::RakeTask.new(:quality) do |cane|
50
+ cane.abc_max = 15
51
+ cane.no_style = true
52
+ end
53
+ end
@@ -3,21 +3,39 @@ lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'elasticsearch/rails/version'
5
5
 
6
- Gem::Specification.new do |spec|
7
- spec.name = "elasticsearch-rails"
8
- spec.version = Elasticsearch::Rails::VERSION
9
- spec.authors = ["Karel Minarik"]
10
- spec.email = ["karel.minarik@elasticsearch.org"]
11
- spec.description = %q{Elasticsearch gem integration for Rails (WIP)}
12
- spec.summary = spec.description
13
- spec.homepage = ""
14
- spec.license = "Apache 2"
6
+ Gem::Specification.new do |s|
7
+ s.name = "elasticsearch-rails"
8
+ s.version = Elasticsearch::Rails::VERSION
9
+ s.authors = ["Karel Minarik"]
10
+ s.email = ["karel.minarik@elasticsearch.org"]
11
+ s.description = "Ruby on Rails integrations for Elasticsearch."
12
+ s.summary = "Ruby on Rails integrations for Elasticsearch."
13
+ s.homepage = "https://github.com/elasticsearch/elasticsearch-rails/"
14
+ s.license = "Apache 2"
15
15
 
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
16
+ s.files = `git ls-files`.split($/)
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+ s.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
21
+ s.extra_rdoc_files = [ "README.md", "LICENSE.txt" ]
22
+ s.rdoc_options = [ "--charset=UTF-8" ]
23
+
24
+ s.add_development_dependency "bundler", "~> 1.3"
25
+ s.add_development_dependency "rake"
26
+
27
+ s.add_development_dependency "shoulda-context"
28
+ s.add_development_dependency "mocha"
29
+ s.add_development_dependency "turn"
30
+ s.add_development_dependency "yard"
31
+ s.add_development_dependency "ruby-prof"
32
+ s.add_development_dependency "pry"
33
+ s.add_development_dependency "ci_reporter"
34
+
35
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
36
+ s.add_development_dependency "simplecov"
37
+ s.add_development_dependency "cane"
38
+ s.add_development_dependency "require-prof"
39
+ s.add_development_dependency "coveralls"
40
+ end
23
41
  end
@@ -1,5 +1,5 @@
1
1
  module Elasticsearch
2
2
  module Rails
3
- VERSION = "0.0.0"
3
+ VERSION = "0.1.0.rc1"
4
4
  end
5
5
  end
@@ -0,0 +1,318 @@
1
+ # =====================================================================================================
2
+ # Template for generating a no-frills Rails application with support for Elasticsearch full-text search
3
+ # =====================================================================================================
4
+ #
5
+ # This file creates a basic, fully working Rails application with support for Elasticsearch full-text
6
+ # search via the `elasticsearch-rails` gem; https://github.com/elasticsearch/elasticsearch-rails.
7
+ #
8
+ # Requirements:
9
+ # -------------
10
+ #
11
+ # * Git
12
+ # * Ruby >= 1.9.3
13
+ # * Rails >= 4
14
+ # * Java >= 7 (for Elasticsearch)
15
+ #
16
+ # Usage:
17
+ # ------
18
+ #
19
+ # $ rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/01-basic.rb
20
+ #
21
+ # =====================================================================================================
22
+
23
+ require 'elasticsearch'
24
+ client = Elasticsearch::Client.new
25
+
26
+ at_exit do
27
+ pid = File.read("#{destination_root}/tmp/pids/elasticsearch.pid") rescue nil
28
+ if pid
29
+ say_status "Stop", "Elasticsearch", :yellow
30
+ run "kill #{pid}"
31
+ end
32
+ end
33
+
34
+ run "touch tmp/.gitignore"
35
+
36
+ append_to_file ".gitignore", "vendor/elasticsearch-0.90.7/\n"
37
+
38
+ git :init
39
+ git add: "."
40
+ git commit: "-m 'Initial commit: Clean application'"
41
+
42
+ # ----- Download Elasticsearch --------------------------------------------------------------------
43
+
44
+ unless (client.ping rescue false)
45
+ COMMAND = <<-COMMAND.gsub(/^ /, '')
46
+ curl -# -O "http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.tar.gz"
47
+ tar -zxf elasticsearch-0.90.7.tar.gz
48
+ rm -f elasticsearch-0.90.7.tar.gz
49
+ ./elasticsearch-0.90.7/bin/elasticsearch -p #{destination_root}/tmp/pids/elasticsearch.pid
50
+ COMMAND
51
+
52
+ puts "\n"
53
+ say_status "ERROR", "Elasticsearch not running!\n", :red
54
+ puts '-'*80
55
+ say_status '', "It appears that Elasticsearch is not running on this machine."
56
+ say_status '', "Is it installed? Do you want me to install it for you with this command?\n\n"
57
+ COMMAND.each_line { |l| say_status '', "$ #{l}" }
58
+ puts
59
+ say_status '', "(To uninstall, just remove the generated application directory.)"
60
+ puts '-'*80, ''
61
+
62
+ if yes?("Install Elasticsearch?", :bold)
63
+ puts
64
+ say_status "Install", "Elasticsearch", :yellow
65
+
66
+ commands = COMMAND.split("\n")
67
+ exec = commands.pop
68
+ inside("vendor") do
69
+ commands.each { |command| run command }
70
+ run "(#{exec})" # Launch Elasticsearch in subshell
71
+ end
72
+ end
73
+ end
74
+
75
+ # ----- Add README --------------------------------------------------------------------------------
76
+
77
+ puts
78
+ say_status "README", "Adding Readme...\n", :yellow
79
+ puts '-'*80, ''; sleep 0.25
80
+
81
+ remove_file 'README.rdoc'
82
+
83
+ create_file 'README.rdoc', <<-README
84
+ = Ruby on Rails and Elasticsearch: Example application
85
+
86
+ This application is an example of integrating the {Elasticsearch}[http://www.elasticsearch.org]
87
+ search engine with the {Ruby On Rails}[http://rubyonrails.org] web framework.
88
+
89
+ It has been generated by application templates available at
90
+ https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-rails/lib/rails/templates.
91
+
92
+ == [1] Basic
93
+
94
+ The `basic` version provides a simple integration for a simple Rails model, `Article`, showing how
95
+ to include the search engine support in your model, automatically index changes to records,
96
+ and use a form to perform simple search require 'requests.'
97
+
98
+ README
99
+
100
+
101
+ git add: "."
102
+ git commit: "-m '[01] Added README for the application'"
103
+
104
+ # ----- Use Thin ----------------------------------------------------------------------------------
105
+
106
+ begin
107
+ require 'thin'
108
+ puts
109
+ say_status "Rubygems", "Adding Thin into Gemfile...\n", :yellow
110
+ puts '-'*80, '';
111
+
112
+ gem 'thin'
113
+ rescue LoadError
114
+ end
115
+
116
+ # ----- Auxiliary gems ----------------------------------------------------------------------------
117
+
118
+ gem 'turn', group: 'test'
119
+ gem 'mocha', group: 'test', require: 'mocha/setup'
120
+
121
+ # ----- Remove CoffeeScript, Sass and "all that jazz" ---------------------------------------------
122
+
123
+ comment_lines 'Gemfile', /gem 'coffee/
124
+ comment_lines 'Gemfile', /gem 'sass/
125
+ comment_lines 'Gemfile', /gem 'uglifier/
126
+
127
+ # ----- Add gems into Gemfile ---------------------------------------------------------------------
128
+
129
+ puts
130
+ say_status "Rubygems", "Adding Elasticsearch libraries into Gemfile...\n", :yellow
131
+ puts '-'*80, ''; sleep 0.75
132
+
133
+ gem 'elasticsearch', git: 'git@github.com:elasticsearch/elasticsearch-ruby.git'
134
+ gem 'elasticsearch-model', git: 'git@github.com:elasticsearch/elasticsearch-rails.git'
135
+ gem 'elasticsearch-rails', git: 'git@github.com:elasticsearch/elasticsearch-rails.git'
136
+
137
+ # ----- Install gems ------------------------------------------------------------------------------
138
+
139
+ puts
140
+ say_status "Rubygems", "Installing Rubygems...", :yellow
141
+ puts '-'*80, ''
142
+
143
+ run "bundle install"
144
+
145
+ git add: "Gemfile*"
146
+ git commit: "-m 'Added libraries into Gemfile'"
147
+
148
+ # ----- Generate Article resource -----------------------------------------------------------------
149
+
150
+ puts
151
+ say_status "Model", "Generating the Article resource...", :yellow
152
+ puts '-'*80, ''; sleep 0.75
153
+
154
+ generate :scaffold, "Article title:string content:text published_on:date"
155
+ route "root to: 'articles#index'"
156
+ rake "db:migrate"
157
+
158
+ git add: "."
159
+ git commit: "-m 'Added the generated Article resource'"
160
+
161
+ # ----- Add Elasticsearch integration into the model ----------------------------------------------
162
+
163
+ puts
164
+ say_status "Model", "Adding search support into the Article model...", :yellow
165
+ puts '-'*80, ''; sleep 0.25
166
+
167
+ run "rm -f app/models/article.rb"
168
+ file 'app/models/article.rb', <<-CODE
169
+ class Article < ActiveRecord::Base
170
+ include Elasticsearch::Model
171
+ include Elasticsearch::Model::Callbacks
172
+ end
173
+ CODE
174
+
175
+ git commit: "-a -m 'Added Elasticsearch support into the Article model'"
176
+
177
+ # ----- Add Elasticsearch integration into the interface ------------------------------------------
178
+
179
+ puts
180
+ say_status "Controller", "Adding controller action, route, and HTML for searching...", :yellow
181
+ puts '-'*80, ''; sleep 0.25
182
+
183
+ inject_into_file 'app/controllers/articles_controller.rb', before: %r|^\s*# GET /articles/1$| do
184
+ <<-CODE
185
+
186
+ # GET /articles/search
187
+ def search
188
+ @articles = Article.search(params[:q]).records
189
+
190
+ render action: "index"
191
+ end
192
+
193
+ CODE
194
+ end
195
+
196
+ inject_into_file 'app/views/articles/index.html.erb', after: %r{<h1>Listing articles</h1>} do
197
+ <<-CODE
198
+
199
+ <hr>
200
+
201
+ <%= form_tag search_articles_path, method: 'get' do %>
202
+ <%= label_tag :query %>
203
+ <%= text_field_tag :q, params[:q] %>
204
+ <%= submit_tag :search %>
205
+ <% end %>
206
+
207
+ <hr>
208
+
209
+ CODE
210
+ end
211
+
212
+ inject_into_file 'app/views/articles/index.html.erb', after: %r{<%= link_to 'New Article', new_article_path %>} do
213
+ <<-CODE
214
+ <%= link_to 'All Articles', articles_path if params[:q] %>
215
+ CODE
216
+ end
217
+
218
+ gsub_file 'config/routes.rb', %r{resources :articles$}, <<-CODE
219
+ resources :articles do
220
+ collection { get :search }
221
+ end
222
+ CODE
223
+
224
+ gsub_file 'test/controllers/articles_controller_test.rb', %r{setup do.*?end}m, <<-CODE
225
+ setup do
226
+ @article = articles(:one)
227
+
228
+ Article.__elasticsearch__.import
229
+ Article.__elasticsearch__.refresh_index!
230
+ end
231
+ CODE
232
+
233
+ inject_into_file 'test/controllers/articles_controller_test.rb', after: %r{test "should get index" do.*?end}m do
234
+ <<-CODE
235
+
236
+
237
+ test "should get search results" do
238
+ get :search, q: 'mystring'
239
+ assert_response :success
240
+ assert_not_nil assigns(:articles)
241
+ assert_equal 2, assigns(:articles).size
242
+ end
243
+ CODE
244
+ end
245
+
246
+ git commit: "-a -m 'Added search form and controller action'"
247
+
248
+ # ----- Seed the database -------------------------------------------------------------------------
249
+
250
+ puts
251
+ say_status "Database", "Seeding the database with data...", :yellow
252
+ puts '-'*80, ''; sleep 0.25
253
+
254
+ remove_file "db/seeds.rb"
255
+ create_file 'db/seeds.rb', %q{
256
+ contents = [
257
+ 'Lorem ipsum dolor sit amet.',
258
+ 'Consectetur adipisicing elit, sed do eiusmod tempor incididunt.',
259
+ 'Labore et dolore magna aliqua.',
260
+ 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.',
261
+ 'Excepteur sint occaecat cupidatat non proident.'
262
+ ]
263
+
264
+ puts "Deleting all articles..."
265
+ Article.delete_all
266
+
267
+ unless ENV['COUNT']
268
+
269
+ puts "Creating articles..."
270
+ %w[ One Two Three Four Five ].each_with_index do |title, i|
271
+ Article.create title: title, content: contents[i], published_on: i.days.ago.utc
272
+ end
273
+
274
+ else
275
+
276
+ print "Generating articles..."
277
+ (1..ENV['COUNT'].to_i).each_with_index do |title, i|
278
+ Article.create title: "Title #{title}", content: 'Lorem ipsum dolor', published_on: i.days.ago.utc
279
+ print '.' if i % ENV['COUNT'].to_i/10 == 0
280
+ end
281
+ puts "\n"
282
+
283
+ end
284
+ }
285
+
286
+ run "rails runner 'Article.__elasticsearch__.create_index! force: true'"
287
+ rake "db:seed"
288
+
289
+ git add: "db/seeds.rb"
290
+ git commit: "-m 'Added the database seeding script'"
291
+
292
+ # ----- Print Git log -----------------------------------------------------------------------------
293
+
294
+ puts
295
+ say_status "Git", "Details about the application:", :yellow
296
+ puts '-'*80, ''
297
+
298
+ git tag: "basic"
299
+ git log: "--reverse --oneline"
300
+
301
+ # ----- Start the application ---------------------------------------------------------------------
302
+
303
+ require 'net/http'
304
+ if (begin; Net::HTTP.get(URI('http://localhost:3000')); rescue Errno::ECONNREFUSED; false; rescue Exception; true; end)
305
+ puts "\n"
306
+ say_status "ERROR", "Some other application is running on port 3000!\n", :red
307
+ puts '-'*80
308
+
309
+ port = ask("Please provide free port:", :bold)
310
+ else
311
+ port = '3000'
312
+ end
313
+
314
+ puts "", "="*80
315
+ say_status "DONE", "\e[1mStarting the application.\e[0m", :yellow
316
+ puts "="*80, ""
317
+
318
+ run "rails server --port=#{port}"