Class: Ferret::Search::TopDocs

Inherits:
Object
  • Object
show all
Defined in:
lib/ferret/search/top_docs.rb

Overview

Expert: Returned by low-level search implementations. See Searcher#search

Direct Known Subclasses

TopFieldDocs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_hits, score_docs, fields = SortField::FIELD_SCORE) ⇒ TopDocs

Expert: Constructs a TopDocs.



22
23
24
25
26
# File 'lib/ferret/search/top_docs.rb', line 22

def initialize(total_hits, score_docs, fields = SortField::FIELD_SCORE) 
  @total_hits = total_hits
  @score_docs = score_docs
  @fields = fields
end

Instance Attribute Details

#fieldsObject

Expert: The total number of hits for the query. See Hits#length()



7
8
9
# File 'lib/ferret/search/top_docs.rb', line 7

def fields
  @fields
end

#score_docsObject

Expert: The total number of hits for the query. See Hits#length()



7
8
9
# File 'lib/ferret/search/top_docs.rb', line 7

def score_docs
  @score_docs
end

#total_hitsObject Also known as: size

Expert: The total number of hits for the query. See Hits#length()



7
8
9
# File 'lib/ferret/search/top_docs.rb', line 7

def total_hits
  @total_hits
end

Instance Method Details

#eachObject

iterate through each of the score docs, yielding the document number and the score. eg:

top_docs.each do |doc, score|
  puts "Doc number #{doc} found with score of #{score}"}
end


17
18
19
# File 'lib/ferret/search/top_docs.rb', line 17

def each
  score_docs.each {|sd| yield(sd.doc, sd.score) }
end

#to_sObject



28
29
30
31
32
33
34
# File 'lib/ferret/search/top_docs.rb', line 28

def to_s
  buffer = "#{total_hits} hits sorted by <"
  buffer << [fields].flatten.map {|field| "#{@field}" }.join(", ")
  buffer << ">:\n"
  score_docs.each {|sd| buffer << "\t#{sd}\n" }
  return buffer
end