class Gum::Search
Public Class Methods
inherited(klass)
click to toggle source
# File lib/gum/search.rb, line 12 def self.inherited(klass) klass.fields = Set.new klass.filters = [] klass.orders = [] end
new(params)
click to toggle source
# File lib/gum/search.rb, line 42 def initialize(params) @params = params @q = params[query_param] end
order_by(*args)
click to toggle source
# 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)
click to toggle source
# File lib/gum/search.rb, line 32 def self.query(attr) self.query_param = attr end
scope(&block)
click to toggle source
# File lib/gum/search.rb, line 24 def self.scope(&block) self.scope = block end
searchable(*attrs)
click to toggle source
# File lib/gum/search.rb, line 28 def self.searchable(*attrs) self.fields += attrs.flatten end
use(type)
click to toggle source
@param [Chewy::Type] type
# 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
Public Instance Methods
search()
click to toggle source
# 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_query()
click to toggle source
# File lib/gum/search.rb, line 47 def to_query type.query(query).filter(compile_filters).order(compile_orders) end
Private Instance Methods
compile_filters()
click to toggle source
# File lib/gum/search.rb, line 60 def compile_filters filters.map do |filter| filter.render(@params) end.compact end
compile_orders()
click to toggle source
# File lib/gum/search.rb, line 66 def compile_orders orders.map do |order| order.render(@params) end.compact end
query()
click to toggle source
# File lib/gum/search.rb, line 72 def query return {} unless @q.present? { query_string: { fields: fields.to_a, query: @q, default_operator: 'and' } } end