Fix searching for files and tables content [SCI-9855]

This commit is contained in:
Andrej 2024-06-03 15:54:11 +02:00
parent 5643f8fd66
commit 38ec558b36

View file

@ -2,6 +2,7 @@
module SearchableModel module SearchableModel
extend ActiveSupport::Concern extend ActiveSupport::Concern
DATA_VECTOR_ATTRIBUTES = ['asset_text_data.data_vector', 'tables.data_vector'].freeze
included do included do
# Helper function for relations that # Helper function for relations that
@ -174,21 +175,19 @@ module SearchableModel
def self.create_query_clause(attrs, index, negate, query_clauses, value_hash, phrase, current_operator) def self.create_query_clause(attrs, index, negate, query_clauses, value_hash, phrase, current_operator)
phrase = sanitize_sql_like(phrase) phrase = sanitize_sql_like(phrase)
phrase = Regexp.escape(phrase)
exact_match = phrase =~ /^".*"$/ exact_match = phrase =~ /^".*"$/
like = exact_match ? '~' : 'ILIKE' like = exact_match ? '~' : 'ILIKE'
phrase = exact_match ? "(^|\\s)#{phrase[1..-2]}(\\s|$)" : "%#{phrase}%"
where_clause = (attrs.map.with_index do |a, i| where_clause = (attrs.map.with_index do |attribute, i|
i = (index * attrs.count) + i i = (index * attrs.count) + i
if %w(repository_rows.id repository_number_values.data).include?(a) if %w(repository_rows.id repository_number_values.data).include?(attribute)
"#{a} IS NOT NULL AND (((#{a})::text) #{like} :t#{i}) OR " "#{attribute} IS NOT NULL AND (((#{attribute})::text) #{like} :t#{i}) OR "
elsif defined?(model::PREFIXED_ID_SQL) && a == model::PREFIXED_ID_SQL elsif defined?(model::PREFIXED_ID_SQL) && attribute == model::PREFIXED_ID_SQL
"#{a} IS NOT NULL AND (#{a} #{like} :t#{i}) OR " "#{attribute} IS NOT NULL AND (#{attribute} #{like} :t#{i}) OR "
elsif ['asset_text_data.data_vector', 'tables.data_vector'].include?(a) elsif DATA_VECTOR_ATTRIBUTES.include?(attribute)
"#{a} @@ plainto_tsquery(:t#{i}) OR " "#{attribute} @@ to_tsquery(:t#{i}) OR "
else else
"#{a} IS NOT NULL AND ((trim_html_tags(#{a})) #{like} :t#{i}) OR " "#{attribute} IS NOT NULL AND ((trim_html_tags(#{attribute})) #{like} :t#{i}) OR "
end end
end).join[0..-5] end).join[0..-5]
@ -199,9 +198,20 @@ module SearchableModel
end end
value_hash.merge!( value_hash.merge!(
(attrs.map.with_index do |_, i| (attrs.map.with_index do |attribute, i|
i = (index * attrs.count) + i i = (index * attrs.count) + i
["t#{i}".to_sym, phrase]
new_phrase = exact_match ? phrase[1..-2] : phrase
if DATA_VECTOR_ATTRIBUTES.include?(attribute)
new_phrase = Regexp.escape(new_phrase.gsub(/[!()&|:]/, ' ').strip).split(/\s+/)
new_phrase.map! { |t| "#{t}:*" } unless exact_match
new_phrase = new_phrase.join('&').tr('\'', '"')
else
new_phrase = Regexp.escape(new_phrase)
new_phrase = exact_match ? "(^|\\s)#{new_phrase}(\\s|$)" : "%#{new_phrase}%"
end
["t#{i}".to_sym, new_phrase]
end).to_h end).to_h
) )
end end