class Orchestrate::Search::QueryBuilder
Query Builder object for constructing search queries
Attributes
@return [Collection] The collection this object will search.
Public Class Methods
Initialize a new SearchBuilder object @param collection [Orchestrate::Collection] The collection to search. @param query [#to_s] The Lucene Query to perform.
# File lib/orchestrate/search/query_builder.rb, line 10 def initialize(collection, query) @collection = collection @query = query @kinds = [] @types = [] end
Public Instance Methods
@return [AggregateBuilder] An AggregateBuilder
object to construct aggregate search params
# File lib/orchestrate/search/query_builder.rb, line 96 def aggregate options[:aggregate] ||= AggregateBuilder.new(self) end
@return [Orchestrate::Search::Results] A Results
object to enumerate over.
# File lib/orchestrate/search/query_builder.rb, line 101 def find options[:aggregate] = options[:aggregate].to_param if options[:aggregate] Results.new(collection, query, options) end
Sets the ‘kind’ to search. @param kinds [Array<String>] The orchestrate kinds to be included (‘item’ or ‘event’). @return [QueryBuilder] self.
# File lib/orchestrate/search/query_builder.rb, line 82 def kinds(*kinds) @kinds = kinds self end
Sets the limit for the query to Orchestrate
, so we don’t ask for more than is needed. Does not fire a request. @overload limit
@return [Integer, nil] The number of items to retrieve. Nil is equivalent to zero.
@overload limit(count)
@param count [Integer] The number of items to retrieve. @return [Search] self.
# File lib/orchestrate/search/query_builder.rb, line 54 def limit(count=nil) if count options[:limit] = count > 100 ? 100 : count self else options[:limit] end end
Sets the offset for the query to Orchestrate
, so you can skip items. Does not fire a request. Impelemented as separate method from drop, unlike take, because take is a more common use case. @overload offset
@return [Integer, nil] The number of items to skip. Nil is equivalent to zero.
@overload offset(count)
@param count [Integer] The number of items to skip. @return [Search] self.
# File lib/orchestrate/search/query_builder.rb, line 70 def offset(count=nil) if count options[:offset] = count self else options[:offset] end end
@return [Hash] Optional parameters for the search query.
# File lib/orchestrate/search/query_builder.rb, line 32 def options @options ||= { limit: 100 } end
Sets the sorting parameters for a query’s Search
Results
. order
takes multiple arguments, but each even numbered argument must be either :asc or :desc Odd-numbered arguments defaults to :asc @example
@app[:items].search("foo").order(:name, :asc, :rank, :desc, :created_at)
# File lib/orchestrate/search/query_builder.rb, line 42 def order(*args) options[:sort] = args.each_slice(2).map {|field, dir| dir ||= :asc; "#{field}:#{dir}" }.join(',') self end
@return [#to_s] The Lucene Query String.
# File lib/orchestrate/search/query_builder.rb, line 18 def query query = "(#{@query})" query << " AND @path.kind:(#{@kinds.join(' ')})" if @kinds.any? query << " AND @path.type:(#{@types.join(' ')})" if @types.any? query end
@return Pretty-Printed string representation of the Search
object
# File lib/orchestrate/search/query_builder.rb, line 26 def to_s "#<Orchestrate::Search::QueryBuilder collection=#{collection.name} query=#{query} options=#{options}>" end
Sets the event types to search. @param types [Array<String>] The orchestrate event types to search (e.g. ‘activities’, ‘wall_posts’) @return [QueryBuilder] self.
# File lib/orchestrate/search/query_builder.rb, line 90 def types(*types) @types = types self end