Skip to content

Searching for email addresses #224

@Jaco-Pretorius

Description

@Jaco-Pretorius

I'm trying to search for users based on either name or email address. As far as I can tell postgresql interprets the '@' symbol as a special character and breaks my search.

Here is my setup on the search:

  pg_search_scope :search_by_name_and_email,
    against: { name: "A", email: "B" },
    using: { tsearch: { prefix: true }}

When I pass through a query of 'user@ex' I get the following sql:

SELECT "users".*, ((ts_rank((setweight(to_tsvector('simple', coalesce("users"."name"::text, '')), 'A') || setweight(to_tsvector('simple', coalesce("users"."email"::text, '')), 'B')), (to_tsquery('simple', ''' ' || 'user@ex' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "users"  WHERE (((setweight(to_tsvector('simple', coalesce("users"."name"::text, '')), 'A') || setweight(to_tsvector('simple', coalesce("users"."email"::text, '')), 'B')) @@ (to_tsquery('simple', ''' ' || 'user@ex' || ' ''' || ':*'))))  ORDER BY pg_search_rank DESC, "users"."id" ASC

It seems like postgresql is breaking up my query at the '@' token, based on the following:

select * from ts_parse('default', 'user@ex');
 tokid | token 
-------+-------
     1 | user
    12 | @
     1 | ex
(3 rows)
select * from ts_debug('simple', 'user@ex');
   alias   |   description   | token | dictionaries | dictionary | lexemes 
-----------+-----------------+-------+--------------+------------+---------
 asciiword | Word, all ASCII | user  | {simple}     | simple     | {user}
 blank     | Space symbols   | @     | {}           |            | 
 asciiword | Word, all ASCII | ex    | {simple}     | simple     | {ex}
(3 rows)

Should I be escaping my query in some way, or does pg_search simply not support searching by fields which contain an '@' symbol? (I'm guessing we would have the same problem with other 'special' characters in posgresql, such as '&', '|' or '_'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions