Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#34363] Project custom field set to searchable is not searchable

Dombi Attila650c2a7aba46
diff --git a/db/migrate/20240715183144_add_indexes_to_custom_options.rb b/db/migrate/20240715183144_add_indexes_to_custom_options.rb
new file mode 100644
index 00000000000..9eccaa26163
--- /dev/null
+++ b/db/migrate/20240715183144_add_indexes_to_custom_options.rb
@@ -0,0 +1,13 @@
+class AddIndexesToCustomOptions < ActiveRecord::Migration[7.1]
+ def change
+ # A previous migration 20230328154645_add_gin_trgm_index_on_journals_and_custom_values
+ # already enabled on the extension. Hence we do not attempt to enable it here, just
+ # to use it if it's available.
+
+ if extensions.include?("pg_trgm")
+ add_index(:custom_options, :value, using: "gin", opclass: :gin_trgm_ops)
+ end
+
+ add_index(:custom_options, :custom_field_id)
+ end
+end
diff --git a/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb b/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb
index 24f4bc4fb3e..d51c63f7bf6 100644
--- a/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb
+++ b/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb
@@ -97,13 +97,19 @@ module Redmine
token_clauses += Array(searchable_custom_fields_conditions)
end
- sql = (["(#{token_clauses.join(' OR ')})"] * tokens.size).join(" AND ")
+ sql = Array.new(tokens.size) do |index|
+ "(#{token_clauses.join(' OR ').gsub('?', ":token_#{index}")})"
+ end.join(" AND ")
if tsv_clauses.present?
sql << (" OR #{tsv_clauses.join(' OR ')}")
end
- find_conditions = [sql, *(tokens.map { |w| "%#{w.downcase}%" } * token_clauses.size).sort]
+ named_tokens = tokens.each_with_object({}).with_index do |(token, acc), index|
+ acc[:"token_#{index}"] = "%#{token.downcase}%"
+ end
+
+ find_conditions = [sql, named_tokens]
project_conditions = [searchable_projects_condition]
@@ -171,13 +177,16 @@ module Redmine
end
def searchable_custom_fields_conditions
- searchable_custom_field_ids = CustomField.where(type: "#{name}CustomField",
- searchable: true).pluck(:id)
+ searchable_custom_field_ids = CustomField.where(type: "#{name}CustomField", searchable: true).pluck(:id)
if searchable_custom_field_ids.any?
custom_field_condition =
CustomValue.select("1").where(customized_type: name)
- .where("customized_id=#{table_name}.id AND value ILIKE ?")
+ .joins("LEFT JOIN custom_options
+ ON custom_options.id = custom_values.value::bigint
+ AND custom_options.custom_field_id = custom_values.custom_field_id")
+ .where("customized_id=#{table_name}.id")
.where(custom_field_id: searchable_custom_field_ids)
+ .where("(custom_values.value ILIKE ?) OR (custom_options.value ILIKE ?)")
"EXISTS ( #{custom_field_condition.to_sql} )"
end