Class: Finder
Overview
Finder is the class that is in charge to ensure that an index is instantiated and that the raw query is converted into a Ferret::Query. It then launches a search and returns found documents with corresponding matching scores.
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#execute! ⇒ Object
Launches the search and writes correspondings @matching_documents @total_hits @time_needed.
-
#executed? ⇒ Boolean
Returns true if it has been executed.
-
#index ⇒ Object
No need to instantiate a new index for every search, so it gets cached.
-
#initialize(raw_query, sort_by = 'relevance', page = 1, results_per_page = Picolena::ResultsPerPage) ⇒ Finder
constructor
Instantiates a new Finder extracts a Ferret::Query from plain text raw query ensures that an Index has been instantiated reloads the index if needed prepares matching_documents pagination and ensures that the index contains at least one document.
Constructor Details
#initialize(raw_query, sort_by = 'relevance', page = 1, results_per_page = Picolena::ResultsPerPage) ⇒ Finder
Instantiates a new Finder extracts a Ferret::Query from plain text raw query ensures that an Index has been instantiated reloads the index if needed prepares matching_documents pagination and ensures that the index contains at least one document.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/picolena/templates/app/models/finder.rb', line 18 def initialize(raw_query,sort_by='relevance', page=1,results_per_page=Picolena::ResultsPerPage) @query = Query.extract_from(raw_query) @raw_query= raw_query Indexer.ensure_index_existence reload_index! if should_be_reloaded? @per_page=results_per_page @offset=(page.to_i-1)*results_per_page @sort_by=sort_by index_should_have_documents end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
5 6 7 |
# File 'lib/picolena/templates/app/models/finder.rb', line 5 def query @query end |
Instance Method Details
#execute! ⇒ Object
Launches the search and writes correspondings
@matching_documents
@total_hits
@time_needed
If a document is found in the index but not on the filesystem, it doesn’t appear on the @matching_documents list.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/picolena/templates/app/models/finder.rb', line 36 def execute! @matching_documents=[] start=Time.now @total_hits = index.search_each(query, :limit => @per_page, :offset=>@offset, :sort => (sort_by_date if @sort_by=='date')){|index_id, score| begin found_doc=Document.new(index[index_id][:complete_path]) found_doc.matching_content=index.highlight(query, index_id, :field => :content, :excerpt_length => 80, :pre_tag => "<<", :post_tag => ">>" ) found_doc.score=score @matching_documents<<found_doc rescue Errno::ENOENT #"File has been moved/deleted!" end } @executed=true @time_needed=Time.now-start end |
#executed? ⇒ Boolean
Returns true if it has been executed.
57 58 59 |
# File 'lib/picolena/templates/app/models/finder.rb', line 57 def executed? @executed end |