Class: Chef::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/search.rb,
lib/chef/search/result.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearch

Returns a new instance of Search.



27
28
29
# File 'lib/chef/search.rb', line 27

def initialize
  @index = Ferret::Index::Index.new(:path => Chef::Config[:search_index_path])
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



25
26
27
# File 'lib/chef/search.rb', line 25

def index
  @index
end

Instance Method Details

#has_index?(index) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/chef/search.rb', line 70

def has_index?(index)
  list_indexes.detect { |i| i == index }
end

#list_indexesObject



62
63
64
65
66
67
68
# File 'lib/chef/search.rb', line 62

def list_indexes
  indexes = Hash.new
  @index.search_each("index_name:*", :limit => :all) do |id, score|
    indexes[@index.doc(id)["index_name"]] = true
  end
  indexes.keys
end

#search(type, query = "*", attributes = [], &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chef/search.rb', line 31

def search(type, query="*", attributes=[], &block)
  search_query = build_search_query(type, query)
  start_time = Time.now
  results = []
  block ||= lambda { |b| b }
  
  @index.search_each(search_query, :limit => :all) do |id, score|
    results << block.call(build_hash(@index.doc(id)))
  end
  
  Chef::Log.debug("Search #{search_query} complete in #{Time.now - start_time} seconds")
  
  attributes.empty? ? results : filter_by_attributes(results,attributes)
end