开发者

Hibernate Search Filter not working with Tokenized field

开发者 https://www.devze.com 2023-04-08 16:43 出处:网络
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 f

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_T开发者_C百科OKENIZED, 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消