lib/picolena/templates/app/models/finder.rb in picolena-0.1.6 vs lib/picolena/templates/app/models/finder.rb in picolena-0.1.7
- old
+ new
@@ -3,40 +3,38 @@
def index
@@index ||= Indexer.index
end
- def initialize(raw_query,page=1,results_per_page=Picolena::ResultsPerPage)
+ def initialize(raw_query,by_date=false, page=1,results_per_page=Picolena::ResultsPerPage)
@query = Query.extract_from(raw_query)
@raw_query= raw_query
Indexer.ensure_index_existence
@per_page=results_per_page
@offset=(page.to_i-1)*results_per_page
+ @by_date=by_date
index_should_have_documents
end
def execute!
@matching_documents=[]
start=Time.now
- top_docs=index.search(query, :limit => @per_page, :offset=>@offset)
- top_docs.hits.each{|hit|
- index_id,score=hit.doc,hit.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 => ">>"
- ) unless @raw_query=~/^\*+\.\w*$/
- found_doc.score=score
- @matching_documents<<found_doc
- rescue Errno::ENOENT
- #"File has been moved/deleted!"
- end
+ @total_hits = index.search_each(query, :limit => @per_page, :offset=>@offset, :sort => (sort_by_date if @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
- @total_hits=top_docs.total_hits
+ @time_needed=Time.now-start
end
# Returns true if it has been executed.
def executed?
@executed
@@ -52,16 +50,18 @@
execute! unless executed?
instance_variable_get("@#{attribute_name}")
}
}
-
-
def self.reload!
@@index = nil
end
private
+
+ def sort_by_date
+ Ferret::Search::SortField.new(:modified, :type => :byte, :reverse => true)
+ end
def index_should_have_documents
raise IndexError, "no document found" unless index.size > 0
end
end
\ No newline at end of file