Class: MakeTextSearch::Adapters::PostgreSQL

Inherits:
Object
  • Object
show all
Defined in:
lib/make-text-search/adapters/postgresql_ts.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ PostgreSQL

Returns a new instance of PostgreSQL.



15
16
17
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 15

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 5

def connection
  @connection
end

Class Method Details

.is_available?(connection) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 7

def self.is_available?(connection)
  begin
    connection.select_one("select to_tsquery('test') as query").has_key?("query")
  rescue ActiveRecord::StatementInvalid
    false
  end
end

Instance Method Details

#create_text_search_documents_table(table_name) ⇒ Object

Schema actions



21
22
23
24
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 21

def create_text_search_documents_table(table_name)
  connection.execute %[CREATE TABLE #{table_name} (id serial primary key, record_type varchar(300) NOT NULL, record_id integer NOT NULL, language varchar(20), document tsvector)]
  connection.execute %[CREATE INDEX #{table_name}_idx ON #{table_name} USING gin(document)]
end

#quote(value) ⇒ Object

Document actions



28
29
30
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 28

def quote(value)
  @connection.quote value
end

#remove_document(record) ⇒ Object



50
51
52
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 50

def remove_document(record)
  Document.delete_all ["record_type = ? AND record_id = ?", record.class.text_search_class_name, record.id]
end

#scope_search_text(model, query, language = Rails.application.config.make_text_search.default_language) ⇒ Object

Query actions



55
56
57
58
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 55

def scope_search_text(model, query, language = Rails.application.config.make_text_search.default_language)
  document_query = Document.select("record_id").where("record_type = :type AND document @@ to_tsquery(#{language ? ":language, " : ""}:query)", :type => model.text_search_class_name, :language => language, :query => query)
  model.where %[#{model.table_name}.id IN (#{document_query.to_sql})]
end

#update_document(record) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/make-text-search/adapters/postgresql_ts.rb', line 32

def update_document(record)

  record_class = record.class
  return if record_class.text_search_fields.empty?

  record_type, record_id = record_class.text_search_class_name, record.id
  unless document = Document.find_by_record_type_and_record_id(record_type, record_id)
    document = Document.new
    document.record = record
    document.save!
  end

  language = record.text_search_language
  quoted_language = record.connection.quote(language)
  ts_vector = "to_tsvector(#{language ? "#{quoted_language}, " : ""}#{record.connection.quote record.text_search_build_document})"
  Document.update_all "language = #{quoted_language}, document = #{ts_vector}", ["id = ?", document.id]
end