简体   繁体   English

如何使用Hibernate Lucene搜索完整词组和单个词条?

[英]How to search a full phrase along with single terms using Hibernate Lucene?

Earlier we searching like "Father David Azrael". 之前,我们的搜索方式类似于“父亲David Azrael”。 It returns all results names with term Father or David or Azrael. 它返回所有带有“父亲”或“大卫”或“阿兹雷尔”的结果名称。

Following was code in picture: 以下是图片中的代码:

String[] FIELDS =  { "id", "person.firstName", "person.middleName", "person.lastName" };
for (String field : FIELDS) {
           for (String match : pattern) {     
                   bool.should(qb.keyword().wildcard().onField(field).matching(match).createQuery());
           }
        }
        query = bool.createQuery();

Along with above scenario, now there is additional requirement that if end user want to search "Father David Azrael"; 除了上述情况外,现在还有其他要求,即最终用户要搜索“父亲David Azrael”; he should get one result only. 他应该只会得到一个结果。

Means if we search "Father David Azrael"; 意味着如果我们搜索“父亲David Azrael”; then only results having this name should appears but if user search "Father Azrael" then it should work as working now. 那么仅应显示具有该名称的结果,但如果用户搜索“父亲阿兹雷尔”,则该结果应可立即使用。

So what changes need to be done with above code to implement the same? 那么,为了实现相同的功能,需要对上述代码进行哪些更改?

You can use a phrase query, as shown in section 5.1.2.4 of the documentation: 您可以使用短语查询,如文档的5.1.2.4部分所示:

bool.should(qb
    .phrase()
    .onField(field)
    .sentence("Father David Azrael")
    .createQuery());

I don't know what you mean about "Father David Azrael" being treated differently than "Father Azrael", so I won't comment on that part, but that's how you would create a phrase query. 我不知道您对“父亲大卫·阿兹雷尔”和“父亲·阿兹雷尔”的区别对待是什么意思,所以我不会在那部分发表评论,但这就是您创建短语查询的方式。 You could combine the phrase with the existing wildcard queries by adding both to your boolean query to get both sets of results, with the phrase matches generally receiving the highest scores. 您可以将词组与现有的通配符查询结合起来,方法是将两者都添加到布尔查询中以获取两组结果,而词组匹配通常会获得最高分。 Or you could just run the phrase query first, and fall back on the wildcard queries when that gets no results. 或者,您可以先运行短语查询,然后在没有结果的情况下退回到通配符查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM