Hibernate全文搜索自定义顺序由 [英] Hibernate full text search custom order by

查看:69
本文介绍了Hibernate全文搜索自定义顺序由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们要添加自定义顺序以休眠全文搜索,假设我们要根据位置搜索记录,如果位置是国家,州,城市"

We want to add custom order by to hibernate full text search,suppose we want to search the record based on location, if the location is "country,state,city

然后我们要进行搜索,使顶部的记录与用户接近

Then we want to have the search with records in the top which are near to user

我们关注了以下链接.

使用mysql按情况排序"处于休眠状态

但是,当我们将order by子句添加到标准对象中时,并不会添加该子句

But the order by clause is not getting added when we add it to criteria object

仅当我们将文本查询对象设置为如下所示时,排序才有效,在这里我们只能执行asc和desc,而不能进行动态排序.

Sort is working only when we set to full Text Query object like below, here we can do only asc and desc, dynamic order by not.

Sort sort = new Sort( new SortField( "location", SortField.Type.STRING, false ) );
fullTextQuery.setSort( sort );

这是基于地理位置的搜索.

It is geo location based search.

我们在mysql表列中将位置值存储为印度,卡纳塔克邦,班加罗尔",假设来自班加罗尔登录名的用户需要在顶部显示来自班加罗尔的记录.

we are storing the location value in mysql table column as "India,Karnataka,Bangalore" suppose user from Bangalore logins we need to show the records which are from Bangalore first at the top.

如果我们获得有关此问题的任何信息,或解决此问题的任何其他方法,将很有帮助.

It would be helpful if we get some information regarding this, or any other way to achieve this problem.

谢谢,莫欣

推荐答案

听起来您不想按字面意思对这些结果排序",但是您希望与位置匹配的结果排名很高,因此在执行按分数排序的查询时,它们会排在最前面.

It sounds like you don't want to literally 'sort' those results but you want the results which match the location to be ranked very high, so to get them on top when performing a query sorted by score.

默认情况下,记住全文搜索将返回按相关性排序的结果,因此将首先返回与查询最相似"的匹配项.您可以控制相似性"的概念-这就是使用比关系查询更强大的全文引擎的关键所在.

Remember a full-text search by default will return results sorted by relevancy, so the matches which are most similar to the query will be returned first. The concept of "similarity" is something which you can control - and that's the point of using a more powerful full-text engine than a relational query.

因此,您可以通过在 SHOULD 运算符中添加一个布尔值子句,并为其赋予强的提升得分,从而获得匹配班加罗尔"的位置"与位置"匹配的结果

So you could get your results which have "location" matching "Bangalore" by adding a Boolean clause with the SHOULD operator, and giving this a strong boost in scoring.

例如,您可以:

Query combinedQuery = querybuilder
   .bool()
      .must( originalQuery )
      .should( querybuilder.keyword().onField("location").matching(place_keyword).createQuery(); )
.createQuery();

请参阅文档的以下部分:

See these section of the documentation:

  • Boosting to give the "location" field a higher weight in score computation
  • Combining queries

您可以通过使用带有查询的投影来验证如何计算增重和权重.您可以让它产生简单的分数,甚至可以完全解释为什么该分数具有该价值;例如:

You can verify how the boosting and weights are calculated by using projections with queries. You can have it produce the simple score, or even a full explanation on why that score got that value; for example:

org.hibernate.search.FullTextQuery query =
   s.createFullTextQuery(luceneQuery, YourEntity.class);
query.setProjection(
   FullTextQuery.SCORE,
   FullTextQuery.EXPLANATION,
   FullTextQuery.THIS);

List results = query.list();
Object[] firstResult = (Object[]) results.get(0);
float score = firstResult[0];
String explanation = firstResult[1];
YourEntity o = (YourEntity) firstResult[2];

最后,您还可以根据自己的喜好更改评分公式,或阅读有关

Finally, you can also change the scoring formula to your liking, or read about the default Similarity implementation to help understand the scoring system and the "explanation" formula.

这篇关于Hibernate全文搜索自定义顺序由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