Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Display search results page

  • Loading branch information...
commit e25c6bd20a0464c08a0f932036c0081d3bd8dee3 1 parent 71481de
@rwdaigle authored
View
4 app/controllers/gists_controller.rb
@@ -8,6 +8,10 @@ def index
@gists = current_user.gists.includes(:files)
end
+ def search
+ @results = Gist.search params[:q]
+ end
+
def refresh
QC.enqueue("GistFetcher.fetch", current_user.id)
redirect_to gists_path
View
23 app/models/gist.rb
@@ -9,7 +9,14 @@ class Gist < ActiveRecord::Base
has_many :files, :class_name => 'GistFile', :dependent => :delete_all
mapping do
- indexes :description, :analyzer => 'whitespace', :boost => 10
+ indexes :description, :analyzer => 'snowball', :boost => 10
+ indexes :gh_created_at, type: 'date'
+ indexes :files do
+ indexes :filename, analyzer: 'keyword'
+ indexes :content, analyzer: 'snowball'
+ indexes :language, analyzer: 'keyword'
+ indexes :file_type, analyzer: 'keyword'
+ end
end
class << self
@@ -33,6 +40,19 @@ def import(gh_gist)
create(attributes)
end
end
+
+ def search(q)
+ tire.search do
+ query { string q }
+ sort { by :gh_created_at, 'desc' }
+ highlight :description, :'files.content'
+ end
+ end
+
+ def reindex
+ find_each { |gist| gist.update_index }
+ tire.index.refresh
+ end
end
# Required for Tire/Elasticsearch
@@ -43,6 +63,7 @@ def to_indexed_json
def indexed_attributes
{
description: description,
+ url: url,
public: public?,
gh_created_at: gh_created_at,
gh_updated_at: gh_updated_at,
View
7 app/models/gist_file.rb
@@ -2,7 +2,7 @@ class GistFile < ActiveRecord::Base
attr_accessible :gist_id, :filename, :raw_url, :language, :file_type, :content, :size_bytes
- belongs_to :gist
+ belongs_to :gist, :touch => true
class << self
@@ -30,10 +30,11 @@ def import(gh_gist)
def indexed_attributes
{
- name: filename,
+ filename: filename,
content: content,
language: language,
- type: file_type
+ file_type: file_type,
+ size_bytes: size_bytes
}
end
end
View
10 app/views/gists/search.html.haml
@@ -0,0 +1,10 @@
+= "Search for \"#{params[:q]}\""
+
+%ul
+ - for result in @results
+ %li
+ %p= link_to (result.highlight.description ? result.highlight['description'].first.html_safe : result.description), result.url
+ - if(result.highlight[:'files.content'])
+ - result.highlight[:'files.content'].each do |highlight|
+ %p
+ %code= highlight.html_safe
View
1  config/initializers/tire.rb
@@ -2,4 +2,5 @@
Tire.configure do
url ENV['SEARCHBOX_URL']
+ logger STDOUT
end
View
1  config/routes.rb
@@ -10,6 +10,7 @@
resources :gists do
collection do
post 'refresh'
+ get 'search'
end
end
View
15 lib/tasks/search.rake
@@ -0,0 +1,15 @@
+namespace :search do
+
+ # Reindex all gists
+ task :reindex => :environment do
+ Gist.reindex
+ end
+
+ # Delete existing gist index, create new one and reindex all gists
+ task :rebuild => :environment do
+ Gist.tire.index.delete
+ Gist.tire.index.create
+ Gist.reindex
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.