2014-11-20 173 views
0

我有簿條目:Hibernate Search的布爾過濾

@Entity 
@Indexed 
public class Book extends BaseEntity { 
@Field 
private String subtitle; 

@DateBridge(resolution = Resolution.DAY) 
private Date publicationDate; 
@Field 
private int score; 
@IndexedEmbedded 
@ManyToMany(fetch = FetchType.EAGER) 
@Cascade(value = {CascadeType.ALL}) 
private List<Author> authors = new ArrayList<Author>(); 
@Field 
@FieldBridge(impl = BooleanBridge.class) 
private boolean prohibited; 

和過濾布爾場 「phohibited」

public class BFilter extends Filter { 

@Override 
public DocIdSet getDocIdSet(IndexReader indexReader) throws IOException { 
    OpenBitSet bitSet = new OpenBitSet(indexReader.maxDoc()); 
    TermDocs termDocs = indexReader.termDocs(new Term("prohibited","false")); 
    while (termDocs.next()) { 
     bitSet.set(termDocs.doc()); 
    } 
    return bitSet; 
} 
} 

搜索方法

public List<T> findByQuery(Class c, String q) throws InterruptedException { 
    FullTextSession fullTextSession = Search.getFullTextSession(session); 
    fullTextSession.createIndexer().startAndWait(); 

    QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(c).get(); 

    Query luceneQuery = qb 
      .keyword() 
      .fuzzy() 
      .onFields("title", "subtitle", "authors.name", "prohibited", "score") 
      .matching(q) 
      .createQuery(); 

    FullTextQuery createFullTextQuery = fullTextSession.createFullTextQuery(luceneQuery,  Book.class, BaseEntity.class); 
    createFullTextQuery.setFilter(new BFilter()); 

    return createFullTextQuery.list(); 
} 

,如果我申請的是過濾器 - 搜索結果是空的。數據庫中的條目100%在那裏。我究竟做錯了什麼?如果將過濾器字段替換爲「分數」,表示所有工作,並且結果不爲空。不要搜索它在一個布爾字段

回答

0

的基本方法看起來OK。一些評論。您所要求的每個findByQuery調用索引。不知道這是否只是一些測試代碼,但你應該指數在進行搜索之前,只有一次或情況發生變化(你也可以使用自動更新索引)。也可能是因爲您的交易設置,您的搜索無法看到索引數據。但是,如果您完全不使用過濾器,您似乎會說所有的作品。在這種情況下,我會添加一些調試到過濾器或調試它看看發生了什麼,如果它被調用。最後但並非最不重要的一點,你不需要明確地設置@FieldBridge(impl = BooleanBridge.class)