Skip to content

Commit

Permalink
Add advanced_search to base
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Dec 20, 2024
1 parent 4d7d470 commit ac55640
Show file tree
Hide file tree
Showing 24 changed files with 337 additions and 366 deletions.
55 changes: 0 additions & 55 deletions app/classes/query/image_advanced_search.rb

This file was deleted.

49 changes: 48 additions & 1 deletion app/classes/query/image_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Query
# base class for all flavors of Query which return Images
class ImageBase < Query::Base
include Query::Initializers::Names
include Query::Initializers::AdvancedSearch

def model
Image
Expand Down Expand Up @@ -36,7 +37,8 @@ def parameter_declarations
confidence?: [:float],
ok_for_export?: :boolean,
pattern?: :string
).merge(names_parameter_declarations)
).merge(names_parameter_declarations).
merge(advanced_search_parameter_declarations)
end

# rubocop:disable Metrics/AbcSize
Expand All @@ -54,6 +56,7 @@ def initialize_flavor
# add_by_user_condition("images")
# add_ids_condition
add_pattern_condition
add_advanced_search_conditions
initialize_association_parameters
initialize_name_parameters(:observation_images, :observations)
initialize_image_parameters
Expand Down Expand Up @@ -129,6 +132,38 @@ def add_pattern_condition
super
end

def add_advanced_search_conditions
return if advanced_search_params.all? { |key| params[key].blank? }
return if handle_content_search!

add_join(:observation_images, :observations)
initialize_advanced_search
end

# Perform content search as an observation query, then
# coerce into images.
def handle_content_search!
return false if params[:content].blank?

self.executor = lambda do |args|
execute_content_search(args)
end
end

def execute_content_search(args)
# [Sorry, yes, this is a mess. But I don't expect this type of search
# to survive much longer. Image searches are in desperate need of
# critical revision for performance concerns, anyway. -JPH 20210809]
args2 = args.except(:select, :order, :group)
params2 = params.except(:by)
ids = Query.lookup(:Observation, flavor, params2).result_ids(args2)
ids = clean_id_set(ids)
args2 = args.dup
extend_join(args2) << :observation_images
extend_where(args2) << "observation_images.observation_id IN (#{ids})"
model.connection.select_rows(query(args2))
end

def add_inside_observation_conditions
return unless params[:observation] && params[:outer]

Expand Down Expand Up @@ -165,6 +200,18 @@ def search_fields
")"
end

def add_join_to_names
add_join(:observations, :names)
end

def add_join_to_users
add_join(:observations, :users)
end

def add_join_to_locations
add_join(:observations, :locations!)
end

def self.default_order
"created_at"
end
Expand Down
4 changes: 0 additions & 4 deletions app/classes/query/image_with_observations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ def default_order
"name"
end

def add_join_to_locations!
add_join(:observations, :locations!)
end

def coerce_into_observation_query
Query.lookup(:Observation, :all, params_with_old_by_restored)
end
Expand Down
10 changes: 10 additions & 0 deletions app/classes/query/initializers/advanced_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ def advanced_search_parameter_declarations
}
end

# These are the ones that if present, are definitive of advanced_search.
def self.advanced_search_params
[:name, :user, :user_where, :content]
end

def advanced_search_params
[:name, :user, :user_where, :content]
end

def initialize_advanced_search
name, user, location, content = google_parse_params
make_sure_user_entered_something(name, user, location, content)
add_name_condition(name)
add_user_condition(user)
add_location_condition(location)
add_content_condition(content)
@title_tag = :query_title_all_filtered # no longer set by flavor
end

def google_parse_params
Expand Down
76 changes: 76 additions & 0 deletions app/classes/query/initializers/descriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

module Query
module Initializers
# initializing methods inherited by all Query's for Descriptions
# currently only Names
module Descriptions
def descriptions_parameter_declarations
{
with_default_desc?: :boolean,
join_desc?: { string: [:default, :any] },
desc_type?: [{ string: [Description.all_source_types] }],
desc_project?: [:string],
desc_creator?: [User],
desc_content?: :string,
ok_for_export?: :boolean
}
end

