JPQL:按“最佳匹配”对查询结果进行排序可能的?

发布于 2024-12-18 02:21:40 字数 475 浏览 3 评论 0原文

我有以下问题:

我正在使用 JPQL(JPA 2.0 和 eclipselink),我想创建一个查询,为我提供按以下方式排序的结果: 首先,结果按最佳匹配升序排序。之后应该会出现较差的比赛。 我的对象基于一个名为“Person”的简单类,其属性如下:

{String Id,
String forename,
String name}

例如,如果我正在搜索“Picol”,结果应如下所示:

[{129, Picol, Newman}, {23, Johnny, Picol},{454, Picolori, Newta}, {4774, Picolatus, Larimus}...]

PS:我已经考虑过使用两个查询,第一个是使用“equals”进行搜索”,第二个是“like”,尽管我不太确定如何连接两个查询结果......?

希望您的帮助并提前致谢, 弗洛里安

I have the following question/problem:

I'm using JPQL (JPA 2.0 and eclipselink) and I wanna create a query that gives me the results sorted the following way:
At first the results sorted ascending by the best matches. After that should appear the inferior matches.
My objects are based on a simple class called 'Person' with the attributes:

{String Id,
String forename,
String name}

For example if I'm searching for "Picol" the result should look like:

[{129, Picol, Newman}, {23, Johnny, Picol},{454, Picolori, Newta}, {4774, Picolatus, Larimus}...]

PS: I already thought about using two queries, the first is searching with "equals" and the second with "like", although I'm not quite sure how to connect both queryresults...?

Hope for your help and thanks in advance,
Florian

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

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

发布评论

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

评论(1

伴我心暖 2024-12-25 02:21:40

如果,正如您的问题似乎暗示的那样,您只有两个组(第一组:名字或名称等于搜索字符串;第二组:名字或名称包含搜索字符串),并且如果给定组的所有人员都具有相同的“匹配” Score”,那么使用两个查询确实是一个很好的解决方案。

第一个查询:

select p from Person p where p.foreName = :param or p.name = :param

第二个查询:

select p from Person p where (p.foreName like :paramSurroundedWithPercent 
                              or p.name like :paramSurroundedWithPercent)
                             and p.foreName != :param 
                             and p.name != :param

执行两个查询(每个查询返回一个 List),并将第二个列表的所有元素添加到第一个列表中(使用 addAll()代码>方法)

If, as your question seem to imply, you only have two groups (first group : forename or name equals searched string; second group : forename or name contains searched string), and if all the persons of a given group have the same "match score", then using two queries is indeed a good solution.

First query :

select p from Person p where p.foreName = :param or p.name = :param

Second query :

select p from Person p where (p.foreName like :paramSurroundedWithPercent 
                              or p.name like :paramSurroundedWithPercent)
                             and p.foreName != :param 
                             and p.name != :param

Execute both queries (each returning a List<Person>), and add all the elements of the second list to the first one (using the addAll() method)

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