1

I am trying to index an embedded collection (Set) having one-to-many association 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?

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.