Hi,
How to make use of Filters in Hibernate search (lucene search).
In additional to keyword search i would like to add some more constraints.
Actually i tried with the filters.
pojo
Code:
@Entity
@Indexed
@FullTextFilterDefs( {
@FullTextFilterDef(name = "nonDeletedPov", impl = NonDeletedPovFilterFactory.class),
})
public class POV implements java.io.Serializable {@Id
@DocumentId
private String povId;
private long version;
@NaturalId
private PAEvent paEvent;
private PAOrganization paOrganization;
@NaturalId
@IndexedEmbedded(depth=1, prefix="portfolio_")
private Portfolio portfolio;
@Field(index=Index.TOKENIZED, store=Store.NO)
private String modifiedBy;
private Character isDeleted;
}
NonDeletedPovFilterFactory.java
Code:
public class NonDeletedPovFilterFactory {
@Factory
public Filter getFilter() {
//some additional steps to cache the filter results per IndexReader
Filter nonDeletedPovFilter = new NonDeletedPovFilter();
return new CachingWrapperFilter(nonDeletedPovFilter);
}
}
NonDeletedPovFilter.java
Code:
public class NonDeletedPovFilter extends org.apache.lucene.search.Filter {
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
OpenBitSet bitSet = new OpenBitSet( reader.maxDoc() );
TermDocs termDocs = reader.termDocs( new Term( "modifiedBy", "Ambika" ) );
while ( termDocs.next() ) {
bitSet.set( termDocs.doc() );
}
return bitSet;
}
}
Example.java
Code:
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
Session session = null;
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
FullTextSession fullTextSession = Search.createFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
MultiFieldQueryParser parser = new MultiFieldQueryParser(
new String[] { "portfolio_portfolioName" },
new StandardAnalyzer());
Query query = parser.parse("Ambika");
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, POV.class);
fullTextQuery.enableFullTextFilter("nonDeletedPov");
org.hibernate.Query hibQuery = (org.hibernate.Query) fullTextQuery.list();
List<POV> result = hibQuery.list();
tx.commit();
session.close();
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
}
Its giving me Null pointer Exception..
I didn't understand how and when the filters will be invoked, even the filter parameter should also be indexed???
Could anybody clear my doubts. Im really confused.
Help pls,
Ambika.