gpt4 book ai didi

Postgresql - 全文搜索索引 - 意外的查询结果

转载 作者:行者123 更新时间:2023-11-29 13:29:05 24 4
gpt4 key购买 nike

我有一张 table ,上面有一堆列我在这样的表上创建了一个全文索引:

CREATE INDEX phrasetable_exp_idx ON msc.mytable 
USING gin(to_tsvector('norwegian', coalesce(msc.mytable.col1,'') || ' ' ||
coalesce(msc.mytable.col2,'') || ' ' ||
coalesce(msc.mytable.col3,'') || ' ' ||
coalesce(msc.mytable.col4,'') || ' ' ||
coalesce(msc.mytable.col5,'') || ' ' ||
coalesce(msc.mytable.col6,'') || ' ' ||
coalesce(msc.mytable.col7,'')));

我尝试了一些搜索,它们的速度快如闪电,但是,对于一个特定的搜索,我没有得到预期的结果。我的表中有一行 col1 和 col2 都有确切的值“Importkompetanse Oslo AS”在 col3 中,它的值为“9999”。只有查询 to_tsquery('9999') 返回该行,这表明它在 col1 和 col2 中确实具有值“Importkompetanse Oslo AS”,但前两个查询不返回任何匹配项。

SELECT *
FROM msc.mytable
WHERE to_tsvector('norwegian', coalesce(msc.col1,'') || ' ' ||
coalesce(msc.mytable.col2,'') || ' ' ||
coalesce(msc.mytable.col3,'') || ' ' ||
coalesce(msc.mytable.col4,'') || ' ' ||
coalesce(msc.mytable.col5,'') || ' ' ||
coalesce(msc.mytable.col6,'') || ' ' ||
coalesce(msc.mytable.col7,'')));
@@ --to_tsquery('Importkompetanse&Oslo&AS') -- nada
plainto_tsquery('Importkompetanse') -- nada
--to_tsquery('9999') -- OK!

有谁知道为什么我的搜索没有结果?

编辑:

出于某种原因,to_tsquery 返回如下内容:“'9999':9'importkompetans':1,6”importkompetanse这个词好像被砍掉了?

但是,如果我将它设置为简单而不是挪威语,我会得到预期的结果并且一切看起来都不错。这是为什么?

最佳答案

您在 tsvectortsquery 值之间使用了交叉配置。您应该使用一致的配置,例如:

select to_tsvector('norwegian', 'Importkompetanse Oslo AS')
@@ to_tsquery('norwegian', 'Importkompetanse&Oslo&AS');

SQLFiddle

这就是它使用 'simple' 配置(即您的 default )的原因。

注意:您可以随时 debug使用 ts_debug() 进行文本搜索:f.ex。 'Importkompetanse' 没有被截断,'importkompetans' 只是这个词的合适词位(在 'norwegian' 配置中)。

关闭:您使用了一个非常长的、基于表达式的索引,只有当您在查询中也使用了精确的表达式时,才会使用该索引。您在您的示例中正确使用了它,但这会使您的查询变得非常长,并且如果您稍后更改索引表达式,您需要确保所有“使用”也更新。

您可以使用一个简单的 (sql) 函数来简化您的查询:

create or replace function col_tsvector(mytable)
returns tsvector
immutable
language sql
as $function$
return to_tsvector('norwegian',
coalesce($1.col1, '') || ' ' ||
coalesce($1.col2, '') || ' ' ||
coalesce($1.col3, '') || ' ' ||
coalesce($1.col4, '') || ' ' ||
coalesce($1.col5, '') || ' ' ||
coalesce($1.col6, '') || ' ' ||
coalesce($1.col7, ''))
$function$;

有了它,您也可以大大简化您的索引定义和查询。 (您甚至可以使用 attribute notation 。)

关于Postgresql - 全文搜索索引 - 意外的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28057514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com