Class: Nesta::Plugin::Search::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/nesta-plugin-search/init.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(imethod, timeout = false) ⇒ Index

Currently imethod is expected to be:

  • :find_all

  • :find_articles

Initializing with a timeout = false will cause index to not expire without a server restart.



51
52
53
54
55
56
57
# File 'lib/nesta-plugin-search/init.rb', line 51

def initialize(imethod, timeout=false) # 12 hours default
  @index_method = imethod.to_sym
  if timeout
    @timeout = timeout
    @expire_at = Time.now+timeout
  end
end

Instance Attribute Details

#index_methodObject

Returns the value of attribute index_method.



42
43
44
# File 'lib/nesta-plugin-search/init.rb', line 42

def index_method
  @index_method
end

Instance Method Details

#indexObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/nesta-plugin-search/init.rb', line 59

def index
  # index and return if index doesn't exist or is empty
  return @index = reindex unless @index && @index.size > 0

  # return existing index if current index isn't expired or expired_at isn't set
  return @index if @expire_at && @expire_at > Time.now

  # if all else fails, index and return
  return @index = reindex
end

#reindexObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/nesta-plugin-search/init.rb', line 70

def reindex
  @index = Ferret::Index::Index.new

  Nesta::Page.send(@index_method).each do |item|
    unless Nesta::Config.search_ignore_list.include?(item.abspath)
      @index << {:heading => item.heading, :href => item.abspath, :summary => item.summary, :body => item.body}
    end
  end
  @index
end