Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PostgreSQL查找类似帖子_Postgresql_Search_Pattern Matching - Fatal编程技术网

使用PostgreSQL查找类似帖子

使用PostgreSQL查找类似帖子,postgresql,search,pattern-matching,Postgresql,Search,Pattern Matching,我有一张表posts: CREATE TABLE posts ( id serial primary key, content text ); 当用户提交帖子时,我如何将他的帖子与其他帖子进行比较并找到类似的帖子? 我正在寻找类似StackOverflow处理“类似问题”的方法。您需要在Postgres中使用全文搜索 虽然是一个选项,但它主要不适用于这种类型的搜索。典型的用例是根据字典和词干分析在文档中查找单词,而不是比较整个文档 我确信StackOverflow在相似性搜索中发挥了一

我有一张表
posts

CREATE TABLE posts (
  id serial primary key,
  content text
);
当用户提交帖子时,我如何将他的帖子与其他帖子进行比较并找到类似的帖子?

我正在寻找类似StackOverflow处理“类似问题”的方法。

您需要在Postgres中使用全文搜索

虽然是一个选项,但它主要不适用于这种类型的搜索。典型的用例是根据字典和词干分析在文档中查找单词,而不是比较整个文档

我确信StackOverflow在相似性搜索中发挥了一些作用,因为这不是一件小事

您可以使用模块提供的功能获得一半的满意结果:

为此,请务必启用
内容

但你可能需要做更多的事情。在识别新内容中的关键字后,您可以将其与文本搜索相结合

SELECT content, similarity(content, 'grand new title asking foo') AS sim_score
FROM   posts
WHERE  content  % 'grand new title asking foo'
ORDER  BY 2 DESC, content;