Skip to content

Commit

Permalink
Merge pull request #2587 from MushroomObserver/standardize-controller…
Browse files Browse the repository at this point in the history
…-indexes

Merge pattern, regex, advanced search flavors into query params
  • Loading branch information
nimmolo authored Dec 20, 2024
2 parents 5d35379 + ac55640 commit b5bba70
Show file tree
Hide file tree
Showing 41 changed files with 623 additions and 740 deletions.
1 change: 0 additions & 1 deletion app/classes/pattern_search/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def build_query
self.args = {}
parser.terms.each do |term|
if term.var == :pattern
self.flavor = :pattern_search
args[:pattern] = term.parse_pattern
elsif (param = lookup_param(term.var))
query_param, parse_method = param
Expand Down
15 changes: 7 additions & 8 deletions app/classes/pattern_search/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ def model
def build_query
super

# Temporary hack to get include_subtaxa/synonyms to work.
# Will rip out when we do away with pattern search query flavor.
if flavor == :pattern_search &&
(!args[:include_subtaxa].nil? || !args[:include_synonyms].nil?)
self.flavor = :all
args[:names] = args[:pattern]
args.delete(:pattern)
end
# This converts any search that *looks like* a name search into
# an actual name search. NOTE: This affects the index title.
return unless args[:include_subtaxa].present? ||
args[:include_synonyms].present?

args[:names] = args[:pattern]
args.delete(:pattern)
end
end
end
6 changes: 2 additions & 4 deletions app/classes/pattern_search/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ def build_query
private

# Temporary hack to get include_subtaxa/synonyms to work.
# Will rip out when we do away with pattern search query flavor.
# This converts any search that *looks like* a name search into
# an actual name search.
# an actual name search. NOTE: This affects the index title.
def hack_name_query
return unless flavor == :pattern_search &&
args[:names].empty? &&
return unless args[:pattern].present? && args[:names].empty? &&
(is_pattern_a_name? || any_taxa_modifiers_present?)

self.flavor = :all
Expand Down
2 changes: 1 addition & 1 deletion app/classes/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
#
# # The setup all happens in observations/show:
# outer = find_or_create_query(:Observation)
# inner = create_query(:Image, :inside_observation, outer: outer,
# inner = create_query(:Image, :all, outer: outer,
# observation: @observation)
# inner.results each do |image|
# link_to(image,
Expand Down
55 changes: 0 additions & 55 deletions app/classes/query/image_advanced_search.rb

This file was deleted.

81 changes: 79 additions & 2 deletions 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 All @@ -18,6 +19,8 @@ def parameter_declarations
by_user?: User,
users?: [User],
locations?: [:string],
outer?: :query, # for images inside observations
observation?: Observation, # for images inside observations
observations?: [Observation],
project?: Project,
projects?: [:string],
Expand All @@ -34,13 +37,16 @@ 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
def initialize_flavor
super
unless is_a?(Query::ImageWithObservations)
add_ids_condition("images")
add_inside_observation_conditions
add_owner_and_time_stamp_conditions("images")
add_by_user_condition("images")
add_date_condition("images.when", params[:date])
Expand All @@ -50,11 +56,13 @@ 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
initialize_vote_parameters
end
# rubocop:enable Metrics/AbcSize

def initialize_notes_parameters
add_boolean_condition("LENGTH(COALESCE(images.notes,'')) > 0",
Expand Down Expand Up @@ -118,13 +126,70 @@ def initialize_vote_parameters
def add_pattern_condition
return if params[:pattern].blank?

add_search_condition(search_fields, params[:pattern])
add_join(:observation_images, :observations)
add_join(:observations, :locations!)
add_join(:observations, :names)
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]

obs = find_cached_parameter_instance(Observation, :observation)
@title_args[:observation] = obs.unique_format_name
imgs = image_set(obs)
where << "images.id IN (#{imgs})"
self.order = "FIND_IN_SET(images.id,'#{imgs}') ASC"
self.outer_id = params[:outer]
skip_observations_with_no_images
end

def image_set(obs)
ids = []
ids << obs.thumb_image_id if obs.thumb_image_id
ids += obs.image_ids - [obs.thumb_image_id]
clean_id_set(ids)
end

# Tell outer query to skip observations with no images!
def skip_observations_with_no_images
self.tweak_outer_query = lambda do |outer|
extend_where(outer.params) << "observations.thumb_image_id IS NOT NULL"
end
end

def search_fields
"CONCAT(" \
"names.search_name," \
Expand All @@ -135,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
35 changes: 0 additions & 35 deletions app/classes/query/image_inside_observation.rb

This file was deleted.

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
11 changes: 11 additions & 0 deletions app/classes/query/initializers/advanced_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Initializers
module AdvancedSearch
# NOTE: The autocomplaters for name, location, and user all make the ids
# available now, so this could be a lot more efficient.
# But sometimes you're looking for strings that aren't ids.
def advanced_search_parameter_declarations
{
name?: :string,
Expand All @@ -16,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
Loading

0 comments on commit b5bba70

Please sign in to comment.