Class: Stretchy::Builders::ShellBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/stretchy/builders/shell_builder.rb

Constant Summary collapse

DEFAULT_LIMIT =
30
DEFAULT_OFFSET =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ShellBuilder

Returns a new instance of ShellBuilder.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/stretchy/builders/shell_builder.rb', line 12

def initialize(options = {})
  @index              = options[:index] || Stretchy.index_name
  @match_builder      = Stretchy::Builders::MatchBuilder.new
  @where_builder      = Stretchy::Builders::WhereBuilder.new
  @boost_builder      = Stretchy::Builders::BoostBuilder.new
  @aggregate_builder  = {}
  @limit              = options[:limit]  || DEFAULT_LIMIT
  @offset             = options[:offset] || DEFAULT_OFFSET
  @fields             = options[:fields]
  @type               = options[:type]
end

Instance Attribute Details

#aggregate_builderObject

Returns the value of attribute aggregate_builder.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def aggregate_builder
  @aggregate_builder
end

#boost_builderObject

Returns the value of attribute boost_builder.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def boost_builder
  @boost_builder
end

#explainObject

Returns the value of attribute explain.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def explain
  @explain
end

#fieldsObject

Returns the value of attribute fields.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def fields
  @fields
end

#indexObject

Returns the value of attribute index.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def index
  @index
end

#limitObject

Returns the value of attribute limit.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def limit
  @limit
end

#match_builderObject

Returns the value of attribute match_builder.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def match_builder
  @match_builder
end

#offsetObject

Returns the value of attribute offset.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def offset
  @offset
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def type
  @type
end

#where_builderObject

Returns the value of attribute where_builder.



8
9
10
# File 'lib/stretchy/builders/shell_builder.rb', line 8

def where_builder
  @where_builder
end

Instance Method Details

#pageObject Also known as: current_page



24
25
26
# File 'lib/stretchy/builders/shell_builder.rb', line 24

def page
  (offset.to_f / limit).ceil + 1
end

#to_searchHash

Compiles the internal representation of your filters, full-text queries, and boosts into the JSON to be passed to Elastic. If you want to know exactly what your query generated, you can call this method.

Returns:

  • (Hash)

    the query hash to be compiled to json and sent to Elastic



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stretchy/builders/shell_builder.rb', line 37

def to_search
  _to_search = if where_builder.any?
    Stretchy::Queries::FilteredQuery.new(
      query:  match_builder.to_query,
      filter: where_builder.to_filter
    )
  else
    match_builder.to_query
  end

  _to_search = boost_builder.to_search(_to_search) if boost_builder.any?
  _to_search.to_search
end