Hibernate filter not working while indexing through hibernate search
I am trying to index an embedded collection (Set) having one-to-many associati开发者_运维知识库on using @IndexedEmbedded.The problem is that we are only soft deleting the records in our application and I want to apply hibernate filter on the indexed collection so as to exclude the logically deleted records while indexing.
@Index
Class A {
@IndexedEmbedded
@OneToMany(targetEntity = B.class, fetch = FetchType.EAGER)
@Filter(name = "deletedRecordsFilter")
Set<B> setOfBs;
}
For Indexing :
FullTextSession fts = getFullTextSession();
fts.createIndexer(entityClass)
.purgeAllOnStart(true)
.optimizeAfterPurge(true)
.optimizeOnFinish(true)
.batchSizeToLoadObjects(30)
.threadsForSubsequentFetching(8)
.threadsToLoadObjects(4)
.threadsForIndexWriter(3)
.startAndWait();
I have enabled the filter using session.enableFilter("deletedFilterRecords"). The data is indexed but the filter is not working properly. The embedded collections still contain the deleted records.
Is that hibernate filters do not work while indexing through hibernate search or am I missing something? If filters do not work while indexing, then is there any way so as not to index the logically deleted records?
You should use a FullTextFilter, not a standard Hibernate filter. I have used one in the project that I am currently working on. You add an annotation like the below above the definition of your indexed class:
@Indexed
@Entity
@FullTextFilterDefs( {
@FullTextFilterDef(name = "includeInSearchFilter", impl = IncludeInSearchFilterFactory.class,
cache = FilterCacheModeType.INSTANCE_AND_DOCIDSETRESULTS)
})
public class SomeEntity ...
You then need to supply the referenced factory class as well, something like this:
public class IncludeInSearchFilterFactory {
private String includeInSearchResults;
public void setIncludeInSearchResults(String includeInSearchResults) {
this.includeInSearchResults = includeInSearchResults;
}
@Key
public FilterKey getKey() {
StandardFilterKey key = new StandardFilterKey();
key.addParameter(includeInSearchResults);
return key;
}
@Factory
public Filter getFilter() {
Query query = new TermQuery(new Term("includeInSearchResults", includeInSearchResults));
return new QueryWrapperFilter(query);
}
}
In my case the "includeInSearchResults" member was an indexed field on the entity which was set to true if I wanted the object to be returned by my search else it was set to false.
To enable the full text filter:
fullTextQuery.enableFullTextFilter("includeInSearchFilter").setParameter("includeInSearchResults", "true");
精彩评论