diff options
author | Christopher Brown <cb@opscode.com> | 2009-02-06 19:56:32 -0800 |
---|---|---|
committer | Christopher Brown <cb@opscode.com> | 2009-02-06 19:56:32 -0800 |
commit | 3d8781598f450ecff40558d9f69cfbdfad9a49bf (patch) | |
tree | 5f52ce7d7142e49264a388e4fc4c4a3d4f28b4f5 /chef-server | |
parent | 220bb4b2c4f5d20aec95101ecab86758346dd534 (diff) | |
download | chef-3d8781598f450ecff40558d9f69cfbdfad9a49bf.tar.gz |
CHEF-106 refactor for attributes in search method (and out of controller)
Diffstat (limited to 'chef-server')
-rw-r--r-- | chef-server/lib/chef/search.rb | 31 | ||||
-rw-r--r-- | chef-server/lib/controllers/search.rb | 30 |
2 files changed, 26 insertions, 35 deletions
diff --git a/chef-server/lib/chef/search.rb b/chef-server/lib/chef/search.rb index 131bad7a3e..7318d69b0b 100644 --- a/chef-server/lib/chef/search.rb +++ b/chef-server/lib/chef/search.rb @@ -27,22 +27,31 @@ class Chef @index = Ferret::Index::Index.new(:path => Chef::Config[:search_index_path]) end - def search(type, query, &block) + def search(type, query, attributes, &block) search_query = build_search_query(type, query) start_time = Time.now - result = Array.new - if Kernel.block_given? - result = @index.search_each(search_query, :limit => :all) do |id, score| - block.call(build_hash(@index.doc(id))) - end - else - @index.search_each(search_query, :limit => :all) do |id, score| - result << build_hash(@index.doc(id)) - end + results = @index.search_each(search_query, :limit => :all) do |id, score| + q = build_hash(@index.doc(id)) + Kernel.block_given? ? block.call(q) : [q] end + Chef::Log.debug("Search #{search_query} complete in #{Time.now - start_time} seconds") - result + + unless attributes.empty? + results = results.collect do |r| + nr = Hash.new + nr[:index_name] = r[:index_name] + nr[:id] = r[:id] + attributes.each do |attrib| + if r.has_key?(attrib) + nr[attrib] = r[attrib] + end + end + nr + end + end + results end def list_indexes diff --git a/chef-server/lib/controllers/search.rb b/chef-server/lib/controllers/search.rb index 404ebb26ee..2e7be63bbd 100644 --- a/chef-server/lib/controllers/search.rb +++ b/chef-server/lib/controllers/search.rb @@ -1,4 +1,4 @@ -# + # # Author:: Adam Jacob (<adam@opscode.com>) # Copyright:: Copyright (c) 2008 Opscode, Inc. # License:: Apache License, Version 2.0 @@ -31,29 +31,11 @@ class Search < Application def show @s = Chef::Search.new - @results = nil - if params[:q] - @results = @s.search(params[:id], params[:q] == "" ? "*" : params[:q]) - else - @results = @s.search(params[:id], "*") - end - # Boy, this should move to the search function - if params[:a] - attributes = params[:a].split(",").collect { |a| a.to_sym } - unless attributes.length == 0 - @results = @results.collect do |r| - nr = Hash.new - nr[:index_name] = r[:index_name] - nr[:id] = r[:id] - attributes.each do |attrib| - if r.has_key?(attrib) - nr[attrib] = r[attrib] - end - end - nr - end - end - end + + query = params[:q].nil? ? "*" : (params[:q].empty? ? "*" : params[:q]) + attributes = params[:a].nil? ? [] : params[:a].split(",").collect { |a| a.to_sym } + @results = @s.search(params[:id], query, attributes) + display @results end |