Class: ActiveLucene::Index::Searcher

Inherits:
IndexSearcher
  • Object
show all
Defined in:
lib/active_lucene/index/searcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearcher

Returns a new instance of Searcher.



8
9
10
# File 'lib/active_lucene/index/searcher.rb', line 8

def initialize
  super Index.directory, true
end

Class Method Details

.search(param, opts = {}) ⇒ Object



4
5
6
# File 'lib/active_lucene/index/searcher.rb', line 4

def self.search(param, opts = {})
  new.search param, opts
end

Instance Method Details

#search(param, opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_lucene/index/searcher.rb', line 12

def search(param, opts)
  page = (opts[:page] || 1).to_i
  query = Query.for(param)
  highlighter = Highlighter.new(QueryScorer.new(query))
  top_docs = super(query, nil, page * Document::PER_PAGE)
  score_docs = top_docs.scoreDocs 
  returning SearchResult.new(param) do |search_result|
    score_docs[(page - 1) * Document::PER_PAGE ... score_docs.size].each do |score_doc|
      attributes = {}
      doc(score_doc.doc).fields.each do |field|
        attributes.store field.name, field.string_value
        highlight = highlighter.get_best_fragment(Analyzer.new, ALL, field.string_value)
        attributes[:highlight] = highlight if highlight
      end
      search_result.add_document attributes
    end
    search_result.current_page = page
    search_result.total_pages = (top_docs.totalHits / Document::PER_PAGE.to_f).ceil
  end
end