scinote-web/app/models/concerns/searchable_model.rb

198 lines
6.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-02-12 23:52:43 +08:00
module SearchableModel
extend ActiveSupport::Concern
included do
# Helper function for relations that
# adds OR ILIKE where clause for all specified attributes
# for the given search query
2017-05-05 22:41:23 +08:00
scope :where_attributes_like, lambda { |attributes, query, options = {}|
return unless query
2024-04-26 17:14:42 +08:00
attrs = normalized_attributes(attributes)
2016-02-12 23:52:43 +08:00
2017-05-05 22:41:23 +08:00
if options[:whole_word].to_s == 'true' ||
options[:whole_phrase].to_s == 'true' ||
options[:at_search].to_s == 'true'
2021-07-23 17:56:28 +08:00
unless attrs.blank?
2017-05-05 22:41:23 +08:00
like = options[:match_case].to_s == 'true' ? '~' : '~*'
like = 'SIMILAR TO' if options[:at_search].to_s == 'true'
2017-05-05 22:41:23 +08:00
if options[:whole_word].to_s == 'true'
a_query = query.split
.map { |a| Regexp.escape(a) }
.join('|')
a_query = "(#{a_query})"
elsif options[:at_search].to_s == 'true'
a_query = "%#{Regexp.escape(query).downcase}%"
2017-05-05 22:41:23 +08:00
else
a_query = Regexp.escape(query)
end
2017-05-05 22:41:23 +08:00
where_str =
(attrs.map.with_index do |a, i|
if %w(repository_rows.id repository_number_values.data).include?(a)
"((#{a})::text) #{like} :t#{i} OR "
elsif defined?(model::PREFIXED_ID_SQL) && a == model::PREFIXED_ID_SQL
"#{a} #{like} :t#{i} OR "
else
col = options[:at_search].to_s == 'true' ? "lower(#{a})": a
"(trim_html_tags(#{col})) #{like} :t#{i} OR "
end
2017-05-05 22:41:23 +08:00
end
).join[0..-5]
vals = (
attrs.map.with_index do |_, i|
["t#{i}".to_sym, a_query]
2017-05-05 22:41:23 +08:00
end
).to_h
return where(where_str, vals)
end
end
like = options[:match_case].to_s == 'true' ? 'LIKE' : 'ILIKE'
if query.count(' ') > 0
2021-07-23 17:56:28 +08:00
unless attrs.blank?
2017-05-05 22:41:23 +08:00
a_query = query.split.map { |a| "%#{sanitize_sql_like(a)}%" }
2016-07-21 19:11:15 +08:00
where_str =
2016-11-11 18:41:23 +08:00
(attrs.map.with_index do |a, i|
2020-01-09 18:09:24 +08:00
if %w(repository_rows.id repository_number_values.data).include?(a)
"((#{a})::text) #{like} ANY (array[:t#{i}]) OR "
elsif defined?(model::PREFIXED_ID_SQL) && a == model::PREFIXED_ID_SQL
"#{a} #{like} ANY (array[:t#{i}]) OR "
else
"(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR "
end
2016-11-11 18:41:23 +08:00
end
).join[0..-5]
2017-05-05 22:41:23 +08:00
vals = (
attrs.map.with_index do |_, i|
["t#{i}".to_sym, a_query]
2017-05-05 22:41:23 +08:00
end
).to_h
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
return where(where_str, vals)
end
else
2021-07-23 17:56:28 +08:00
unless attrs.blank?
2016-07-21 19:11:15 +08:00
where_str =
2016-11-11 18:41:23 +08:00
(attrs.map.with_index do |a, i|
if %w(repository_rows.id repository_number_values.data).include?(a)
"((#{a})::text) #{like} :t#{i} OR "
elsif defined?(model::PREFIXED_ID_SQL) && a == model::PREFIXED_ID_SQL
"#{a} #{like} :t#{i} OR "
else
"(trim_html_tags(#{a})) #{like} :t#{i} OR "
end
2016-11-11 18:41:23 +08:00
end
).join[0..-5]
2017-05-05 22:41:23 +08:00
vals = (
attrs.map.with_index do |_, i|
["t#{i}".to_sym, "%#{sanitize_sql_like(query.to_s)}%"]
2017-05-05 22:41:23 +08:00
end
).to_h
2016-07-21 19:11:15 +08:00
return where(where_str, vals)
end
2016-02-12 23:52:43 +08:00
end
2017-05-05 22:41:23 +08:00
}
scope :where_attributes_like_boolean, lambda { |attributes, query, options = {}|
return unless query
2024-04-26 17:14:42 +08:00
attrs = normalized_attributes(attributes)
where_array = []
value_array = {}
current_phrase = ''
exact_match = false
negate = false
index = 0
query.split.each do |phrase|
phrase = phrase.strip
if phrase.start_with?('"') && phrase.ends_with?('"')
create_query(attrs, index, negate, where_array, value_array, phrase[1..-2], true)
negate = false
elsif phrase.start_with?('"')
exact_match = true
current_phrase = phrase[1..]
elsif exact_match && phrase.ends_with?('"')
exact_match = false
create_query(attrs, index, negate, where_array, value_array, "#{current_phrase} #{phrase[0..-2]}", true)
current_phrase = ''
negate = false
elsif exact_match
current_phrase = "#{current_phrase} #{phrase}"
elsif phrase.casecmp('and').zero?
next
elsif phrase.casecmp('not').zero?
negate = true
elsif phrase.casecmp('or').zero?
where_array[-1] = "#{where_array.last[0..-5]} OR "
else
create_query(attrs, index, negate, where_array, value_array, "%#{phrase}%")
negate = false
end
index += 1
end
if current_phrase.present?
current_phrase = current_phrase[0..-2] if current_phrase.ends_with?('"')
create_query(attrs, index, negate, where_array, value_array, current_phrase, true)
end
where(where_array.join[0..-5], value_array)
}
2024-04-26 17:14:42 +08:00
def self.normalized_attributes(attributes)
attrs = []
if attributes.blank?
# Do nothing in this case
elsif attributes.is_a? Symbol
attrs = [attributes.to_s]
elsif attributes.is_a? String
attrs = [attributes]
elsif attributes.is_a? Array
attrs = attributes.collect(&:to_s)
else
raise ArgumentError, ':attributes must be an array, symbol or string'
end
attrs
end
def self.create_query(attrs, index, negate, where_array, value_array, phrase, exact_match=false)
like = exact_match ? '~' : 'ILIKE'
phrase = "\\m#{phrase}\\M" if exact_match
where_clause = (attrs.map.with_index do |a, i|
i = (index * attrs.count) + i
if %w(repository_rows.id repository_number_values.data).include?(a)
"#{a} IS NOT NULL AND (((#{a})::text) #{like} :t#{i}) OR "
elsif defined?(model::PREFIXED_ID_SQL) && a == model::PREFIXED_ID_SQL
"#{a} IS NOT NULL AND (#{a} #{like} :t#{i}) OR "
elsif a == 'asset_text_data.data_vector'
"asset_text_data.data_vector @@ plainto_tsquery(:t#{i})) OR"
else
"#{a} IS NOT NULL AND ((trim_html_tags(#{a})) #{like} :t#{i}) OR "
end
end).join[0..-5]
where_array << if negate
"NOT (#{where_clause}) AND "
else
"(#{where_clause}) AND "
end
value_array.merge!(
(attrs.map.with_index do |_, i|
i = (index * attrs.count) + i
["t#{i}".to_sym, phrase]
end).to_h
)
end
2016-02-12 23:52:43 +08:00
end
2016-11-11 18:41:23 +08:00
end