在Postgres全文搜索中搜索最接近的正确单词

发布于 2025-02-07 20:58:36 字数 469 浏览 3 评论 0 原文

我的应用程序中有一个搜索功能,用。 我想以“你的意思...?”的方式添加拼写错误的更正。

到目前为止,我已经看到您可以与。这与我想要的完全不匹配,是我想要校正可能的缺陷输入的建议。仅仅获得相似之处,假设我已经知道正确拼写的单词的含义。

Postgres可以这样做吗?一个例子是将“ Borritoh”纠正为“墨西哥卷饼”。

I have a search-functionality in my app done with https://www.postgresql.org/docs/current/textsearch.html.
I want to add the correction of spelling mistakes, in the fashion of "Did you mean ... ?".

So far I have seen that you can get the similarity of words with https://www.postgresql.org/docs/current/pgtrgm.html. This doesn't exactly match what I want, is I want a recommendation for the correction of a possible flawed input. Just getting the similarity assumes I would already know what correctly spelled word is meant.

Can Postgres do this? An example would be correcting "Borritoh" to "Burrito".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心 2025-02-14 20:58:36

只是获得相似性,假设我已经知道正确拼写的单词的含义。

不,您只需要一个公认单词的字典。然后,您以相似性或距离为限制1的词典订购识别单词的字典。

如果您构建要点风味的Trigram索引,它将直接支持订购:

select word from dictionary ORDER BY word <-> 'Borritoh' limit 1

但是,如果识别的单词都不相似,这将非常慢。最好使用将其放置在其上不提出任何建议的地面(这可能会比GIST索引做得更好,您应该两种方法都可以尝试这两种方式) 。

select word from dictionary where word % 'Borritoh' ORDER BY word <-> 'Borritoh' limit 1

Just getting the similarity assumes I would already know what correctly spelled word is meant.

No, you just need a dictionary of recognized words. You then order your dictionary of recognized words by similarity or distance with a LIMIT 1.

If you build the trigram index of the GiST flavor, it will support ordering directly:

select word from dictionary ORDER BY word <-> 'Borritoh' limit 1

However, this will be pretty slow if none of the recognized words are similar. It might be better to use % to put a floor below which it just doesn't make any recommendation (which which will probably be better done with a GIN than GiST index, you should try it both ways).

select word from dictionary where word % 'Borritoh' ORDER BY word <-> 'Borritoh' limit 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文