休眠搜索-如何获取匹配的字段名称 [英] Hibernate Search- How To get Matching Field Names

查看:135
本文介绍了休眠搜索-如何获取匹配的字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的JSP项目中实现了休眠Lucene搜索.我可以搜索contact_name,contact_email(一对多)等,结果它将成功返回与搜索关键字匹配的联系人列表.但是我的问题是,我想知道匹配项"位于哪个字段(列名).有什么办法可以获取匹配的字段名称和结果.

I have implemented hibernate lucene search in my JSP project. I can search contact_name,contact_email(one To Many),etc.,as a result it will return the list of contacts which matching to the search keyword successfully. But My problem is i want to know in which field(columnn_name) the 'match found'. Is there any way to grab the matching field name along with result.

这就是我编写搜索查询并获得结果的方式

This is how i wrote the search query and getting result

 List<Contact> searchResultContact = new ArrayList<Contact>();
 FullTextSession fullTextSession = Search.getFullTextSession(session);
 QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Contact.class).get();

 org.apache.lucene.search.Query query1 = qb.phrase().withSlop(2)
                    .onField("firstName")
                   .andField("emailDetails.email_id")
                   .boostedTo(5)
                   .sentence(searchText.toLowerCase()).createQuery();
        org.apache.lucene.search.Query query2 = qb.keyword()
                .onField("status")
                .matching("0")
                .createQuery();
        org.apache.lucene.search.Query query = qb
                .bool()
                  .must( query1 )
                  .must( query2)
                .createQuery();

        org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Contact.class);

        searchResultContact = hibQuery.list();

我的联系方式如下

@Entity
@Indexed
@Table(name = "contact")

public class Contact {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "firstName", nullable = false, length = 128)
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO, analyzer = @Analyzer(definition = "ngram"))
private String firstName;

@OneToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "contact_id")
@IndexedEmbedded
List<EmailDetails> emailDetails;

}

先谢谢您.

推荐答案

有什么方法可以获取匹配的字段名称和结果.

Is there any way to grab the matching field name along with result.

在Hibernate Search中没有内置的方法可以做到这一点.也许可以通过深入研究Lucene的内部方法来做到这一点,但是除非您对性能的要求很高,否则我不建议您这样做.

There is no built-in way to do that in Hibernate Search, no. There might be a way to do it by diving deep into Lucene's internals, but I wouldn't recommend that unless you have very heavy performance requirements.

不,实际上,最简单的方法是自己完成.而不是运行单个查询,而是运行三个:

No, really, the easiest way to do this is to do it yourself. Instead of running a single query, run three:

  1. 带有标题或文字"过滤器的人
  2. 一个仅在"firstName"字段上具有过滤器,而一个ID上仅用于考虑与第一个查询匹配的实体的过滤器
  3. 仅在"emailDetails.email_id"字段上使用过滤器,对ID进行过滤以仅考虑与第一个查询匹配的实体

然后,第一个查询为您提供结果,第二个查询为您提供在"firstName"字段上匹配的实体的列表,第三个查询为您提供在"emailDetails.email_id"字段上匹配的实体的列表

Then the first query gives you the results, the second query gives you the list of entities that matched on the "firstName" field, and the third query gives you the list of entities that matched on the "emailDetails.email_id" field.

相关问题: https://stackoverflow.com/a/47832433/6692043 .

这篇关于休眠搜索-如何获取匹配的字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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