Casecommons / pg_search

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search
http://www.casebook.net
MIT License
1.31k stars 370 forks source link

Why add extra space ' ' in generated SQL #389

Open smoothdvd opened 6 years ago

smoothdvd commented 6 years ago

Search result always return empty when I use full text search with Chinese language. PostgreSQL version is 10.5 My pg_search_scope is defined as:

pg_search_scope :search_full_text,
                  against: %i[title],
                  using: {
                    tsearch: {
                      dictionary: 'jiebacfg'
                    }
                  }

Generated SQL:

Notification Load (1.3ms) SELECT "notifications".* FROM "notifications" INNER JOIN (SELECT "notifications"."id" AS pg_search_id, (ts_rank((to_tsvector('jiebacfg', coalesce("notifications"."title"::text, ''))), (to_tsquery('jiebacfg', ''' ' || '大于' || ' ''')), 0)) AS rank FROM "notifications" WHERE (((to_tsvector('jiebacfg', coalesce("notifications"."title"::text, ''))) @@ (to_tsquery('jiebacfg', ''' ' || '大于' || ' '''))))) AS pg_search_8bb6922758ad870f844ad0 ON "notifications"."id" = pg_search_8bb6922758ad870f844ad0.pg_search_id ORDER BY "notifications"."id"DESC, pg_search_8bb6922758ad870f844ad0.rank DESC, "notifications"."id" ASC

If I change ''' ' || '大于' || ' ''' to '大于' and execute the sql in PostgreSQL console, it will return correct result.

lucianosousa commented 5 years ago

Running in a similar issue here

suupic commented 3 years ago

add a monkey path to remove space in SQL

module PgSearch
  module Features
    class TSearch
      def tsquery_expression(term_sql, negated:, prefix:)
        terms = [
          (Arel::Nodes.build_quoted('!') if negated),
          # Arel::Nodes.build_quoted("' "), # comment this line to remove space
          term_sql,
          # Arel::Nodes.build_quoted(" '"), # comment this line to remove space
          (Arel::Nodes.build_quoted(":*") if prefix)
        ].compact

        terms.inject do |memo, term|
          Arel::Nodes::InfixOperation.new("||", memo, Arel::Nodes.build_quoted(term))
        end
      end
    end
  end
end