Module: PgFullTextSearchable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Issue
- Defined in:
- app/models/concerns/pg_full_text_searchable.rb
Overview
This module adds PG full-text search capabilities to a model. A ‘search_data` association with a `search_vector` column is required.
Declare the fields that will be part of the search vector with their corresponding weights. Possible values for weight are A, B, C, or D. For example:
include PgFullTextSearchable pg_full_text_searchable columns: [{ name: ‘title’, weight: ‘A’ }, { name: ‘description’, weight: ‘B’ }]
This module sets up an after_commit hook that updates the search data when the searchable columns are changed. You will need to implement the ‘#persist_pg_full_text_search_vector` method that does the actual insert or update.
This also adds a ‘pg_full_text_search` scope so you can do:
Model.pg_full_text_search(“some search term”)
Constant Summary collapse
- LONG_WORDS_REGEX =
%r([A-Za-z0-9+/@]{50,})
- TSVECTOR_MAX_LENGTH =
1.megabyte.freeze
- TEXT_SEARCH_DICTIONARY =
'english'
- URL_SCHEME_REGEX =
%r{(?<=\A|\W)\w+://(?=\w+)}
- TSQUERY_DISALLOWED_CHARACTERS_REGEX =
%r{[^a-zA-Z0-9 .@/\-_"]}
Instance Method Summary collapse
Instance Method Details
#update_search_data! ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/concerns/pg_full_text_searchable.rb', line 30 def update_search_data! tsvector_sql_nodes = self.class.pg_full_text_searchable_columns.map do |column, weight| tsvector_arel_node(column, weight)&.to_sql end persist_pg_full_text_search_vector(Arel.sql(tsvector_sql_nodes.compact.join(' || '))) rescue ActiveRecord::StatementInvalid => e raise unless e.cause.is_a?(PG::ProgramLimitExceeded) && e..include?('string is too long for tsvector') Gitlab::AppJsonLogger.error( message: 'Error updating search data: string is too long for tsvector', class: self.class.name, model_id: self.id ) end |