@@ -7,7 +7,8 @@ class Document < ApplicationRecord
77 pg_search_scope :search_by_title_and_document ,
88 against : %i[ title document ] ,
99 using : {
10- tsearch : { prefix : true } # This option allows partial matches
10+ tsearch : { prefix : true , dictionary : 'english' ,
11+ tsvector_column : 'search_vector' } # This option allows partial matches
1112 }
1213
1314 has_neighbors :embedding
@@ -34,6 +35,7 @@ class Document < ApplicationRecord
3435
3536 after_save :sync_quip_doc_if_needed
3637 after_commit :schedule_embed_document_job , if : -> { previous_changes . include? ( 'check_hash' ) }
38+ before_save :update_search_vector
3739
3840 def source_type
3941 if source_url . include? ( 'quip.com' )
@@ -105,4 +107,17 @@ def schedule_embed_document_job
105107 # Set the priority and delay, and queue the job if the check_hash has changed
106108 EmbedDocumentJob . set ( priority : 5 , wait : delay_seconds . seconds ) . perform_later ( id )
107109 end
110+
111+ def update_search_vector
112+ # Properly updates the tsvector column using raw SQL
113+ sanitized_sql = Document . sanitize_sql ( [
114+ "to_tsvector('english', coalesce(?, '') || ' ' || coalesce(?, ''))" ,
115+ title ,
116+ document
117+ ] )
118+
119+ self . search_vector = Document . connection . execute (
120+ "SELECT #{ sanitized_sql } AS tsvector"
121+ ) . first [ 'tsvector' ]
122+ end
108123end
0 commit comments