Class: Treat::Workers::Retrievers::Indexers::Ferret

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

Overview

A wrapper for the indexing functions of Ferret, a port of the Java Lucene search engine.

Documentation: rubydoc.info/gems/ferret

Class Method Summary collapse

Class Method Details

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

Create a Ferret index for the collection and store the index in the collection, under the path collection-folder/.index

Annotates the collection with the path to the index for future use (e.g. in searching).



19
20
21
22
23
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
# File 'lib/treat/workers/retrievers/indexers/ferret.rb', line 19

def self.index(collection, options = {})

  unless collection.get(:folder)
    raise Treat::Exception,
    "Only collections stored on disk " +
    "can currently be indexed with Ferret."
  end
  
  path = "#{collection.folder}/.index"
  return path if FileTest.directory?(path)
  
  begin
    FileUtils.mkdir(path)
  rescue Exception => e
    raise Treat::Exception,
    "Could not create folder for index " +
    "under the collection's folder. " +
    "(#{e.message})."
  end
  
  index = ::Ferret::Index::Index.new(
    :default_field => 'content',
    :path => path
  )
  
  collection.each_document do |doc|
    index.add_document(
      :file => doc.file,
      :content => doc.to_s
    )
  end
  
  path
end