def initialize_description_parameters(type = :name)
initialize_with_default_desc_parameter(type)
initialize_join_desc_parameter(type)
initialize_desc_type_parameter(type)
initialize_desc_project_parameter(type)
initialize_desc_creator_parameter(type)
initialize_desc_content_parameter(type)
end

def initialize_with_default_desc_parameter(type)
add_boolean_condition(
"#{type}s.description_id IS NOT NULL",
"#{type}s.description_id IS NULL",
params[:with_default_desc]
)
end

def initialize_join_desc_parameter(type)
if params[:join_desc] == :default
add_join(:"#{type}_descriptions.default")
elsif any_param_desc_fields?
add_join(:"#{type}_descriptions")
end
end

def initialize_desc_type_parameter(type)
add_indexed_enum_condition(
"#{type}_descriptions.source_type",
params[:desc_type],
Description.all_source_types
)
end

def initialize_desc_project_parameter(type)
add_id_condition(
"#{type}_descriptions.project_id",
lookup_projects_by_name(params[:desc_project])
)
end

def initialize_desc_creator_parameter(type)
add_id_condition(
"#{type}_descriptions.user_id",
lookup_users_by_name(params[:desc_creator])
)
end

def initialize_desc_content_parameter(type)
model = "#{type}_descriptions".classify.constantize
fields = model.all_note_fields
fields = fields.map { |f| "COALESCE(#{type}_descriptions.#{f},'')" }
fields = "CONCAT(#{fields.join(",")})"
add_search_condition(fields, params[:desc_content])
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,18 @@ def title_for_species_lists
:query_title_in_species_list.t(type: :observation, species_list: str)
end

# takes a user_id
def title_for_by_user
str = ensure_integer(params[:by_user], User, :name)
:query_title_by_user.t(type: :observation, user: str)
end

# takes a search string
def title_for_user
ensure_integer(params[:user], User, :name)
:query_title_by_user.t(type: :observation, user: params[:user])
end

# takes a list of user_ids
def title_for_users
str = map_join_and_truncate(:users, User, :login)
:query_title_for_user.t(type: :observation, user: str)
Expand Down
31 changes: 0 additions & 31 deletions app/classes/query/location_advanced_search.rb

This file was deleted.

30 changes: 27 additions & 3 deletions app/classes/query/location_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Query::LocationBase < Query::Base
include Query::Initializers::ContentFilters
include Query::Initializers::AdvancedSearch

def model
Location
Expand All @@ -21,7 +22,8 @@ def parameter_declarations
west?: :float,
pattern?: :string,
regexp?: :string
).merge(content_filter_parameter_declarations(Location))
).merge(content_filter_parameter_declarations(Location)).
merge(advanced_search_parameter_declarations)
end

def initialize_flavor
Expand All @@ -31,10 +33,11 @@ def initialize_flavor
add_owner_and_time_stamp_conditions("locations")
add_by_user_condition("locations")
add_by_editor_condition(:location)
add_pattern_condition
add_regexp_condition
add_advanced_search_conditions
end
add_bounding_box_conditions_for_locations
add_pattern_condition
add_regexp_condition
initialize_content_filters(Location)
super
end
Expand All @@ -46,6 +49,13 @@ def add_pattern_condition
super
end

def add_advanced_search_conditions
return if advanced_search_params.all? { |key| params[key].blank? }

add_join(:observations) if params[:content].present?
initialize_advanced_search
end

def add_regexp_condition
return if params[:regexp].blank?

Expand All @@ -54,6 +64,20 @@ def add_regexp_condition
where << "locations.name REGEXP #{regexp}"
end

def add_join_to_names
add_join(:observations, :names)
end

def add_join_to_users
add_join(:observations, :users)
end

def add_join_to_locations; end

def content_join_spec
{ observations: :comments }
end

def search_fields
"CONCAT(" \
"locations.name," \
Expand Down
2 changes: 1 addition & 1 deletion app/classes/query/modules/bounding_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def add_bounding_box_conditions_for_observations
cond1 = cond1.join(" AND ")
cond2 = cond2.join(" AND ")
@where << "IF(locations.id IS NULL OR #{cond0}, #{cond1}, #{cond2})"
add_join_to_locations!
add_join_to_locations
end

# ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit ac55640

Please sign in to comment.