Hibernate搜索布尔过滤器

时间:2014-11-20 18:57:32

标签: lucene 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%存在。我究竟做错了什么?如果您将过滤器字段替换为“得分”,则所有项都有效,结果不为空。不要在布尔字段上搜索

1 个答案:

答案 0 :(得分:0)

基本方法看起来不错。几条评论。您正在为每个 findByQuery 调用调用索引器。不确定这是否只是一些测试代码,但您应该在搜索之前进行索引,只进行一次或当事情发生变化时(您也可以使用自动索引更新)。也可能是根据您的交易设置,您的搜索无法查看索引数据。但是,您似乎说如果您根本不使用过滤器,则一切正常。在这种情况下,我会向过滤器添加一些调试或调试它以查看正在发生的事情以及它是否被调用。最后但同样重要的是,您无需明确设置@FieldBridge(impl = BooleanBridge.class)