Hibernate 搜索过滤器不适用于标记化字段

发布于 2024-12-07 01:21:45 字数 932 浏览 2 评论 0原文

我正在使用 Hibernate search 3.3 来搜索 Lucene 索引。我有一个需要在某个字段上使用的过滤器,但也将其标记为全文搜索。我发现,当我将字段设置为 UN_TOKENIZED 时,过滤器起作用,而全文搜索不起作用;当我将其设置为 TOKENIZED 时,全文搜索起作用,而过滤器不起作用。

POJO

@Field(name = "owningOrganization", index = Index.UN_TOKENIZED, store = Store.YES)
@FieldBridge(impl = OrganizationNameFieldBridge.class)
public Organization getOwningOrganization()
{

过滤器

@Factory
public Filter getFilter()
{
    BooleanQuery query = new BooleanQuery();
    Term orgTerm = new Term("owningOrganization", userOrganization);
    Term activeTerm = new Term("currentStateIsActive", "1");
    query.add(new TermQuery(orgTerm), Occur.SHOULD);
    query.add(new TermQuery(activeTerm), Occur.SHOULD);
    return new CachingWrapperFilter(new QueryWrapperFilter(query));
}

当我输入 owningOrganization:"这是组织的确切值" 的搜索词时,我没有得到任何结果,但过滤器按预期工作。当我将其切换为代币化时,情况正好相反。

有什么建议吗?

I am using Hibernate search 3.3 to search a Lucene index. I have a filter I need to use on a certain field, but also make it TOKENIZED for full text search. What I have found is that when I have the field set to UN_TOKENIZED, the filter works and full text search doesn't, and when I have it set to TOKENIZED, full text search works and the filter doesn't.

POJO

@Field(name = "owningOrganization", index = Index.UN_TOKENIZED, store = Store.YES)
@FieldBridge(impl = OrganizationNameFieldBridge.class)
public Organization getOwningOrganization()
{

Filter

@Factory
public Filter getFilter()
{
    BooleanQuery query = new BooleanQuery();
    Term orgTerm = new Term("owningOrganization", userOrganization);
    Term activeTerm = new Term("currentStateIsActive", "1");
    query.add(new TermQuery(orgTerm), Occur.SHOULD);
    query.add(new TermQuery(activeTerm), Occur.SHOULD);
    return new CachingWrapperFilter(new QueryWrapperFilter(query));
}

When I enter search terms of owningOrganization:"This is the exact value of an organization" I get no results, but the filter works as desired. And opposite when I switch it to TOKENIZED.

Any suggestions?

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

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

发布评论

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

评论(1

寒尘 2024-12-14 01:21:45

您应该对该字段建立索引两次,一次用于搜索(分析),一次用于过滤。默认情况下,使用TermQuery不会应用任何分析。它在索引中搜索指定的术语。

@Fields({
   @Field(name = "owningOrganization_untokenized", index = Index.UN_TOKENIZED),
   @Field(name = "owningOrganization", index = Index.TOKENIZED, store = Store.YES
   }
)
public Organization getOwningOrganization(){
}

通过这种方法,您可以拥有可用标记化和非标记化的所属组织字段。您只需使用正确的字段名称即可。

You should index the field twice, once for searching (analyzed) and once for filtering. Per default using a TermQuery does not apply any analyzing. It searches the index for the term as specified.

@Fields({
   @Field(name = "owningOrganization_untokenized", index = Index.UN_TOKENIZED),
   @Field(name = "owningOrganization", index = Index.TOKENIZED, store = Store.YES
   }
)
public Organization getOwningOrganization(){
}

With this approach you have the owning organization field available tokenized and un-tokenized. You just have to use the right field name.

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