Class: Treat::Workers::Retrievers::Searchers::Ferret

Inherits:
Object
  • Object
show all
Defined in:
lib/treat/workers/retrievers/searchers/ferret.rb

Overview

A simple interface to the Ferret information retrieval library, which performs full-text search within documents of a collection.

Documentation: rubydoc.info/gems/ferret

Constant Summary collapse

DefaultOptions =
{
  :query => nil,
  :limit => :all,
  :callback => nil
}

Class Method Summary collapse

Class Method Details

.search(collection, options = {}) ⇒ Object

Returns an array of retrieved documents.

Options:

  • (String) :q => a search query.

  • (Symbol) :limit => number of documents.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/treat/workers/retrievers/searchers/ferret.rb', line 24

def self.search(collection, options = {})
  
  options = DefaultOptions.merge(options)
  
  unless collection.has?(:index)
    raise Treat::Exception,
    "This collection must be indexed to be searchable."
  end
  
  unless options[:query]
    raise Treat::Exception,
    'You must set a query by using the :q option.'
  end
  
  path = collection.index
  
  unless FileTest.directory?(path)
    raise Treat::Exception,
    "The index at location #{path} cannot be found."
  end

  index = ::Ferret::Index::Index.new(
    :default_field => 'content',
    :path => path
  )
  
  query = options.delete(:q)
  files = {}
  index.search_each(query, options) do |doc, score|
    files[index[doc]['file']] = score
  end
  
  docs = []
  files.each do |doc, score|
    doc2 = collection.document_with_file(doc)
    unless doc2
      raise Treat::Exception,
      "Couldn't retrieve indexed " +
      "document with filename #{doc}."
    end
    if options[:callback]
      options[:callback].call(doc2, score)
    end
    docs << doc2
  end
  
  docs
end