allow searching stories by domain, link domain in story list to such a query

This commit is contained in:
joshua stein 2015-01-29 10:04:02 -06:00
parent 00d347c4f4
commit a16150d8a5
6 changed files with 48 additions and 5 deletions

View File

@ -516,6 +516,7 @@ li .domain {
color: #888;
font-style: italic;
font-size: 8.5pt;
text-decoration: none;
vertical-align: middle;
}

View File

@ -7,9 +7,13 @@ class SearchController < ApplicationController
if params[:q].to_s.present?
@search.q = params[:q].to_s
@search.what = params[:what]
@search.order = params[:order]
if params[:what].present?
@search.what = params[:what]
end
if params[:order].present?
@search.order = params[:order]
end
if params[:page].present?
@search.page = params[:page].to_i
end

View File

@ -11,6 +11,10 @@ ThinkingSphinx::Index.define :story, :with => :active_record do
has hotness, is_expired
has Story.score_sql, :as => :score, :type => :bigint, :sortable => true
# opts[:with] = { :id => ... } doesn't seem to work when sphinx searches on
# story_core, so give this column a different name to restrict on
has id, :as => :story_id
set_property :field_weights => {
:upvotes => 15,
:title => 10,

View File

@ -58,6 +58,30 @@ class Search
opts[:order] = "score DESC"
end
# extract domain query since it must be done separately
domain = nil
words = self.q.to_s.split(" ").reject{|w|
if m = w.match(/^domain:(.+)$/)
domain = m[1]
end
}.join(" ")
if domain.present?
self.what = "stories"
story_ids = Story.select(:id).where("`url` REGEXP '//([^/]*\.)?" +
ActiveRecord::Base.connection.quote_string(domain) + "/'").
collect(&:id)
if story_ids.any?
opts[:with] = { :story_id => story_ids }
else
self.results = []
self.total_results = 0
self.page = 0
return false
end
end
opts[:classes] = case what
when "all"
[ Story, Comment ]
@ -70,7 +94,7 @@ class Search
end
# escape sphinx special chars (using Riddle.escape removes boolean support)
query = self.q.gsub(/([\/~"])/, '\\\\\1')
query = words.gsub(/([\/~"])/, '\\\\\1')
# go go gadget search
self.results = []

View File

@ -227,6 +227,10 @@ class Story < ActiveRecord::Base
end
end
def domain_search_url
"/search?q=domain:#{self.domain}&order=newest"
end
def fetch_story_cache!
if self.url.present?
self.story_cache = StoryCacher.get_story_text(self.url)

View File

@ -29,7 +29,10 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
title="<%= tagging.tag.description %>"><%= tagging.tag.tag %></a>
<% end %>
</span>
<span class="domain"><%= story.domain %></span>
<% if story.domain.present? %>
<a class="domain" href="<%= story.domain_search_url %>"><%=
story.domain %></a>
<% end %>
<% if defined?(single_story) && single_story %>
<% story.merged_stories.each do |ms| %>
@ -45,7 +48,10 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
title="<%= tagging.tag.description %>"><%= tagging.tag.tag %></a>
<% end %>
</span>
<span class="domain"><%= ms.domain %></span>
<% if ms.domain.present? %>
<a class="domain" href="<%= ms.domain_search_url %>"><%=
ms.domain %></a>
<% end %>
<span class="byline">
by <a href="/u/<%= ms.user.username %>"
<% if ms.user.is_new? %>