sql - 用于获取与给定列表相似的相似列表的 Spring JPA 查询
问题描述
我有一个名为Product
. 它还链接到product
数据库表。数据库服务器是 PostgreSQL。我正在使用 Spring Boot 和 Hibernate。
public class Product{
@ToString.Exclude
@ElementCollection
@Nullable
private List<String> labels;
}
该类Product
具有labels
表示照片中对象的字段product
。例如,
List<String> labels = ["apple", "table", "phone"]
我的主要目的是我想编写一个 SQL 查询来获取所有具有“相似标签”的产品。
什么是我的“相似标签”?例如,letlabels1
表示 的标签,product1
letlabels2
表示 的标签product2
。
List<String> labels1 = ["apple", "table", "phone"]
List<String> labels2 = ["apple"]
两个列表中只有 1 项是 "apple"
相同。所以sameItemCounter
等于1
。
两个列表中的总不同项目列表等于,
distinctItemList = ["apple","table","phone"]
所以,distinctItemCounter
等于3
。
如果100*sameItemCounter/distincItemCounter
大于 30 then ,则两个标签列表是“相似标签”。
所以在这种情况下,100*sameItemCounter/distincItemCounter
is 等于33,3
大于30
,那么我们可以说labels1
和labels2
是“相似标签”。所以,相对而言,我们可以说product1
和product2
是“同类产品”。
虽然我可以通过获取数据库中的所有产品而不是在 Spring Boot(后端)中创建所有进程来实现这一点,但我想通过数据库查询来完成所有这些进程。
我想要一个类似于这种形式的答案:
public static final String GET_ALL_SIMILAR_PRODUCTS = "query??";
@Query(value = FIND_PROJECTS, nativeQuery = true)
public List<Product> getAllSimilarProducts(Product p); // need to return all "similar products" of p.
我怎样才能做到这一点?
解决方案
推荐阅读
- python - 所有时期的变量都相同
- javascript - Angular 7 使用 ng build --prod 缩小资产
- jestjs - 尝试运行 jest 的 jest 版本不正确
- c# - 根据XSI类型的attrubutes过滤掉XML文件中的属性
- c# - 发布 web 应用程序后,session.timeout 自动设置为默认值?
- c# - 如何列出我的解决方案中的所有 DLL 并获取它们的版本?
- service-worker - Service Worker 注册成功后出现“bad-precaching-response”错误
- java - Springboot 下拉菜单
- java - 在android中逐行读取文件后如何在UI上显示SetText值?
- microsoft-graph-api - Planner 任务优先级的 Graph API 何时可用?