sql - 当用户可以在 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
解决方案
您是否尝试使用 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
推荐阅读
- php - Laravel 自定义字符串帮助文件
- .net-core - 使用 RabbitMQ 发布/订阅微服务事件总线时如何去重事件
- visual-studio-code - 如何在代码编辑器和我的 Nativescript 项目中解决这个新的 CCS 错误
- apache-zeppelin - 在 docker 映像中运行 zeppelin 时如何为解释器设置用户解释?
- google-sheets - 在 Google 表格中将文本重新格式化为数字的更简单方法
- python - 在python中用gdal读取ecw文件?
- css - CSS获取类的最后一个孩子
- terraform - 使用版本控制创建 GCS 存储桶
- javascript - 使用 PDF.js 异步加载多个 pdf 文件时获取总页数
- gradle - 从 ktor 提供 kotlin 多平台 javascript