0
votes

I am implmenting postgresql text search into my application. When I search for a specific item i get the error.

SELECT COUNT(*) FROM "documents" INNER JOIN (SELECT "documents"."id" AS pg_search_id, (ts_rank((to_tsvector('english', coalesce("documents"."title"::text, '')) || to_tsvector('english', coalesce("documents"."content"::text, ''))), (to_tsquery('english', ''' ' || 'coffee' || ' ''')), 0)) AS rank FROM "documents" WHERE (((to_tsvector('english', coalesce("documents"."title"::text, '')) || to_tsvector('english', coalesce("documents"."content"::text, ''))) @@ (to_tsquery('english', ''' ' || 'coffee' || ' '''))))) pg_search_documents ON "documents"."id" = pg_search_documents.pg_search_id

in my document model

   include PgSearch
    pg_search_scope :search, :against => [:title, :content],
    :using => {tsearch: {dictionary: "english"}}

  def self.text_search(query)
    if query.present?
     search(query)
    else
     all
  end
 end

in my document controller

  def load_documents
    @documents = documents_scope.all.text_search(params[:query])
  end

how my database is wired in the console

Document(id: uuid, category_id: uuid, title: string, created_at: datetime, updated_at: datetime, version_id: uuid)

DocumentVersion(id: uuid, document_id: uuid, document_version_id: uuid, user_id: uuid, title: string, content: text, created_at: datetime, updated_at: datetime

It has something to do with the table join, but im not sure how to fix it. thanks for your help.

2

2 Answers

0
votes

got it, in my model i needed to scope through assosciations

  include PgSearch
    pg_search_scope :search, associated_against: {
    current_version: %i(title content)
  }
0
votes

Usually when Postgres thinks data in a column is a column itself, it is because the data is being referenced in double-quotes... changing the double quotes to single-quotes will alleviate this issue but your code is a bit too convoluted to replicate... select * from <table> where user_id = "e15bde2-6f4b-4299-9e64-82116c719636"; ...will cause this problem, whereas: select * from <table> where user_id = 'e15bde2-6f4b-4299-9e64-82116c719636'; ...won't.