Class: Orchestrate::Search::QueryBuilder
- Inherits:
-
Object
- Object
- Orchestrate::Search::QueryBuilder
- Defined in:
- lib/orchestrate/search/query_builder.rb
Overview
Query Builder object for constructing search queries
Instance Attribute Summary collapse
-
#collection ⇒ Collection
readonly
The collection this object will search.
Instance Method Summary collapse
-
#aggregate ⇒ AggregateBuilder
An AggregateBuilder object to construct aggregate search params.
-
#find ⇒ Orchestrate::Search::Results
A Results object to enumerate over.
-
#initialize(collection, query) ⇒ QueryBuilder
constructor
Initialize a new SearchBuilder object.
-
#kinds(*kinds) ⇒ QueryBuilder
Sets the 'kind' to search.
-
#limit(count = nil) ⇒ Object
Sets the limit for the query to Orchestrate, so we don't ask for more than is needed.
-
#offset(count = nil) ⇒ Object
Sets the offset for the query to Orchestrate, so you can skip items.
-
#options ⇒ Hash
Optional parameters for the search query.
-
#order(*args) ⇒ Object
Sets the sorting parameters for a query's Search Results.
-
#query ⇒ #to_s
The Lucene Query String.
-
#to_s ⇒ Object
(also: #inspect)
Pretty-Printed string representation of the Search object.
-
#types(*types) ⇒ QueryBuilder
Sets the event types to search.
Constructor Details
#initialize(collection, query) ⇒ QueryBuilder
Initialize a new SearchBuilder object
10 11 12 13 14 15 |
# File 'lib/orchestrate/search/query_builder.rb', line 10 def initialize(collection, query) @collection = collection @query = query @kinds = [] @types = [] end |
Instance Attribute Details
#collection ⇒ Collection (readonly)
Returns The collection this object will search.
5 6 7 |
# File 'lib/orchestrate/search/query_builder.rb', line 5 def collection @collection end |
Instance Method Details
#aggregate ⇒ AggregateBuilder
Returns An AggregateBuilder object to construct aggregate search params.
96 97 98 |
# File 'lib/orchestrate/search/query_builder.rb', line 96 def aggregate [:aggregate] ||= AggregateBuilder.new(self) end |
#find ⇒ Orchestrate::Search::Results
Returns A Results object to enumerate over.
101 102 103 104 |
# File 'lib/orchestrate/search/query_builder.rb', line 101 def find [:aggregate] = [:aggregate].to_param if [:aggregate] Results.new(collection, query, ) end |
#kinds(*kinds) ⇒ QueryBuilder
Sets the 'kind' to search.
82 83 84 85 |
# File 'lib/orchestrate/search/query_builder.rb', line 82 def kinds(*kinds) @kinds = kinds self end |
#limit ⇒ Integer? #limit(count) ⇒ Search
Sets the limit for the query to Orchestrate, so we don't ask for more than is needed. Does not fire a request.
54 55 56 57 58 59 60 61 |
# File 'lib/orchestrate/search/query_builder.rb', line 54 def limit(count=nil) if count [:limit] = count > 100 ? 100 : count self else [:limit] end end |
#offset ⇒ Integer? #offset(count) ⇒ Search
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.
70 71 72 73 74 75 76 77 |
# File 'lib/orchestrate/search/query_builder.rb', line 70 def offset(count=nil) if count [:offset] = count self else [:offset] end end |
#options ⇒ Hash
Returns Optional parameters for the search query.
32 33 34 |
# File 'lib/orchestrate/search/query_builder.rb', line 32 def @options ||= { limit: 100 } end |
#order(*args) ⇒ Object
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
42 43 44 45 |
# File 'lib/orchestrate/search/query_builder.rb', line 42 def order(*args) [:sort] = args.each_slice(2).map {|field, dir| dir ||= :asc; "#{field}:#{dir}" }.join(',') self end |
#query ⇒ #to_s
Returns The Lucene Query String.
18 19 20 21 22 23 |
# 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 |
#to_s ⇒ Object Also known as: inspect
Returns Pretty-Printed string representation of the Search object.
26 27 28 |
# File 'lib/orchestrate/search/query_builder.rb', line 26 def to_s "#<Orchestrate::Search::QueryBuilder collection=#{collection.name} query=#{query} options=#{}>" end |
#types(*types) ⇒ QueryBuilder
Sets the event types to search.
90 91 92 93 |
# File 'lib/orchestrate/search/query_builder.rb', line 90 def types(*types) @types = types self end |