Skip to content

Commit

Permalink
full search results page hooked up
Browse files Browse the repository at this point in the history
  • Loading branch information
schacon committed May 2, 2012
1 parent 77bb6ca commit 1435ea2
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 188 deletions.
5 changes: 1 addition & 4 deletions TODO.txt
Expand Up @@ -9,10 +9,6 @@ Initial Launch (3.0):
* Book content
* commands.html

* Search
* full search results
- include highlighting

* Sidebar related content
- ajaxify(?)
- related table
Expand Down Expand Up @@ -74,6 +70,7 @@ Initial Launch (3.0):
* add video shownotes to search
* add stackoverflow to search
* only search your locale plus english
* search internal pages, too (blog, etc)

* Videos
* viewing experience
Expand Down
37 changes: 30 additions & 7 deletions app/controllers/site_controller.rb
Expand Up @@ -10,21 +10,44 @@ def admin
end

def search
sname = params['search'].downcase
@term = sname = params['search'].to_s.downcase
@data = search_term(sname)
render :partial => 'shared/search'
end

def search_results
@term = sname = params['search'].to_s.downcase
data = search_term(sname, true)
@top = []
@rest = []
data[:results].each do |type|
type[:matches].each do |hit|
if hit[:score] >= 1.0
@top << hit
else
@rest << hit
end
end
end
@top.sort! { |a, b| b[:score] <=> a[:score] }
@rest.sort! { |a, b| b[:score] <=> a[:score] }
render "results"
end

@data = {
def search_term(sname, highlight = false)
data = {
:term => sname,
:results => []
}

if results = Doc.search(sname)
@data[:results] << results
if results = Doc.search(sname, highlight)
data[:results] << results
end

if results = Section.search(sname)
@data[:results] << results
if results = Section.search(sname, 'en', highlight)
data[:results] << results
end

render :partial => 'shared/search'
data
end
end
5 changes: 5 additions & 0 deletions app/helpers/site_helper.rb
@@ -1,2 +1,7 @@
module SiteHelper
def highlight_no_html(high)
strip_tags(high.to_s)
.gsub('[highlight]', '<span class="highlight">')
.gsub('[xhighlight]', '</span>')
end
end
20 changes: 15 additions & 5 deletions app/models/doc.rb
Expand Up @@ -8,7 +8,7 @@
class Doc < ActiveRecord::Base
has_many :doc_versions

def self.search(term)
def self.search(term, highlight = false)
query_options = {
"bool" => {
"should" => [
Expand All @@ -19,16 +19,26 @@ def self.search(term)
}
}

resp = BONSAI.search('doc',
'query' => query_options,
'size' => 10)
highlight_options = {
'pre_tags' => ['[highlight]'],
'post_tags' => ['[xhighlight]'],
'fields' => { 'text' => {"fragment_size" => 200} }
}

options = { 'query' => query_options,
'size' => 10 }
options['highlight'] = highlight_options

resp = BONSAI.search('doc', options)

ref_hits = []
resp['hits']['hits'].each do |hit|
highlight = hit.has_key?('highlight') ? hit['highlight']['text'].first : nil
name = hit["_source"]["name"]
ref_hits << {
:name => name,
#:meta => hit["_score"],
:score => hit["_score"],
:highlight => highlight,
:url => "/ref/#{name}"
}
end
Expand Down
22 changes: 18 additions & 4 deletions app/models/section.rb
Expand Up @@ -73,9 +73,12 @@ def index
nil # this is busted in production for some reason, which is really an issue
end

def self.search(term)
def self.search(term, lang = 'en', highlight = false)
query_options = {
"bool" => {
"must" => [
{ "term" => { "lang" => lang } }
],
"should" => [
{ "prefix" => { "section" => { "value" => term, "boost" => 12.0 } } },
{ "term" => { "html" => term } }
Expand All @@ -84,9 +87,17 @@ def self.search(term)
}
}

resp = BONSAI.search('book',
'query' => query_options,
'size' => 10)
highlight_options = {
'pre_tags' => ['[highlight]'],
'post_tags' => ['[xhighlight]'],
'fields' => { 'html' => {"fragment_size" => 200} }
}

options = { 'query' => query_options,
'size' => 10 }
options['highlight'] = highlight_options

resp = BONSAI.search('book', options)

ref_hits = []
resp['hits']['hits'].each do |hit|
Expand All @@ -95,9 +106,12 @@ def self.search(term)
slug = hit["_id"]
lang = hit["_source"]["lang"]
meta = "Chapter " + hit["_source"]['number'] + ' : ' + hit["_source"]["chapter"]
highlight = hit.has_key?('highlight') ? hit['highlight']['html'].first : nil
ref_hits << {
:name => name,
:meta => meta,
:score => hit["_score"],
:highlight => highlight,
:url => "/book/#{lang}/#{slug}"
}
end
Expand Down
5 changes: 4 additions & 1 deletion app/views/layouts/layout.html.haml
@@ -1,7 +1,10 @@
!!! 5
%html{:lang => "en"}
%head
%title Git
- if @page_title
%title= @page_title
- else
%title Git
%meta{:charset => "utf-8"}
%meta{:content => "IE=edge,chrome=1", "http-equiv" => "X-UA-Compatible"}
= stylesheet_link_tag "git-scm.css"
Expand Down
31 changes: 31 additions & 0 deletions app/views/shared/_related.html.haml
@@ -0,0 +1,31 @@
%h4 Related StackOverflow Questions

%ul.stackoverflow
%li
=link_to "About Git's merge and rebase"
%span
<strong>5</strong> votes <em>/</em> <strong>3</strong> Answers
%li
=link_to "git rebase vs git merge"
%span
<strong>81</strong> votes <em>/</em> <strong>5</strong> Answers
%li
=link_to "How to do a rebase with git gui?"
%span
<strong>5</strong> votes <em>/</em> <strong>3</strong> Answers
%li
=link_to "recovering from git rebase"
%span
<strong>27</strong> votes <em>/</em> <strong>15</strong> Answers
%li
=link_to "git pull VS git fetch git rebase"
%span
<strong>15</strong> votes <em>/</em> <strong>3</strong> Answers
%li
=link_to "Undoing a git rebase"
%span
<strong>160</strong> votes <em>/</em> <strong>7</strong> Answers
%li
=link_to "How to know if there is a git rebase in progress?"
%span
<strong>16</strong> votes <em>/</em> <strong>7</strong> Answers
2 changes: 1 addition & 1 deletion app/views/shared/_search.html.haml
Expand Up @@ -5,7 +5,7 @@
%td.matches
%ul
%li
<a href="/search/results">Show all results...</a>
%a{:href=>"/search/results?search=#{@term}"} Show all results...
- @data[:results].each do |cat|
%tr
%td.category
Expand Down

0 comments on commit 1435ea2

Please sign in to comment.