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

Parameters:


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)

Returns The collection this object will search.

Returns:

  • (Collection)

    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

#aggregateAggregateBuilder

Returns An AggregateBuilder object to construct aggregate search params.

Returns:

  • (AggregateBuilder)

    An AggregateBuilder object to construct aggregate search params


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

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

#findOrchestrate::Search::Results

Returns A Results object to enumerate over.

Returns:


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.

Parameters:

  • kinds (Array<String>)

    The orchestrate kinds to be included ('item' or 'event').

Returns:


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.

Overloads:

  • #limitInteger?

    Returns The number of items to retrieve. Nil is equivalent to zero.

    Returns:

    • (Integer, nil)

      The number of items to retrieve. Nil is equivalent to zero.

  • #limit(count) ⇒ Search

    Returns self.

    Parameters:

    • count (Integer)

      The number of items to retrieve.

    Returns:


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.

Overloads:

  • #offsetInteger?

    Returns The number of items to skip. Nil is equivalent to zero.

    Returns:

    • (Integer, nil)

      The number of items to skip. Nil is equivalent to zero.

  • #offset(count) ⇒ Search

    Returns self.

    Parameters:

    • count (Integer)

      The number of items to skip.

    Returns:


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

Returns Optional parameters for the search query.

Returns:

  • (Hash)

    Optional parameters for the search query.


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

Returns The Lucene Query String.

Returns:

  • (#to_s)

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

Returns Pretty-Printed string representation of the Search object.

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=#{options}>"
end

#types(*types) ⇒ QueryBuilder

Sets the event types to search.

Parameters:

  • types (Array<String>)

    The orchestrate event types to search (e.g. 'activities', 'wall_posts')

Returns:


90
91
92
93
# File 'lib/orchestrate/search/query_builder.rb', line 90

def types(*types)
  @types = types
  self
end