Module: Sequel::Plugins::TextSearchable::InstanceMethods

Defined in:
lib/sequel/plugins/text_searchable.rb

Instance Method Summary collapse

Instance Method Details

#_run_after_model_hookObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sequel/plugins/text_searchable.rb', line 92

def _run_after_model_hook
  if SequelTextSearchable.index_mode == :async
    # We must refetch the model to index since it happens on another thread.
    SequelTextSearchable.threadpool.post do
      self.model.text_search_reindex_model(self.pk)
    end

  elsif SequelTextSearchable.index_mode == :sync
    self.text_search_reindex
  end
end

#after_createObject



75
76
77
78
# File 'lib/sequel/plugins/text_searchable.rb', line 75

def after_create
  super
  self._run_after_model_hook
end

#after_updateObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sequel/plugins/text_searchable.rb', line 80

def after_update
  super
  if self.class.text_search_terms.nil?
    # If the instance implements a custom text_search_terms, we have to always call it,
    # since we can't otherwise know if relevant values have changed.
    self._run_after_model_hook
    return
  end
  has_changes = self.class.text_search_columns_and_ranks.any? { |col, _rank| self.previous_changes.include?(col) }
  self._run_after_model_hook if has_changes
end

#text_search_reindexObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sequel/plugins/text_searchable.rb', line 127

def text_search_reindex
  got_terms = self.text_search_terms
  return if got_terms.empty?
  terms = got_terms.flat_map { |t| _text_search_term_to_col_and_rank(t) }
  exprs = terms.filter_map do |(col, rank)|
    col = Sequel.function(:coalesce, col, "")
    expr = Sequel.function(:to_tsvector, self.model.text_search_language, col)
    expr = Sequel.function(:setweight, expr, rank) if rank
    expr
  end
  full_expr = Sequel.join(exprs)
  self.this.update(self.model.text_search_column => full_expr)
end

#text_search_termsObject

Return the values used for the tsvector value.

In general this should include relevant text fields (like name and descriptions) on the receiver and related objects.

Each value in the array can be one of the following:

  • nil: skipped

  • str like ‘value’: Used in ‘to_tsvector(’value’)‘.

  • tuple[str, str] like (‘value, ’B’): Used in ‘setweight(to_tsvector(’value’), ‘B’)

  • has a text_search_values_for_related’ method: All of these are included in the returned list.

    Useful for adding all of a parent relation's fields to related components,
    while the parent may need a more complex text_search_values.
    
  • has a ‘text_search_values’ method: All of these are included in the returned list.

Raises:

  • (NotImplementedError)


118
119
120
121
122
123
124
125
# File 'lib/sequel/plugins/text_searchable.rb', line 118

def text_search_terms
  raise NotImplementedError, "#{self.class.name} must implement text_search_terms" if
    self.model.text_search_terms.nil?
  return self.model.text_search_columns_and_ranks.map do |col, rank|
    val = self.send(col)
    rank ? [val, rank] : val
  end
end