Class: DbTextSearch::FullText

Inherits:
Object
  • Object
show all
Defined in:
lib/db_text_search/full_text.rb,
lib/db_text_search/full_text/mysql_adapter.rb,
lib/db_text_search/full_text/sqlite_adapter.rb,
lib/db_text_search/full_text/abstract_adapter.rb,
lib/db_text_search/full_text/postgres_adapter.rb

Overview

Provides basic full-text search for a list of terms, and FTS index creation.

Defined Under Namespace

Classes: AbstractAdapter, MysqlAdapter, PostgresAdapter, SqliteAdapter

Constant Summary collapse

DEFAULT_PG_TS_CONFIG =

The default Postgres text search config.

%q('english')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, column) ⇒ FullText

Returns a new instance of FullText.

Parameters:

  • scope (ActiveRecord::Relation, Class<ActiveRecord::Base>)
  • column (Symbol)

    name



15
16
17
18
# File 'lib/db_text_search/full_text.rb', line 15

def initialize(scope, column)
  @adapter = self.class.adapter_class(scope.connection, scope.table_name, column).new(scope, column)
  @scope   = scope
end

Class Method Details

.adapter_class(connection, _table_name, _column_name) ⇒ Class<AbstractAdapter>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • connection (ActiveRecord::ConnectionAdapters::AbstractAdapter)
  • _table_name (String, Symbol)
  • _column_name (String, Symbol)

Returns:



47
48
49
50
51
52
53
54
# File 'lib/db_text_search/full_text.rb', line 47

def self.adapter_class(connection, _table_name, _column_name)
  DbTextSearch.match_adapter(
      connection,
      mysql:    -> { MysqlAdapter },
      postgres: -> { PostgresAdapter },
      sqlite:   -> { SqliteAdapter }
  )
end

.add_index(connection, table_name, column_name, name: "#{table_name}_#{column_name}_fts", pg_ts_config: DEFAULT_PG_TS_CONFIG) ⇒ Object

Add an index for full text search.

Parameters:

  • connection (ActiveRecord::ConnectionAdapters::AbstractAdapter)
  • table_name (String, Symbol)
  • column_name (String, Symbol)
  • name (String, Symbol) (defaults to: "#{table_name}_#{column_name}_fts")

    index name

  • pg_ts_config (String) (defaults to: DEFAULT_PG_TS_CONFIG)

    for Postgres, the TS config to use; ignored for non-postgres.



36
37
38
39
40
# File 'lib/db_text_search/full_text.rb', line 36

def self.add_index(connection, table_name, column_name, name: "#{table_name}_#{column_name}_fts",
    pg_ts_config: DEFAULT_PG_TS_CONFIG)
  adapter_class(connection, table_name, column_name)
      .add_index(connection, table_name, column_name, name: name, pg_ts_config: pg_ts_config)
end

Instance Method Details

#search(term_or_terms, pg_ts_config: DEFAULT_PG_TS_CONFIG) ⇒ ActiveRecord::Relation

Parameters:

  • term_or_terms (String, Array<String>)
  • pg_ts_config (String) (defaults to: DEFAULT_PG_TS_CONFIG)

    for Postgres, the TS config to use; ignored for non-postgres.

Returns:

  • (ActiveRecord::Relation)


23
24
25
26
27
# File 'lib/db_text_search/full_text.rb', line 23

def search(term_or_terms, pg_ts_config: DEFAULT_PG_TS_CONFIG)
  values = Array(term_or_terms)
  return @scope.none if values.empty?
  @adapter.search(values, pg_ts_config: pg_ts_config)
end