Class: Orchestrate::Search::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrate/search/query_builder.rb

Overview

Query Builder object for constructing search queries

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#collectionCollection (readonly)



5
6
7
# File 'lib/orchestrate/search/query_builder.rb', line 5

def collection
  @collection
end

Instance Method Details

#aggregateAggregateBuilder



96
97
98
# File 'lib/orchestrate/search/query_builder.rb', line 96

def aggregate
  options[:aggregate] ||= AggregateBuilder.new(self)
end

#findOrchestrate::Search::Results



101
102
103
104
# 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

#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

#limitInteger? #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
    options[:limit] = count > 100 ? 100 : count
    self
  else
    options[:limit]
  end
end

#offsetInteger? #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
    options[:offset] = count
    self
  else
    options[:offset]
  end
end

#optionsHash



32
33
34
# File 'lib/orchestrate/search/query_builder.rb', line 32

def options
  @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

Examples:

@app[:items].search("foo").order(:name, :asc, :rank, :desc, :created_at)


42
43
44
45
# 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

#query#to_s



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_sObject Also known as: inspect



26
27
28
# File 'lib/orchestrate/search/query_builder.rb', line 26

def to_s
  "#<Orchestrate::Search::QueryBuilder collection=#{collection.name} query=#{query} options=#{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