lib/picolena/templates/app/models/finder.rb in picolena-0.1.5 vs lib/picolena/templates/app/models/finder.rb in picolena-0.1.6
- old
+ new
@@ -1,21 +1,21 @@
-class Finder
+class Finder
attr_reader :query
-
+
def index
@@index ||= Indexer.index
end
-
+
def initialize(raw_query,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
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|
@@ -25,26 +25,25 @@
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
- found_doc.index_id=index_id
@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
end
-
+
# Returns true if it has been executed.
def executed?
@executed
end
-
+
# To ensure that
# matching_documents
# total_hits
# time_needed
# methods are called only after the index has been searched.
@@ -52,42 +51,18 @@
define_method(attribute_name){
execute! unless executed?
instance_variable_get("@#{attribute_name}")
}
}
-
- # Returns matching document for any given query only if
- # exactly one document is found.
- # Raises otherwise.
- def matching_document
- case matching_documents.size
- when 0
- raise IndexError, "No document found"
- when 1
- matching_documents.first
- else
- raise IndexError, "More than one document found"
- end
- end
-
- class<<self
- def searcher
- @@searcher ||= Ferret::Search::Searcher.new(Picolena::IndexSavePath)
- end
-
- def term_search(field,term)
- query = Ferret::Search::TermQuery.new(field,term)
- searcher.search(query).hits.first
- end
- def reload!
- @@searcher = nil
- @@index = nil
- end
+
+
+ def self.reload!
+ @@index = nil
end
-
+
private
-
+
def index_should_have_documents
raise IndexError, "no document found" unless index.size > 0
end
end
\ No newline at end of file