Search code examples
postgresqlfull-text-searchstemming

Prevent stemming of words starting with # in PostgreSQL full text search


Basically, I want to be able to get an exact match (hashtag included) for queries like this:

=#SELECT to_tsvector('english', '#adoption');
 to_tsvector
-------------
 'adopt':1

Instead, I want for words starting with #, to see:

=#SELECT to_tsvector('english', '#adoption');
 to_tsvector
-------------
 '#adoption':1

Is this possible with psql full text search?


Solution

  • Before you search or index, you could replace each # character with some other character that you don't use in your texts, but which changes the parser's interpretation:

    test=> SELECT alias, lexemes FROM ts_debug('english', '#adoption');
    ┌───────────┬─────────┐
    │   alias   │ lexemes │
    ├───────────┼─────────┤
    │ blank     │         │
    │ asciiword │ {adopt} │
    └───────────┴─────────┘
    (2 rows)
    
    test=> SELECT alias, lexemes FROM ts_debug('english', '/adoption');
    ┌───────┬─────────────┐
    │ alias │   lexemes   │
    ├───────┼─────────────┤
    │ file  │ {/adoption} │
    └───────┴─────────────┘
    (1 row)