Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add owner filter w/ @rwdaigle syntax

  • Loading branch information...
commit 4f685700609ed976e3cafbe0fc737da1bd294d7e 1 parent e6ddbf4
@rwdaigle authored
View
43 app/models/gist.rb
@@ -16,13 +16,13 @@ class Gist < ActiveRecord::Base
index_name ELASTICSEARCH_INDEX_NAME
mapping do
- indexes :description, :analyzer => 'snowball', :boost => 10
- indexes :gh_created_at, type: 'date'
+ indexes :description, :analyzer => 'snowball', :boost => 100
+ indexes :gh_updated_at, type: 'date'
indexes :user_id, :analyzer => :not_analyzed
indexes :owner_gh_username, analyzer: 'keyword'
indexes :starred, :analyzer => :not_analyzed
indexes :files do
- indexes :filename, analyzer: 'standard', :boost => 5
+ indexes :filename, analyzer: 'standard', :boost => 10
indexes :content, analyzer: 'snowball'
indexes :language, analyzer: 'keyword'
indexes :file_type, analyzer: 'standard'
@@ -62,13 +62,17 @@ def search(user, q)
if(!q.blank?)
log({ns: self, fn: __method__, query: q, measure: true}, user) do
begin
+
+ # Preprocess
+ query_params = preprocess(user, q)
+
tire.search :load => true do
- query { string q }
- fields [:description, :url, :public, :gh_updated_at, :id, :comment_count, :'files.filename', :'files.language']
- # sort { by :gh_created_at, 'desc' }
- filter :term, :user_id => user.id
- highlight :description, :options => { :tag => "<em>" }
- size 15
+ query { string query_params[:query] }
+ fields query_params[:fields]
+ query_params[:filter].each do |field, value|
+ filter :term, field => value
+ end
+ size query_params[:size]
end
rescue Tire::Search::SearchRequestFailed => e
if(e.message.include?("SearchParseException")) # Swallow malformed queries
@@ -87,6 +91,24 @@ def search(user, q)
results
end
+ def preprocess(user, q)
+
+ # Standard stuff
+ query = {}
+ query[:fields] = [:description, :'files.filename', :'files.content']
+ query[:filter] = {:user_id => user.id}
+ query[:size] = 15
+
+ # Extract "@jonmountjoy" owner syntax from queries
+ if(owner_match = %r{@(\w+)}.match(q))
+ query[:filter][:owner_gh_username] = owner = owner_match[1]
+ q = q.sub("@#{owner}", "")
+ end
+
+ query[:query] = q.blank? ? "*" : q
+ query
+ end
+
def reindex(gists = scoped)
log({ns: self, fn: __method__}) do
gists.find_in_batches(batch_size: ENV['ELASTICSEARCH_IMPORT_BATCH_SIZE'].to_i) do |gist_batch|
@@ -124,12 +146,9 @@ def indexed_attributes
{
user_id: user_id,
owner_gh_username: owner_gh_username,
- owner_gh_avatar_url: owner_gh_avatar_url,
description: description,
- url: url,
public: public?,
starred: starred?,
- gh_created_at: gh_created_at,
gh_updated_at: gh_updated_at,
comment_count: comment_count,
files: files.collect(&:indexed_attributes)
View
8 app/models/user.rb
@@ -27,16 +27,16 @@ def authenticate(auth)
end
end
- def refresh_indexes
+ def refresh_indexes(all = false)
User.active_auth.pluck(:id).each do |user_id|
- refresh_index(user_id)
+ refresh_index(user_id, all)
end
end
- def refresh_index(user_id)
+ def refresh_index(user_id, all = false)
user = User.find(user_id)
log({ns: self, fn: __method__}, user) do
- Gist.reindex(user.gists.since(user.last_indexed_at))
+ Gist.reindex(all ? user.gists : user.gists.since(user.last_indexed_at))
user.indexed!
end
end
View
2  app/views/gists/search.html.haml
@@ -4,7 +4,7 @@
.topsearch.command-bar-activated
= form_tag(search_gists_path, :method => "get", :id => "top_search_form", :class => "command_bar_form") do
.commandbar.focused
- = text_field_tag(:q, params[:q], :size => 50, :class => "mousetrap", :id => "command-bar", :placeholder => "Search your gists: this AND that \"this phrase\" -excluding +including", :tabindex => "1", :autocomplete => "off")
+ = text_field_tag(:q, params[:q], :size => 50, :class => "mousetrap", :id => "command-bar", :placeholder => "Search your gists: this AND that \"this phrase\" -excluding +including by @githubUser", :tabindex => "1", :autocomplete => "off")
#results.display(style="display: #{!params[:q].blank? ? "block" : "none"};")
= render :partial => 'results'
#search-error.display
View
2  lib/tasks/gisted.rake
@@ -10,6 +10,6 @@ namespace :gisted do
Gist.tire.index.delete
Gist.tire.index.create
GistFetcher.fetch
- QC.enqueue("User.refresh_indexes")
+ QC.enqueue("User.refresh_indexes", true)
end
end
View
4 lib/tasks/search.rake
@@ -2,14 +2,14 @@ namespace :search do
# Reindex all gists
task :reindex => :environment do
- User.refresh_indexes
+ User.refresh_indexes(true)
end
# Delete existing gist index, create new one and reindex all gists
task :rebuild => :environment do
Gist.tire.index.delete
Gist.tire.index.create
- User.refresh_indexes
+ User.refresh_indexes(true)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.