防止在 PostgreSQL 全文搜索中以 # 开头的单词

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

基本上,我希望能够为这样的查询获得完全匹配(包括主题标签):

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

相反,我想要以#开头的单词,以查看:

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

psql 全文搜索是否可行?

在搜索或索引之前,您可以将每个 # 字符替换为您在文本中不使用的其他字符,但会改变解析器的解释:

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)