Class: Gum::Search

Inherits:
Object
  • Object
show all
Extended by:
Filters
Defined in:
lib/gum/search.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filters

define_filter, register

Constructor Details

#initialize(params) ⇒ Search

Returns a new instance of Search.



42
43
44
45
# File 'lib/gum/search.rb', line 42

def initialize(params)
  @params = params
  @q = params[query_param]
end

Class Method Details

.inherited(klass) ⇒ Object



12
13
14
15
16
# File 'lib/gum/search.rb', line 12

def self.inherited(klass)
  klass.fields = Set.new
  klass.filters = []
  klass.orders = []
end

.order_by(*args) ⇒ Object



36
37
38
39
40
# File 'lib/gum/search.rb', line 36

def self.order_by(*args)
  Factory.build(Gum::OrderBy, args) do |order|
    orders.push order
  end
end

.query(attr) ⇒ Object



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

def self.query(attr)
  self.query_param = attr
end

.scope(&block) ⇒ Object



24
25
26
# File 'lib/gum/search.rb', line 24

def self.scope(&block)
  self.scope = block
end

.searchable(*attrs) ⇒ Object



28
29
30
# File 'lib/gum/search.rb', line 28

def self.searchable(*attrs)
  self.fields += attrs.flatten
end

.use(type) ⇒ Object

Parameters:

  • type (Chewy::Type)

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/gum/search.rb', line 19

def self.use(type)
  raise ArgumentError, 'type must be a Chewy::Type' unless type < Chewy::Type
  self.type = type
end

Instance Method Details

#searchObject



51
52
53
54
55
56
# File 'lib/gum/search.rb', line 51

def search
  to_query.load(scope: scope)
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
  @error = e.message.match(/QueryParsingException\[([^;]+)\]/).try(:[], 1)
  type.none
end

#to_queryObject



47
48
49
# File 'lib/gum/search.rb', line 47

def to_query
  type.query(query).filter(compile_filters).order(compile_orders)
end