【问题标题】:Pg Text Search~ PG::UndefinedColumn: ERROR: column documents.content does not existPg 文本搜索~ PG::UndefinedColumn: ERROR: 列documents.content 不存在
【发布时间】:2015-10-18 19:10:43
【问题描述】:

我正在我的应用程序中实现 postgresql 文本搜索。当我搜索特定项目时,我得到了错误。

SELECT COUNT(*) FROM "documents" INNER JOIN (SELECT "documents"."id" AS pg_search_id, (ts_rank((to_tsvector('english', 合并(“文档”。“标题”::文本,''))|| to_tsvector('英文', coalesce("documents"."content"::text, ''))), (to_tsquery('english', ''' ' || '咖啡' || ' ''')), 0)) AS rank FROM "documents" WHERE (((to_tsvector('english', coalesce("documents"."title"::text, '')) || to_tsvector('english', coalesce("documents"."content"::text, ''))) @@ (to_tsquery('english', ''' ' || '咖啡' || ' '''))))) pg_search_documents ON "文档"."id" = pg_search_documents.pg_search_id

在我的文档模型中

   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

在我的文档控制器中

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

我的数据库是如何连接到控制台的

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

它与表连接有关,但我不确定如何修复它。感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails postgresql ruby-on-rails-4 pg-search


    【解决方案1】:

    知道了,在我的模型中,我需要通过关联来确定范围

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

    【讨论】:

      【解决方案2】:

      通常当 Postgres 认为列中的数据是列本身时,这是因为数据被双引号引用...将双引号更改为单引号将缓解此问题,但您的代码有点太复杂的复制... select * from <table> where user_id = "e15bde2-6f4b-4299-9e64-82116c719636"; ...会导致这个问题,而: select * from <table> where user_id = 'e15bde2-6f4b-4299-9e64-82116c719636'; ...不会。

      【讨论】:

        猜你喜欢
        • 2017-03-14
        • 2015-05-11
        • 2019-09-21
        • 2013-10-10
        • 2015-10-13
        • 2013-12-15
        • 2015-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多