docbrown/app/services/search/query_builders/user.rb
rhymes cc8bfcb5c0
Enable Rubocop 0.89 cops and fix Lint/ violations (#9709)
* Enable new Lint/ cops and run rubocop -a

* Fixing last remaining things with rubocop -a

* Enable and fix Style/ExplicitBlockArgument and Style/GlobalStdStream

* Forgot parenthesis
2020-08-10 16:57:12 +02:00

51 lines
1.2 KiB
Ruby

module Search
module QueryBuilders
class User < QueryBase
QUERY_KEYS = %i[
search_fields
].freeze
# In the event we want to search for documents that do NOT contain certain values
EXCLUDED_TERM_KEYS = {
exclude_roles: "roles"
}.freeze
DEFAULT_PARAMS = {
sort_by: "hotness_score",
sort_direction: "desc",
size: 0
}.freeze
def initialize(params:)
super()
@params = params.deep_symbolize_keys
# default to excluding users who are banned
@params[:exclude_roles] = ["banned"]
build_body
end
private
def build_queries
@body[:query] = { bool: {} }
@body[:query][:bool][:must] = query_conditions if query_keys_present?
@body[:query][:bool][:must_not] = excluded_term_keys if excluded_term_keys_present?
end
def excluded_term_keys_present?
self.class::EXCLUDED_TERM_KEYS.detect { |key, _| @params[key].present? }
end
def excluded_term_keys
EXCLUDED_TERM_KEYS.filter_map do |term_key, search_key|
next unless @params.key? term_key
{ terms: { search_key => Array.wrap(@params[term_key]) } }
end
end
end
end
end