Sha256: 1970bac53357d8fd4dc8053cdd622643293e2ef21c9605e6a1bd48be3116ae13

Contents?: true

Size: 1.95 KB

Versions: 30

Compression:

Stored size: 1.95 KB

Contents

#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/config'
require 'uri'

class Chef
  class Search
    class Query 

      attr_accessor :rest

      def initialize(url=nil)
        @rest = Chef::REST.new(url ||Chef::Config[:search_url])
      end

      # Search Solr for objects of a given type, for a given query. If you give
      # it a block, it will handle the paging for you dynamically.
      def search(type, query="*:*", sort='X_CHEF_id_CHEF_X asc', start=0, rows=1000, &block)
        raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol))
        
        response = @rest.get_rest("search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}")
        if block
          response["rows"].each { |o| block.call(o) unless o.nil?}
          unless (response["start"] + response["rows"].length) >= response["total"]
            nstart = response["start"] + rows
            search(type, query, sort, nstart, rows, &block)
          end
          true
        else
          [ response["rows"], response["start"], response["total"] ]
        end
      end
      
      def list_indexes
        response = @rest.get_rest("search")
      end 

      private
        def escape(s)
          s && URI.escape(s.to_s) 
        end
    end
  end
end

Version data entries

30 entries across 30 versions & 4 rubygems

Version Path
chef-0.9.18 lib/chef/search/query.rb
naked-chef-0.9.15.3 lib/chef/search/query.rb
chef-0.9.16 lib/chef/search/query.rb
naked-chef-0.9.15.2 lib/chef/search/query.rb
naked-chef-0.9.15.1 lib/chef/search/query.rb
chef-0.9.14 lib/chef/search/query.rb
chef-0.9.14.rc.1 lib/chef/search/query.rb
mbailey-chef-0.9.12.2 lib/chef/search/query.rb
chef-0.9.14.beta.1 lib/chef/search/query.rb
mbailey-chef-0.9.12.1 lib/chef/search/query.rb
chef-0.9.12 lib/chef/search/query.rb
chef-0.9.10 lib/chef/search/query.rb
chef-0.9.10.rc.3 lib/chef/search/query.rb
chef-0.9.10.rc.2 lib/chef/search/query.rb
chef-0.9.10.rc.1 lib/chef/search/query.rb
chef-0.9.10.rc.0 lib/chef/search/query.rb
tecnh-chef-0.9.9.vpc lib/chef/search/query.rb
chef-0.9.8 lib/chef/search/query.rb
chef-0.9.8.rc.0 lib/chef/search/query.rb
chef-0.9.8.beta.2 lib/chef/search/query.rb