Class: FullSearch::Index
- Inherits:
-
Object
- Object
- FullSearch::Index
- Defined in:
- lib/full_search/index.rb
Class Method Summary collapse
- .create_triggers!(model) ⇒ Object
- .drop!(model) ⇒ Object
- .drop_triggers!(model) ⇒ Object
- .ensure_table!(model) ⇒ Object
- .fts_table_name(model) ⇒ Object
- .missing_table?(model) ⇒ Boolean
- .optimize!(model) ⇒ Object
- .rebuild!(model) ⇒ Object
- .rebuild_if_needed!(model) ⇒ Object
- .reindex_source_fields!(model) ⇒ Object
- .sqlite!(model) ⇒ Object
- .sqlite?(model = nil) ⇒ Boolean
- .stored_config_hash(model) ⇒ Object
- .trigram_table_exists?(model) ⇒ Boolean
- .trigram_table_name(model) ⇒ Object
- .trigram_table_needed?(model) ⇒ Boolean
- .upsert_source_row!(model, rowid, values) ⇒ Object
- .verified_tables ⇒ Object
Class Method Details
.create_triggers!(model) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/full_search/index.rb', line 137 def create_triggers!(model) connection.execute(insert_trigger_sql(model)) connection.execute(delete_trigger_sql(model)) connection.execute(update_trigger_sql(model)) if model.full_search_dsl.soft_delete_column connection.execute(soft_delete_removal_trigger_sql(model)) end if trigram_table_needed?(model) connection.execute(insert_trigram_trigger_sql(model)) connection.execute(delete_trigram_trigger_sql(model)) connection.execute(update_trigram_trigger_sql(model)) end end |
.drop!(model) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/full_search/index.rb', line 157 def drop!(model) IndexCache.clear! verified_tables.delete(model.table_name) sqlite!(model) drop_triggers!(model) connection.execute("DROP TABLE IF EXISTS #{qt(fts_table_name(model))};") connection.execute("DROP TABLE IF EXISTS #{qt(trigram_table_name(model))};") end |
.drop_triggers!(model) ⇒ Object
151 152 153 154 155 |
# File 'lib/full_search/index.rb', line 151 def drop_triggers!(model) (trigger_names(model) + trigram_trigger_names(model)).each do |name| connection.execute("DROP TRIGGER IF EXISTS #{qt(name)};") end end |
.ensure_table!(model) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/full_search/index.rb', line 21 def ensure_table!(model) return if model.respond_to?(:table_name) && verified_tables.include?(model.table_name) sqlite!(model) conn = connection dsl = model.full_search_dsl return unless dsl fts_was_created = false unless table_exists?(model) conn.execute(create_virtual_table_sql(model)) fts_was_created = true end trigram_was_created = false if trigram_table_needed?(model) && !trigram_table_exists?(model) FullSearch::Typo.warn_unsupported!(model) unless FullSearch::Typo.supported?(model) conn.execute(create_trigram_virtual_table_sql(model)) trigram_was_created = true end if fts_was_created || trigram_was_created conn.execute(backfill_sql(model)) if fts_was_created conn.execute(backfill_trigram_sql(model)) if trigram_was_created reindex_source_fields!(model) if dsl.fields.any?(&:source) store_config_hash!(model) end ensure_triggers!(model) if model_table_exists?(model) verified_tables.add(model.table_name) end |
.fts_table_name(model) ⇒ Object
166 167 168 |
# File 'lib/full_search/index.rb', line 166 def fts_table_name(model) "#{model.table_name}_fts" end |
.missing_table?(model) ⇒ Boolean
198 199 200 |
# File 'lib/full_search/index.rb', line 198 def missing_table?(model) !table_exists?(model) end |
.optimize!(model) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/full_search/index.rb', line 107 def optimize!(model) FullSearch::Instrumentation.instrument("optimize", model: model.name) do sqlite!(model) connection.execute("INSERT INTO #{qt(fts_table_name(model))}(#{qt(fts_table_name(model))}) VALUES('optimize');") end end |
.rebuild!(model) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/full_search/index.rb', line 57 def rebuild!(model) IndexCache.clear! sqlite!(model) dsl = model.full_search_dsl return unless dsl conn = connection with_rebuild_lock(model) do FullSearch::Instrumentation.instrument("rebuild", model: model.name) do drop_triggers!(model) conn.execute("DROP TABLE IF EXISTS #{qt(fts_table_name(model))};") conn.execute("DROP TABLE IF EXISTS #{qt(trigram_table_name(model))};") conn.execute(create_virtual_table_sql(model)) if trigram_table_needed?(model) FullSearch::Typo.warn_unsupported!(model) unless FullSearch::Typo.supported?(model) conn.execute(create_trigram_virtual_table_sql(model)) end conn.execute(backfill_sql(model)) conn.execute(backfill_trigram_sql(model)) if trigram_table_needed?(model) reindex_source_fields!(model) if dsl.fields.any?(&:source) create_triggers!(model) optimize!(model) store_config_hash!(model, rebuilt_at: Time.current) verified_tables.add(model.table_name) end end end |
.rebuild_if_needed!(model) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/full_search/index.rb', line 88 def rebuild_if_needed!(model) IndexCache.clear! sqlite!(model) dsl = model.full_search_dsl return false unless dsl ensure_table!(model) stored = stored_config_hash(model) if stored && stored == dsl.config_hash false else rebuild!(model) true end end |
.reindex_source_fields!(model) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/full_search/index.rb', line 114 def reindex_source_fields!(model) dsl = model.full_search_dsl return unless dsl source_fields = dsl.fields.select(&:source) return if source_fields.empty? model.find_each(batch_size: 500) do |record| pairs = source_fields.to_h { |f| [column_name(f), FullSearch::Model.evaluate_source(record, f).to_s] } next if pairs.empty? upsert_source_row!(model, record.id, pairs) end end |
.sqlite!(model) ⇒ Object
184 185 186 187 |
# File 'lib/full_search/index.rb', line 184 def sqlite!(model) adapter = model ? model.connection.adapter_name : connection.adapter_name raise UnsupportedDatabaseError, "full_search requires SQLite, but #{adapter} is configured" unless sqlite?(model) end |
.sqlite?(model = nil) ⇒ Boolean
174 175 176 177 178 179 180 181 182 |
# File 'lib/full_search/index.rb', line 174 def sqlite?(model = nil) if model model.connection.adapter_name.downcase.include?("sqlite") else connection.adapter_name.downcase.include?("sqlite") end rescue connection.adapter_name.downcase.include?("sqlite") end |
.stored_config_hash(model) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/full_search/index.rb', line 189 def stored_config_hash(model) IndexCache.fetch("stored_config_hash:#{model.table_name}") do row = connection.execute( "SELECT config_hash FROM full_search_index_versions WHERE table_name=#{q(model.table_name)}" ).first row&.[]("config_hash") end end |
.trigram_table_exists?(model) ⇒ Boolean
206 207 208 209 210 |
# File 'lib/full_search/index.rb', line 206 def trigram_table_exists?(model) connection.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name=#{q(trigram_table_name(model))} LIMIT 1" ).any? end |
.trigram_table_name(model) ⇒ Object
170 171 172 |
# File 'lib/full_search/index.rb', line 170 def trigram_table_name(model) "#{fts_table_name(model)}_trigram" end |
.trigram_table_needed?(model) ⇒ Boolean
202 203 204 |
# File 'lib/full_search/index.rb', line 202 def trigram_table_needed?(model) model.full_search_dsl&.typo_tolerance? && model.full_search_dsl.tokenize != "trigram" end |
.upsert_source_row!(model, rowid, values) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/full_search/index.rb', line 128 def upsert_source_row!(model, rowid, values) table = qt(fts_table_name(model)) sets = values.map { |name, value| "#{qc(name)} = #{q(value)}" }.join(", ") connection.execute(<<~SQL) UPDATE #{table} SET #{sets} WHERE rowid = #{q(rowid)}; SQL end |
.verified_tables ⇒ Object
17 18 19 |
# File 'lib/full_search/index.rb', line 17 def verified_tables @verified_tables ||= Set.new end |