class CustomAttributes::FluentSearchQuery

Public Class Methods

new(customizable) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 5
def initialize(customizable)
  @search_query = CustomAttributes::SearchQuery.new
  @search_query.customizable = customizable unless customizable.nil?

  @raw_results = nil
  @field_list = {}
  @field_queries = {}
  @field_config = {}
end

Public Instance Methods

count() click to toggle source

Performs the search and returns the count of results

# File lib/custom_attributes/fluent_search_query.rb, line 126
def count
  search.results.count
rescue Faraday::ConnectionFailed => e
  @search_query.customizable.class.handle_search_connection_error(e)
end
field_config(field_config) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 36
def field_config(field_config)
  @field_config = field_config

  self
end
field_list(field_list) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 21
def field_list(field_list)
  @field_list = Hash[field_list.map do |type, fields|
    fields = fields.split(',') if fields.is_a? String
    [type, fields]
  end]

  self
end
field_queries(field_queries) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 30
def field_queries(field_queries)
  @field_queries = field_queries

  self
end
filter_by(field_list) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 42
def filter_by(field_list)
  @search_query.filter_by.merge! field_list

  self
end
filter_by_delimiter(string) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 54
def filter_by_delimiter(string)
  @search_query.filter_by_delimiter = string

  self
end
filter_or(field_list) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 48
def filter_or(field_list)
  @search_query.filter_or.merge! field_list

  self
end
filter_or_delimiter(string) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 60
def filter_or_delimiter(string)
  @search_query.filter_or_delimiter = string

  self
end
match_any() click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 72
def match_any
  @search_query.match_any = true

  self
end
page(page) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 78
def page(page)
  @search_query.page = page.to_i

  self
end
per_page(per_page) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 84
def per_page(per_page)
  @search_query.per_page = per_page.to_i

  self
end
query(query) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 15
def query(query)
  @search_query.query = query

  self
end
query_hash() click to toggle source

Return the hash that is conform to elasticsearch's DSL Does not perform a search query!

# File lib/custom_attributes/fluent_search_query.rb, line 139
def query_hash
  process_field_list

  @search_query.build
end
raw() click to toggle source

Use at own risk, no Connection Exception handling here

# File lib/custom_attributes/fluent_search_query.rb, line 133
def raw
  @raw_results
end
records() click to toggle source

Fetches the ActiveRecord database records Does not perform search, so call search() first!

# File lib/custom_attributes/fluent_search_query.rb, line 113
def records
  return nil if @raw_results.nil?

  begin
    @raw_results.count

    @raw_results.records
  rescue Faraday::ConnectionFailed => e
    @search_query.customizable.class.handle_search_connection_error(e)
  end
end
results() click to toggle source

Returns the results after search has been performed Call search() first!

# File lib/custom_attributes/fluent_search_query.rb, line 99
def results
  return nil if @raw_results.nil?

  begin
    @raw_results.count

    @raw_results.results
  rescue Faraday::ConnectionFailed => e
    @search_query.customizable.class.handle_search_connection_error(e)
  end
end
sort_by(field_list) click to toggle source
# File lib/custom_attributes/fluent_search_query.rb, line 66
def sort_by(field_list)
  @search_query.sort_by.merge! field_list

  self
end

Private Instance Methods

process_field_list() click to toggle source

Combine field list, field queries and field config into one field list

# File lib/custom_attributes/fluent_search_query.rb, line 148
def process_field_list
  # apply default fields if no user specified fields are present
  if @field_list.empty?
    field_list = @search_query.default_fields
  else
    field_list = Hash[@field_list.map { |type, list| [type, Hash[list.map { |v| [v.to_sym, {}] }]] }]
  end

  # field list is in the format {fields: {field1: {}, field2: {},...}, custom_fields: {field1: {}, ...}}
  # this mapper has the purpose to populate the hash with additional configuration on a per-field basis
  #
  # type: either :fields or :custom_fields
  # list: a hash of fields with options
  field_list.map do |type, list|
    [
      type,
      Hash[list.map do |slug, options|
        # apply options already present in the field_list (possible default settings)
        field_options = options

        # overwrite query (fulltext search) with field specific setting
        if @field_queries[type] && @field_queries[type][slug]
          field_options[:query] = @field_queries[type][slug]
        end

        # add other configuration such as fuzziness, operator, ...
        if @field_config[type] && @field_config[type][slug]
          field_options.merge! @field_config[type][slug]
        end

        [slug, field_options]
      end]
    ]
  end

  @search_query.field_list = field_list
end