Class: FullSearch::Index
- Inherits:
-
Object
- Object
- FullSearch::Index
- Defined in:
- lib/full_search/index.rb
Class Method Summary collapse
- .drop!(model) ⇒ Object
- .ensure_table!(model) ⇒ Object
- .fts_table_name(model) ⇒ Object
- .optimize!(model) ⇒ Object
- .rebuild!(model) ⇒ Object
- .rebuild_if_needed!(model) ⇒ Object
- .reindex_source_fields!(model) ⇒ Object
- .sqlite? ⇒ Boolean
- .stored_config_hash(model) ⇒ Object
- .trigram_table_name(model) ⇒ Object
Class Method Details
.drop!(model) ⇒ Object
94 95 96 97 98 |
# File 'lib/full_search/index.rb', line 94 def drop!(model) drop_triggers!(model) connection.execute("DROP TABLE IF EXISTS #{fts_table_name(model)};") connection.execute("DROP TABLE IF EXISTS #{trigram_table_name(model)};") end |
.ensure_table!(model) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/full_search/index.rb', line 8 def ensure_table!(model) return unless FullSearch::Index.sqlite? conn = connection dsl = model.full_search_dsl return unless dsl table_was_created = false unless table_exists?(model) conn.execute(create_virtual_table_sql(model)) table_was_created = true end if dsl.typo_tolerance? && !trigram_table_exists?(model) FullSearch::Typo.warn_unsupported! unless FullSearch::Typo.supported? conn.execute(create_trigram_virtual_table_sql(model)) table_was_created = true end if table_was_created conn.execute(backfill_sql(model)) conn.execute(backfill_trigram_sql(model)) if dsl.typo_tolerance? reindex_source_fields!(model) if dsl.fields.any?(&:source) store_config_hash!(model) end ensure_triggers!(model) if model_table_exists?(model) end |
.fts_table_name(model) ⇒ Object
100 101 102 |
# File 'lib/full_search/index.rb', line 100 def fts_table_name(model) "#{model.table_name}_fts" end |
.optimize!(model) ⇒ Object
84 85 86 |
# File 'lib/full_search/index.rb', line 84 def optimize!(model) connection.execute("INSERT INTO #{fts_table_name(model)}(#{fts_table_name(model)}) VALUES('optimize');") end |
.rebuild!(model) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/full_search/index.rb', line 39 def rebuild!(model) return unless FullSearch::Index.sqlite? dsl = model.full_search_dsl return unless dsl conn = connection with_rebuild_lock(model) do drop_triggers!(model) conn.execute("DROP TABLE IF EXISTS #{fts_table_name(model)};") conn.execute("DROP TABLE IF EXISTS #{trigram_table_name(model)};") conn.execute(create_virtual_table_sql(model)) if dsl.typo_tolerance? FullSearch::Typo.warn_unsupported! unless FullSearch::Typo.supported? conn.execute(create_trigram_virtual_table_sql(model)) end conn.execute(backfill_sql(model)) conn.execute(backfill_trigram_sql(model)) if dsl.typo_tolerance? reindex_source_fields!(model) if dsl.fields.any?(&:source) create_triggers!(model) optimize!(model) store_config_hash!(model, rebuilt_at: Time.current) end end |
.rebuild_if_needed!(model) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/full_search/index.rb', line 66 def rebuild_if_needed!(model) return false unless FullSearch::Index.sqlite? 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
88 89 90 91 92 |
# File 'lib/full_search/index.rb', line 88 def reindex_source_fields!(model) model.find_each do |record| FullSearch::Callbacks.reindex_record!(record) end end |
.sqlite? ⇒ Boolean
108 109 110 |
# File 'lib/full_search/index.rb', line 108 def sqlite? connection.adapter_name.downcase.include?("sqlite") end |
.stored_config_hash(model) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/full_search/index.rb', line 112 def stored_config_hash(model) row = connection.execute( "SELECT config_hash FROM full_search_index_versions WHERE table_name=#{q(model.table_name)}" ).first row&.[]("config_hash") end |
.trigram_table_name(model) ⇒ Object
104 105 106 |
# File 'lib/full_search/index.rb', line 104 def trigram_table_name(model) "#{fts_table_name(model)}_trigram" end |