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
|