2
# Author:: Adam Jacob (<adam@opscode.com>)
3
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4
# License:: Apache License, Version 2.0
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
19
require 'chef/search/result'
28
@index = Ferret::Index::Index.new(:path => Chef::Config[:search_index_path])
31
def search(type, query="*", attributes=[], &block)
32
search_query = build_search_query(type, query)
35
block ||= lambda { |b| b }
37
@index.search_each(search_query, :limit => :all) do |id, score|
38
results << block.call(build_hash(@index.doc(id)))
41
Chef::Log.debug("Search #{search_query} complete in #{Time.now - start_time} seconds")
43
attributes.empty? ? results : filter_by_attributes(results,attributes)
46
def filter_by_attributes(results, attributes)
47
results.collect do |r|
49
nr[:index_name] = r[:index_name]
51
attributes.each do |attrib|
53
nr[attrib] = r[attrib]
60
private :filter_by_attributes
64
@index.search_each("index_name:*", :limit => :all) do |id, score|
65
indexes[@index.doc(id)["index_name"]] = true
71
list_indexes.detect { |i| i == index }
75
def build_search_query(type, query)
76
query = "id:*" if query == '*'
77
"index_name:#{type} AND (#{query})"
81
result = Chef::Search::Result.new
82
doc.fields.each do |f|