简体   繁体   English

在SmartListing Gem中使用全文搜索进行分组和搜索时出现PostgreSQL错误

[英]PostgreSQL error when grouping and search with full text search in SmartListing Gem

I'm using smartlisting gem with postgresql in a rails app. 我在rails应用程序中使用smartgisting gem和postgresql。 The table shows a list of elements result of an association that in some cases the elements can repeat, but I need to show that element only once in the table, but when I add a DISTINCT or uniq to the active record it transforms in an array and smartlisting shows and error. 该表显示了一个关联结果的元素列表,在某些情况下元素可以重复,但是我需要在表中只显示该元素一次,但是当我将DISTINCT或uniq添加到活动记录时,它会转换为数组和智能列表显示和错误。 Then I tried to solve it using a gruop, and IT WORKS! 然后我尝试使用gruop解决它,它的工作原理! but when I tried to do a fulltext search using pg_scope, the pg query crash. 但是当我尝试使用pg_scope进行全文搜索时,pg查询崩溃了。 Here the code: 这里的代码:

class Prospect < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_by_name,
                  against: [:name, :father_last_name, :mother_last_name],
                  associated_against: { user: [:first_name, :father_last_name,
                                               :mother_last_name] },
                  ignoring: :accents

  has_many :projects, through: :project_prospects, dependent: :destroy
  .
  .
  .
class User < ActiveRecord::Base
  has_many :project_prospect
  has_many :projects, through: :user_projects, dependent: :nullify
end

The query 查询

search = User.find(id).prospects.select("prospects.*").group("prospects.id")
search = search.search_query("Say my name") if search_full_text.present? 

The group works to delete duplicates in the activerecord but when the full text search is needed I get the next error 该组用于删除活动记录中的重复项,但是当需要全文搜索时,我会收到下一个错误

PG::GroupingError: ERROR:  column "pg_search_ece0055f6ad1de91cd168e.rank" must appear in 
the GROUP BY clause or be used in an aggregate function 

LINE 1: ...168e.pg_search_id GROUP BY prospects.id  ORDER BY pg_search_...

After hours searching the only solution for this problem was make the group after the full text search and add a variable named 'rank' to the query in the SELECT, like this: 在数小时后搜索此问题的唯一解决方案是在全文搜索后创建组,并在SELECT中向查询添加名为“rank”的变量,如下所示:

search = User.find(id).prospects
if search_full_text.present? 
    search = search.search_query("Say my name")
                   .select("prospects.*").group("prospects.id, rank")
else
    search = search.select("prospects.*").group("prospects.id")
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM