首页 > 解决方案 > 当用户可以在 Postgresql 中搜索部分名称或拼写错误时如何搜索书籍

问题描述

我正在为书店的用户提供基于网络的搜索。用户可以在单个字段中输入书名、作者或 ISBN。如果在标题(书名)中给出错误的拼写,搜索会出现问题,例如如果搜索“美国人民”,它将搜索完美但“美国人民”或“Amreka 人民”或“人民”美国”不会给我准确的结果。

我通过 Spring Boot java 应用程序调用查询,作为 hack,我分三个阶段进行。如果第一个查询有 0 个结果,那么我移到第二个,如果第二个有 0 个结果,那么我移到第三个。

我一直在尝试诸如模式匹配和 pg_trgm(Postgres trigrams)之类的东西,但没有成功。任何帮助将不胜感激。

第一个查询是:

    select distinct * from (select title, author, publisher, isbn, edition, 
      book_type, binding, price, image_isbn, special_price, 
      weight,currency_conversion.currency_name, 
      currency_conversion.abbreviation, currency_conversion.conversion_rate 
   from books inner join currency_conversion on currency_conversion.id = 
   books.currency_id where title ILIKE 'People of America' or isbn ILIKE 
   'People of America' or author ILIKE 'People of America') as ab;

第二个查询是:

   select distinct * from (select title, author, publisher, isbn, edition, 
   book_type, binding, price, image_isbn, 
    special_price, weight,currency_conversion.currency_name, 
currency_conversion.abbreviation, currency_conversion.conversion_rate from books 
  inner join currency_conversion 
  on currency_conversion.id = books.currency_id where title ilike '%People of America%' or isbn ILIKE '%People of America%' or author ilike '%People of America%') as ab 

第三个查询是:

select distinct * from (select title, author, publisher, isbn, edition, book_type, binding, price, image_isbn, special_price, 
weight,currency_conversion.currency_name, currency_conversion.abbreviation, currency_conversion.conversion_rate from books inner join currency_conversion 
on currency_conversion.id = books.currency_id where title % 'People of America' or title ilike '%People of America%' 
or isbn ILIKE 'People of America' or author % 'People of America') as ab

标签: sqlpostgresqlspring-boot

解决方案


您是否尝试使用 pg_trgm 模块的 word_similarity 功能?

select word_similarity('The People of The America', 'People America');

result:
0.681818

您可以设置相似度阈值,例如:>0.5

或者您应该看到 FTS 选项https://www.postgresql.org/docs/10/textsearch.html


推荐阅读