refactor query builders but pulling out common logic into a base class (#6678)

This commit is contained in:
Molly Struve 2020-03-18 08:34:20 -05:00 committed by GitHub
parent 92a8d624c3
commit ea70d4c4b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 138 deletions

View file

@ -1,6 +1,6 @@
module Search
module QueryBuilders
class ChatChannelMembership
class ChatChannelMembership < QueryBase
FILTER_KEYS = %i[
channel_status
channel_type
@ -18,8 +18,6 @@ module Search
size: 0
}.freeze
attr_accessor :params, :body
def initialize(params, user_id)
@params = params.deep_symbolize_keys
@params[:viewable_by] = user_id
@ -31,38 +29,14 @@ module Search
build_body
end
def as_hash
@body
end
private
def build_body
@body = ActiveSupport::HashWithIndifferentAccess.new
build_queries
add_sort
set_size
end
def build_queries
@body[:query] = {}
@body[:query][:bool] = { filter: filter_conditions }
@body[:query][:bool][:must] = query_conditions if query_keys_present?
end
def add_sort
sort_key = @params[:sort_by] || DEFAULT_PARAMS[:sort_by]
sort_direction = @params[:sort_direction] || DEFAULT_PARAMS[:sort_direction]
@body[:sort] = {
sort_key => sort_direction
}
end
def set_size
# By default we will return 0 documents if size is not specified
@body[:size] = @params[:size] || DEFAULT_PARAMS[:size]
end
def filter_conditions
FILTER_KEYS.map do |filter_key|
next if @params[filter_key].blank? || @params[filter_key] == "all"
@ -70,25 +44,6 @@ module Search
{ term: { filter_key => @params[filter_key] } }
end.compact
end
def query_keys_present?
QUERY_KEYS.detect { |key| @params[key].present? }
end
def query_conditions
QUERY_KEYS.map do |query_key|
next if @params[query_key].blank?
{
simple_query_string: {
query: "#{@params[query_key]}*",
fields: [query_key],
lenient: true,
analyze_wildcard: true
}
}
end.compact
end
end
end
end

View file

@ -1,6 +1,6 @@
module Search
module QueryBuilders
class ClassifiedListing
class ClassifiedListing < QueryBase
TERM_KEYS = %i[
category
contact_via_connect
@ -28,8 +28,6 @@ module Search
size: 0
}.freeze
attr_accessor :params, :body
def initialize(params)
@params = params.deep_symbolize_keys
@ -41,38 +39,14 @@ module Search
build_body
end
def as_hash
@body
end
private
def build_body
@body = ActiveSupport::HashWithIndifferentAccess.new
build_queries
add_sort
set_size
end
def build_queries
@body[:query] = { bool: {} }
@body[:query][:bool][:filter] = filter_conditions
@body[:query][:bool][:must] = query_conditions if query_keys_present?
end
def add_sort
sort_key = @params[:sort_by] || DEFAULT_PARAMS[:sort_by]
sort_direction = @params[:sort_direction] || DEFAULT_PARAMS[:sort_direction]
@body[:sort] = {
sort_key => sort_direction
}
end
def set_size
# By default we will return 0 documents if size is not specified
@body[:size] = @params[:size] || DEFAULT_PARAMS[:size]
end
def filter_conditions
filter_conditions = []
@ -104,25 +78,6 @@ module Search
def range_keys_present?
RANGE_KEYS.detect { |key| @params[key].present? }
end
def query_keys_present?
QUERY_KEYS.detect { |key| @params[key].present? }
end
def query_conditions
QUERY_KEYS.map do |query_key|
next if @params[query_key].blank?
{
simple_query_string: {
query: "#{@params[query_key]}*",
fields: [query_key],
lenient: true,
analyze_wildcard: true
}
}
end.compact
end
end
end
end

View file

@ -0,0 +1,52 @@
module Search
module QueryBuilders
class QueryBase
attr_accessor :params, :body
def as_hash
@body
end
private
def build_body
@body = ActiveSupport::HashWithIndifferentAccess.new
build_queries
add_sort
set_size
end
def add_sort
sort_key = @params[:sort_by] || self.class::DEFAULT_PARAMS[:sort_by]
sort_direction = @params[:sort_direction] || self.class::DEFAULT_PARAMS[:sort_direction]
@body[:sort] = {
sort_key => sort_direction
}
end
def set_size
# By default we will return 0 documents if size is not specified
@body[:size] = @params[:size] || self.class::DEFAULT_PARAMS[:size]
end
def query_keys_present?
self.class::QUERY_KEYS.detect { |key| @params[key].present? }
end
def query_conditions
self.class::QUERY_KEYS.map do |query_key|
next if @params[query_key].blank?
{
simple_query_string: {
query: "#{@params[query_key]}*",
fields: [query_key],
lenient: true,
analyze_wildcard: true
}
}
end.compact
end
end
end
end

View file

@ -1,6 +1,6 @@
module Search
module QueryBuilders
class User
class User < QueryBase
QUERY_KEYS = %i[
search_fields
].freeze
@ -11,62 +11,17 @@ module Search
size: 0
}.freeze
attr_accessor :params, :body
def initialize(params)
@params = params.deep_symbolize_keys
build_body
end
def as_hash
@body
end
private
def build_body
@body = ActiveSupport::HashWithIndifferentAccess.new
build_queries
add_sort
set_size
end
def build_queries
@body[:query] = { bool: {} }
@body[:query][:bool][:must] = query_conditions if query_keys_present?
end
def add_sort
sort_key = @params[:sort_by] || DEFAULT_PARAMS[:sort_by]
sort_direction = @params[:sort_direction] || DEFAULT_PARAMS[:sort_direction]
@body[:sort] = {
sort_key => sort_direction
}
end
def set_size
# By default we will return 0 documents if size is not specified
@body[:size] = @params[:size] || DEFAULT_PARAMS[:size]
end
def query_keys_present?
QUERY_KEYS.detect { |key| @params[key].present? }
end
def query_conditions
QUERY_KEYS.map do |query_key|
next if @params[query_key].blank?
{
simple_query_string: {
query: "#{@params[query_key]}*",
fields: [query_key],
lenient: true,
analyze_wildcard: true
}
}
end.compact
end
end
end
end