class EsSearchable::SearchCollection

Constants

Attrs
SearchName

Public Class Methods

new(klass) click to toggle source
# File lib/es_searchable/search_collection.rb, line 7
def initialize(klass)
        @klass = klass
        @time, @response, @count, @collections = nil, nil, nil, nil
end

Public Instance Methods

==(coll) click to toggle source
# File lib/es_searchable/search_collection.rb, line 93
def ==(coll)
        self.search_params == coll.search_params
end
each(&block) click to toggle source
# File lib/es_searchable/search_collection.rb, line 81
def each(&block)
  self.load.collections.each(&block)
end
like(params) click to toggle source
# File lib/es_searchable/search_collection.rb, line 31
def like(params)
        store_conditions :query, :must, params.map { |k, v| parse_like_params(k, v) }
        self.clone
end
limit(limit) click to toggle source
# File lib/es_searchable/search_collection.rb, line 49
def limit(limit)
        @limit = conditions[:size] = limit
        self.clone
end
load() click to toggle source
# File lib/es_searchable/search_collection.rb, line 72
def load
        load_data
        @klass.handle_es_response(self)
end
load_json() click to toggle source
# File lib/es_searchable/search_collection.rb, line 77
def load_json
        load_data
end
map(&block) click to toggle source
# File lib/es_searchable/search_collection.rb, line 85
def map(&block)
  self.load.collections.map(&block)
end
offset(offset) click to toggle source
# File lib/es_searchable/search_collection.rb, line 54
def offset(offset)
        @offset = conditions[:from] = offset
        self.clone
end
parse_like_params(key, value) click to toggle source
# File lib/es_searchable/search_collection.rb, line 36
def parse_like_params(key, value)
        if value.is_a?(Hash)
                { match: { key => value.map {|k, v| { operator: k, query: v}}.first }}
        else
                { match: { key => value }}
        end
end
search_params() click to toggle source
# File lib/es_searchable/search_collection.rb, line 89
def search_params
        conditions
end
select(*attrs) click to toggle source
# File lib/es_searchable/search_collection.rb, line 44
def select(*attrs)
        conditions.merge!(fields: attrs)
        self.clone
end

Private Instance Methods

conditions() click to toggle source
# File lib/es_searchable/search_collection.rb, line 120
def conditions
        @conditions ||= Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
end
load_data() click to toggle source
# File lib/es_searchable/search_collection.rb, line 101
def load_data
        @response = @klass.es_search(conditions)
        @time = @response["took"]
        @count = @response["hits"]["total"]

        @collections = @response["hits"]["hits"].map do |i| 
                if self.search_params[:fields].blank?
                        i['_source']
                else
                        {}.tap do |hash|
                                i['fields'].each do |k, v|
                                        hash[k] = [v].flatten.first
                                end
                        end
                end
        end
        @response
end
method_missing(meth, *args, &blk) click to toggle source
Calls superclass method
# File lib/es_searchable/search_collection.rb, line 164
def method_missing(meth, *args, &blk)
        case meth
        when /(.*)_(gt|lt|gte|lte)/
                conds = [{ range: { $1  =>  { $2.to_sym  => args.first } } }]
                store_conditions(:filter, :must, conds)
                return self.clone
        when /(.*)_between/
                conds = [{ range: { $1  =>  { gte: args[0], lte: args[1] } } }]
                store_conditions(:filter, :must, conds)
                return self.clone
        when /(.*)_like/
                if args.length == 1
                        conds = [{ match: { $1 => args.first } }]
                        store_conditions(:query, :must, conds)
                elsif args.length == 2 && %w(and or).include?(args.last.to_s)
                        conds = [{ match: { $1 => { operator: args.last, query: args.first } } }]
                        store_conditions(:query, :must, conds)
                else
                end
                return self.clone
        else
                super
        end
end
parse_params(params) click to toggle source
# File lib/es_searchable/search_collection.rb, line 142
def parse_params(params)
        [].tap do |filters|
                params.each do |k, v|
                        case v
                        when Array
                                filters << { terms: { k => v } }
                        when Hash
                                if SearchName.include?(k)
                                        filters << { bool: { SearchName[k.to_sym] => parse_params(v) } }
                                else
                                        v[:like].present? and filters << { query: parse_like_params(k, v[:like]) }

                                        v.slice!(:lte, :gte, :lt, :gt)
                                        v.present? and filters << { range: { k => v } }
                                end
                        else 
                                filters << { term: { k => v } }
                        end
                end
        end
end
set_filters(filters, type) click to toggle source
# File lib/es_searchable/search_collection.rb, line 124
def set_filters(filters, type)
        store_conditions(:filter, type, filters)
end
set_queries(queries, type) click to toggle source
# File lib/es_searchable/search_collection.rb, line 128
def set_queries(queries, type)
        store_conditions(:query, type, queries)
end
store_conditions(search_type, condition_type, conds) click to toggle source
# File lib/es_searchable/search_collection.rb, line 132
def store_conditions(search_type, condition_type, conds)
        conds.blank? and return

        if conditions[:query][:filtered][search_type][:bool][condition_type].present?
                conditions[:query][:filtered][search_type][:bool][condition_type] += conds
        else
                conditions[:query][:filtered][search_type][:bool][condition_type] = conds
        end
end