postgresql 通配符搜索和排名未按预期工作
postgresql Wildcard search and ranking not working as expected
我有这个 tsvector 列用于全文搜索两列的名称和地址。
ALTER TABLE public.business_info ADD column search_vector tsvector NULL GENERATED ALWAYS AS (setweight(to_tsvector('simple'::regconfig, COALESCE(name, ''::text)), 'A'::"char") || setweight(to_tsvector('simple'::regconfig, COALESCE(address, ''::text)), 'B'::"char")) STORED
我正在尝试使用以下查询来查找具有全文通配符搜索的记录
SELECT name,ts_rank_cd(search_vector, to_tsquery('Oklahoma&State:*')) as rank
from business_info order by rank desc
但我得到的结果如下所示,尽管我很期待,名称为“Oklahoma state”的记录显示了更高的排名。知道如何对这些结果进行排名以获得对用户提供的输入具有更高排名的结果吗?
您可以使用 strpos 查找字符串中另一个字符串出现的时间。但是如果substrig根本没有出现,你得到0,所以你可能需要特殊处理:
ORDER BY nullif(strpos(name,'Oklahoma state'),0);
请注意,这将无法对您的查询仍然找到的内容进行排名,例如 'state quick red fox jumps over Oklahoma'。使用 FTS 进行匹配,但对排名的子字符串查询不一致,也许你应该对两者都使用子字符串。
如果您对字符串起始位置的高分感兴趣,只需为这部分定义一个表达式。
这是前两个词的例子
select
coalesce((string_to_array(name, ' '))[1],' ')||' ' || coalesce((string_to_array(name, ' '))[2],' ') as name_pfx,
name
from business_info;
name_pfx, name
Southwest Oklahoma Southwest Oklahoma State University at Oklahoma State University-OKC
比为此前缀定义更高的权重,例如
ALTER TABLE business_info
ADD column search_vector tsvector NULL GENERATED ALWAYS AS
(setweight(to_tsvector('simple'::regconfig,
COALESCE((coalesce((string_to_array(name, ' '))[1],' ')||' ' || coalesce((string_to_array(name, ' '))[2],' ')), ''::text)), 'A'::"char") ||
setweight(to_tsvector('simple'::regconfig, COALESCE(name, ''::text)), 'B'::"char")) stored;
结果符合预期
SELECT name_pfx||' '||name name,ts_rank_cd(search_vector, to_tsquery('Oklahoma&State:*')) as rank
from business_info order by rank desc
"rank", "name"
2.1714287 Oklahoma State Oklahoma State University - Oklahoma City
2.0714285 Oklahoma State Oklahoma State University at Western Oklahoma
0.93333334 Southwest Oklahoma Southwest Oklahoma State University at Oklahoma State University-OKC
0.90000004 Southwest Oklahoma Southwest Oklahoma State University at Northwest Oklahoma State University-A
0.90000004 Western Oklahoma Western Oklahoma State College at Western Oklahoma State College
我有这个 tsvector 列用于全文搜索两列的名称和地址。
ALTER TABLE public.business_info ADD column search_vector tsvector NULL GENERATED ALWAYS AS (setweight(to_tsvector('simple'::regconfig, COALESCE(name, ''::text)), 'A'::"char") || setweight(to_tsvector('simple'::regconfig, COALESCE(address, ''::text)), 'B'::"char")) STORED
我正在尝试使用以下查询来查找具有全文通配符搜索的记录
SELECT name,ts_rank_cd(search_vector, to_tsquery('Oklahoma&State:*')) as rank
from business_info order by rank desc
但我得到的结果如下所示,尽管我很期待,名称为“Oklahoma state”的记录显示了更高的排名。知道如何对这些结果进行排名以获得对用户提供的输入具有更高排名的结果吗?
您可以使用 strpos 查找字符串中另一个字符串出现的时间。但是如果substrig根本没有出现,你得到0,所以你可能需要特殊处理:
ORDER BY nullif(strpos(name,'Oklahoma state'),0);
请注意,这将无法对您的查询仍然找到的内容进行排名,例如 'state quick red fox jumps over Oklahoma'。使用 FTS 进行匹配,但对排名的子字符串查询不一致,也许你应该对两者都使用子字符串。
如果您对字符串起始位置的高分感兴趣,只需为这部分定义一个表达式。
这是前两个词的例子
select
coalesce((string_to_array(name, ' '))[1],' ')||' ' || coalesce((string_to_array(name, ' '))[2],' ') as name_pfx,
name
from business_info;
name_pfx, name
Southwest Oklahoma Southwest Oklahoma State University at Oklahoma State University-OKC
比为此前缀定义更高的权重,例如
ALTER TABLE business_info
ADD column search_vector tsvector NULL GENERATED ALWAYS AS
(setweight(to_tsvector('simple'::regconfig,
COALESCE((coalesce((string_to_array(name, ' '))[1],' ')||' ' || coalesce((string_to_array(name, ' '))[2],' ')), ''::text)), 'A'::"char") ||
setweight(to_tsvector('simple'::regconfig, COALESCE(name, ''::text)), 'B'::"char")) stored;
结果符合预期
SELECT name_pfx||' '||name name,ts_rank_cd(search_vector, to_tsquery('Oklahoma&State:*')) as rank
from business_info order by rank desc
"rank", "name"
2.1714287 Oklahoma State Oklahoma State University - Oklahoma City
2.0714285 Oklahoma State Oklahoma State University at Western Oklahoma
0.93333334 Southwest Oklahoma Southwest Oklahoma State University at Oklahoma State University-OKC
0.90000004 Southwest Oklahoma Southwest Oklahoma State University at Northwest Oklahoma State University-A
0.90000004 Western Oklahoma Western Oklahoma State College at Western Oklahoma State College