diff --git a/Gemfile b/Gemfile index 5b35b33..64d62a3 100644 --- a/Gemfile +++ b/Gemfile @@ -31,7 +31,7 @@ gem "rdiscount" gem "oauth" -gem "thinking-sphinx", "2.0.12" +gem "thinking-sphinx", "~> 3.0.6" gem "mail" diff --git a/Gemfile.lock b/Gemfile.lock index da84eca..8fecf91 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -38,6 +38,7 @@ GEM hike (1.2.3) htmlentities (4.3.1) i18n (0.6.9) + innertube (1.1.0) jquery-rails (3.0.4) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) @@ -47,6 +48,7 @@ GEM mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) + middleware (0.1.0) mime-types (1.25.1) mini_portile (0.5.2) minitest (4.7.5) @@ -99,10 +101,12 @@ GEM activesupport (>= 3.0) sprockets (~> 2.8) sqlite3 (1.3.8) - thinking-sphinx (2.0.12) - activerecord (>= 3.0.3) + thinking-sphinx (3.0.6) + activerecord (>= 3.1.0) builder (>= 2.1.2) - riddle (>= 1.5.2) + innertube (>= 1.0.2) + middleware (>= 0.1.0) + riddle (>= 1.5.9) thor (0.18.1) thread_safe (0.1.3) atomic @@ -138,6 +142,6 @@ DEPENDENCIES rdiscount rspec-rails (~> 2.6) sqlite3 - thinking-sphinx (= 2.0.12) + thinking-sphinx (~> 3.0.6) uglifier (>= 1.3.0) unicorn diff --git a/README.md b/README.md index 1976c94..7b827d6 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ generated key from the output of `rake secret`: * (Optional, only needed for the search engine) Install Sphinx. Build Sphinx config and start server: - lobsters$ rake thinking_sphinx:rebuild + lobsters$ rake ts:rebuild * Define your site's name and default domain, which are used in various places, in a `config/initializers/production.rb` or similar file: diff --git a/app/indices/comment_index.rb b/app/indices/comment_index.rb new file mode 100644 index 0000000..b246a2b --- /dev/null +++ b/app/indices/comment_index.rb @@ -0,0 +1,13 @@ + +ThinkingSphinx::Index.define :comment, :with => :active_record do + indexes comment + indexes user.username, :as => :author + + has "(cast(upvotes as signed) - cast(downvotes as signed))", + :as => :score, :type => :bigint, :sortable => true + + has is_deleted + has created_at + + where sanitize_sql(:is_deleted => false, :is_moderated => false) +end diff --git a/app/indices/story_index.rb b/app/indices/story_index.rb new file mode 100644 index 0000000..034838f --- /dev/null +++ b/app/indices/story_index.rb @@ -0,0 +1,21 @@ + +ThinkingSphinx::Index.define :story, :with => :active_record do + indexes url + indexes title + indexes description + indexes user.username, :as => :author + indexes tags(:tag), :as => :tags + + has created_at, :sortable => true + has hotness, is_expired + has "(cast(upvotes as signed) - cast(downvotes as signed))", + :as => :score, :type => :bigint, :sortable => true + + set_property :field_weights => { + :upvotes => 15, + :title => 10, + :tags => 5, + } + + where sanitize_sql(:is_expired => false) +end diff --git a/app/models/comment.rb b/app/models/comment.rb index 01311b4..826e86a 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -24,19 +24,6 @@ class Comment < ActiveRecord::Base MAX_EDIT_MINS = 45 - define_index do - indexes comment - indexes user.username, :as => :author - - has "(cast(upvotes as signed) - cast(downvotes as signed))", - :as => :score, :type => :bigint, :sortable => true - - has is_deleted - has created_at - - where "is_deleted = 0 AND is_moderated = 0" - end - validate do self.comment.to_s.strip == "" && errors.add(:comment, "cannot be blank.") diff --git a/app/models/search.rb b/app/models/search.rb index 87e4ecf..5192d4c 100644 --- a/app/models/search.rb +++ b/app/models/search.rb @@ -36,30 +36,28 @@ class Search def search_for_user!(user) opts = { - :match_mode => :extended, - :rank_mode => :bm25, - :page => @page, + :ranker => :bm25, + :page => @page, :per_page => @per_page, + :include => [ :story, :user ], } if order == "newest" - opts[:order] = :created_at - opts[:sort_mode] = :desc + opts[:order] = "created_at DESC" elsif order == "points" - opts[:order] = :score - opts[:sort_mode] = :desc + opts[:order] = "score DESC" end - opts[:classes] = [] - if what == "all" - opts[:classes] = [ Story, Comment ] - elsif what == "comments" - opts[:classes] = [ Comment ] - elsif what == "stories" - opts[:classes] = [ Story ] - end - - opts[:include] = [ :story, :user ] + opts[:classes] = case what + when "all" + [ Story, Comment ] + when what == "comments" + [ Comment ] + when what == "stories" + [ Story ] + else + [] + end # sphinx seems to interpret slashes as a regex(?) so escape them since # nobody is probably using them, but don't just use Riddle.escape because diff --git a/app/models/story.rb b/app/models/story.rb index bc6b031..79d2a31 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -23,27 +23,6 @@ class Story < ActiveRecord::Base before_save :log_moderation after_create :mark_submitter - define_index do - indexes url - indexes title - indexes description - indexes user.username, :as => :author - indexes tags(:tag), :as => :tags - - has created_at, :sortable => true - has hotness, is_expired - has "(cast(upvotes as signed) - cast(downvotes as signed))", - :as => :score, :type => :bigint, :sortable => true - - set_property :field_weights => { - :upvotes => 15, - :title => 10, - :tags => 5, - } - - where "is_expired = 0" - end - validate do if self.url.present? # URI.parse is not very lenient, so we can't use it diff --git a/config/sphinx.yml b/config/thinking_shinx.yml similarity index 100% rename from config/sphinx.yml rename to config/thinking_shinx.yml diff --git a/script/start_searchd b/script/start_searchd index 2d2fb4c..8b5a1c5 100755 --- a/script/start_searchd +++ b/script/start_searchd @@ -2,4 +2,4 @@ cd /d/rails/lobsters # rebuild does a start -sudo -u lobsters -H env RAILS_ENV=production NO_TRIPWIRE=1 bundle19 exec rake19 thinking_sphinx:rebuild +sudo -u lobsters -H env RAILS_ENV=production NO_TRIPWIRE=1 bundle19 exec rake19 ts:rebuild