Permalink
Browse files

Use Elasticsearch search

  • Loading branch information...
rwdaigle committed Oct 5, 2012
1 parent 6d2d594 commit e8662e5b3183f299d31c7ac2f677010d27985f2e
Showing with 60 additions and 2 deletions.
  1. +4 −0 Gemfile
  2. +12 −0 Gemfile.lock
  3. +24 −1 app/models/gist.rb
  4. +5 −0 app/models/gist_fetcher.rb
  5. +10 −1 app/models/gist_file.rb
  6. +5 −0 config/initializers/tire.rb
View
@@ -11,6 +11,10 @@ gem 'rack-worker'
gem 'dalli'
gem 'octokit'
gem 'queue_classic'
+# gem 'indextank'
+# gem 'faraday-stack', '~> 0.1.5' # https://github.com/flaptor/indextank-rb/issues/8
+gem 'tire'
+gem 'yajl-ruby'
gem 'jquery-rails'
gem 'haml-rails'
View
@@ -48,6 +48,7 @@ GEM
haml (~> 3.1)
railties (>= 3.1, < 4.1)
hashie (1.2.0)
+ hashr (0.0.22)
hike (1.2.1)
httpauth (0.2.0)
i18n (0.6.1)
@@ -118,6 +119,8 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
sass (3.2.1)
sass-rails (3.2.5)
railties (~> 3.2.0)
@@ -134,13 +137,20 @@ GEM
rack (>= 1.0.0)
thor (0.16.0)
tilt (1.3.3)
+ tire (0.4.3)
+ activemodel (>= 3.0)
+ hashr (~> 0.0.19)
+ multi_json (~> 1.0)
+ rake
+ rest-client (~> 1.6)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.33)
uglifier (1.3.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
+ yajl-ruby (1.1.0)
PLATFORMS
ruby
@@ -158,4 +168,6 @@ DEPENDENCIES
rails (= 3.2.8)
sass-rails (~> 3.2.3)
thin
+ tire
uglifier (>= 1.0.3)
+ yajl-ruby
View
@@ -1,10 +1,18 @@
class Gist < ActiveRecord::Base
- attr_accessible :gh_id, :user_id, :description, :url, :git_pull_url, :git_push_url, :public, :comment_count, :gh_created_at, :gh_updated_at
+ include Tire::Model::Search
+ include Tire::Model::Callbacks
+
+ attr_accessible :gh_id, :user_id, :description, :url, :git_pull_url, :git_push_url, :public,
+ :comment_count, :gh_created_at, :gh_updated_at
belongs_to :user
has_many :files, :class_name => 'GistFile', :dependent => :delete_all
+ mapping do
+ indexes :description, :analyzer => 'whitespace', :boost => 10
+ end
+
class << self
def import(gh_gist)
@@ -27,4 +35,19 @@ def import(gh_gist)
end
end
end
+
+ # Required for Tire/Elasticsearch
+ def to_indexed_json
+ indexed_attributes.to_json
+ end
+
+ def indexed_attributes
+ {
+ description: description,
+ public: public?,
+ gh_created_at: gh_created_at,
+ gh_updated_at: gh_updated_at,
+ files: files.collect(&:indexed_attributes)
+ }
+ end
end
@@ -7,6 +7,7 @@ def fetch(user_id)
gh = gh_client(user)
fetch_gists(gh, user)
fetch_files(gh, user)
+ update_search_indices(user)
end
protected
@@ -25,6 +26,10 @@ def fetch_files(gh, user)
end
end
+ def update_search_indices(user)
+ user.gists.each { |gist| gist.update_index }
+ end
+
private
def gh_client(user)
View
@@ -27,4 +27,13 @@ def import(gh_gist)
end
end
end
-end
+
+ def indexed_attributes
+ {
+ name: filename,
+ content: content,
+ language: language,
+ type: file_type
+ }
+ end
+end
@@ -0,0 +1,5 @@
+require 'yajl/json_gem'
+
+Tire.configure do
+ url ENV['SEARCHBOX_URL']
+end

0 comments on commit e8662e5

Please sign in to comment.