Class: Documentation::Searchers::Sunspot

Inherits:
Abstract
  • Object
show all
Defined in:
lib/documentation/searchers/sunspot.rb

Instance Method Summary collapse

Instance Method Details

#delete(page) ⇒ Object



21
22
23
# File 'lib/documentation/searchers/sunspot.rb', line 21

def delete(page)
  ::Sunspot.remove(page)
end

#index(page) ⇒ Object



17
18
19
# File 'lib/documentation/searchers/sunspot.rb', line 17

def index(page)
  ::Sunspot.index(page)
end

#resetObject



25
26
27
# File 'lib/documentation/searchers/sunspot.rb', line 25

def reset
  ::Sunspot.remove_all(Documentation::Page)
end

#search(query, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/documentation/searchers/sunspot.rb', line 29

def search(query, options = {})
  # Default options
  options[:page]      ||= 1
  options[:per_page]  ||= 15
  
  result = ::Sunspot.search(Documentation::Page) do |search|
    search.keywords query, :highlight => true
    search.paginate :page => options[:page], :per_page => options[:per_page]
  end
  
  search_result                 = Documentation::SearchResult.new
  search_result.page            = options[:page].to_i
  search_result.per_page        = options[:per_page].to_i
  search_result.total_results   = result.total
  search_result.query           = query
  search_result.time            = result.query_time
  search_result.raw_results     = result.hits.inject(Hash.new) do |hash, hit|
    hash[hit.primary_key.to_i] = {
      :score => hit.score,
      :highlights => hit.highlights(:content).collect{|x| x.format{|word| "{{{#{word}}}}"}}
    }
    hash
  end
  
  # Return it
  search_result
end

#setupObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/documentation/searchers/sunspot.rb', line 5

def setup
  ::Sunspot.setup(Documentation::Page) do
    text :title, :boost => 2.0
    text :content, :stored => true
    string :title, :stored => true
    string :permalink
    string :full_permalink
    time :created_at
    time :updated_at
  end
end