Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: FullTextSearch query filtered by list of ids
PostPosted: Fri Mar 09, 2012 1:02 pm 
Newbie

Joined: Wed Mar 16, 2011 6:07 pm
Posts: 9
Hi,

I need to be able to apply filter to fullTextSearch query injecting a collection as parameter. Basically something like "IN (1,2,3)" clause in SQL. Is possible to achieve that with a filter?

In case that can't be injected a collection as parameter, is possible to to enable multiple times same filter with different parameter?

Code:
query.enableFullTextFilter( "objectId" ).setParameter( "id", 1);
query.enableFullTextFilter( "objectId" ).setParameter( "id", 2);



I'm supposing that filters are the natural way to make this restriction, what more options I have to apply collection restriction?


Top
 Profile  
 
 Post subject: Re: FullTextSearch query filtered by list of ids
PostPosted: Mon Mar 12, 2012 7:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

a filter seems to be a good choice for your usecase. The API to provide a parameter to a FulltextFilter is FullTextFilter setParameter(String name, Object value);.
This means you can pass any object you like as parameter, you just have to cast the parameter in the filter implementation.

--Hardy


Top
 Profile  
 
 Post subject: Re: FullTextSearch query filtered by list of ids
PostPosted: Tue Apr 24, 2012 1:37 pm 
Newbie

Joined: Wed Mar 16, 2011 6:07 pm
Posts: 9
Hi Hardy,

Thank you for the answer.

What is the proper way to implement filter for collections?
Maybe using the booleanQuery and add every item into boolean query? If this is the case, what have to be used BooleanClause.Occur.MUST or BooleanClause.Occur.SHOULD?

Code:
TermQuery termQuery = new TermQuery(new Term("anotherentity.id", id + ""));
booleanQuery.add(termQuery , BooleanClause.Occur.MUST);


Top
 Profile  
 
 Post subject: Re: FullTextSearch query filtered by list of ids
PostPosted: Wed Apr 25, 2012 10:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

BooleanQuery sounds good. MUST vs SHOULD depends on your usecase.


Top
 Profile  
 
 Post subject: Re: FullTextSearch query filtered by list of ids
PostPosted: Thu Nov 08, 2012 1:13 am 
Newbie

Joined: Thu Nov 08, 2012 1:06 am
Posts: 1
Hi,

If you are looking for OR then "Should" would work for you.

Thanks

package com.store.filter;

import java.util.List;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.CachingWrapperFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.TermQuery;
import org.hibernate.search.annotations.Factory;
import org.hibernate.search.annotations.Key;
import org.hibernate.search.filter.FilterKey;
import org.hibernate.search.filter.StandardFilterKey;

/**
* FullTextFilter with multiple ids
*
* @author actolap
*
*/
public class SourceFilter {

private List<Integer> ids;

public List<Integer> getIds() {
return ids;
}

public void setIds(List<Integer> ids) {
this.ids = ids;
}

@Key
public FilterKey getKey() {
StandardFilterKey key = new StandardFilterKey();
key.addParameter(ids);
return key;
}

@Factory
public Filter getFilter() {
BooleanQuery booleanQuery = new BooleanQuery();
TermQuery termQuery = null;
for (Integer id : ids) {
termQuery = new TermQuery(new Term("source.id", id.toString()));
booleanQuery.add(termQuery, BooleanClause.Occur.SHOULD);
}
return new CachingWrapperFilter(new QueryWrapperFilter(booleanQuery));
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.