2011-08-25 105 views
1

我想索引一個一對多關聯使用@ IndexedEmbedded嵌入式集合(集)。問題是,我們只是軟刪除我們的應用程序中的記錄,我想要在索引集合上應用hibernate篩選器,以便在索引時排除邏輯刪除的記錄。休眠過濾器不工作,而索引通過休眠搜索

@Index 
Class A { 

@IndexedEmbedded 
@OneToMany(targetEntity = B.class, fetch = FetchType.EAGER) 
@Filter(name = "deletedRecordsFilter") 
Set<B> setOfBs; 

} 

索引:

  FullTextSession fts = getFullTextSession(); 
      fts.createIndexer(entityClass) 
      .purgeAllOnStart(true) 
      .optimizeAfterPurge(true) 
      .optimizeOnFinish(true) 
      .batchSizeToLoadObjects(30) 
      .threadsForSubsequentFetching(8) 
      .threadsToLoadObjects(4) 
      .threadsForIndexWriter(3) 
      .startAndWait(); 

我已經啓用使用session.enableFilter( 「deletedFilterRecords」)的過濾器。數據已編入索引,但過濾器工作不正常。嵌入的集合仍包含已刪除的記錄。

是hibernate篩選器不工作,而通過hibernate搜索索引或我缺少的東西嗎? 如果過濾器在索引時不起作用,那麼有什麼辦法可以避免爲邏輯刪除的記錄建立索引?

回答

1

您應該使用FullTextFilter,而不是標準的Hibernate篩選器。我在當前正在開發的項目中使用了一個。您添加註釋,如下面的索引類定義的上方:

@Indexed 
@Entity 
@FullTextFilterDefs({ 
    @FullTextFilterDef(name = "includeInSearchFilter", impl = IncludeInSearchFilterFactory.class, 
      cache = FilterCacheModeType.INSTANCE_AND_DOCIDSETRESULTS) 
}) 
public class SomeEntity ... 

然後,您需要提供參考的工廠類爲好,這樣的事情:

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); 
    } 
} 

在我的情況下「 includeInSearchResults「成員是實體上的索引字段,如果我希望對象由我的搜索返回,則設置爲true,否則將其設置爲false。

要啓用全文過濾器:

fullTextQuery.enableFullTextFilter("includeInSearchFilter").setParameter("includeInSearchResults", "true");