Hibernate Search无法返回正确的结果

时间:2012-05-12 02:39:37

标签: hibernate-annotations hibernate-search

我为hibernate搜索构建了一个示例项目,它没有任何异常,但是当我搜索一个字符串,我有一些带有该字符串的对象时,它返回空。我不知道该怎么办!!!有人可以帮助我...... Project是基于maven的,我使用hibernate注释来进行hibernate和hibernate搜索。 这是我的hibernate.cfg.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/eprnews
        </property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">***</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <!-- 
        <property name="hibernate.hbm2ddl.auto">create-drop</property>

        Hibernate Search --> 
        <!-- org.hibernate.search.store.FSDirectoryProvider --> 
        <!-- org.hibernate.search.store.RAMDirectoryProvider for test -->
        <!-- 
        <property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property>
         -->
        <property name="hibernate.search.default.directory_provider">filesystem</property>
        <property name="hibernate.search.default.indexBase">./indexes</property>
        <!-- 
         -->
        <property name="hibernate.search.worker.execution">async</property>
        <property name="hibernate.search.default.optimizer.transaction_limit.max">100</property>

        <!-- Mapped classes -->
        <mapping class="net.leemoo.test.entity.NewsEntity"/>
    </session-factory>
</hibernate-configuration>

这是我的实体:

@Entity
@Table(name="news")
@Indexed
@AnalyzerDef(
            name = "PersianAnal",
            charFilters = @CharFilterDef(factory = PersianCharFilterFactory.class),
            tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
            filters={
                        @TokenFilterDef(factory = ArabicNormalizationFilterFactory.class),
                        @TokenFilterDef(factory = PersianNormalizationFilterFactory.class)
                    }
            )
public class NewsEntity {

    @Id
    @GeneratedValue
    private Long id;

    @Field
    @Analyzer(definition="PersianAnal")
    private String title;

    @Field
    @Analyzer(definition="PersianAnal")
    private String newsAbstract;

    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @Analyzer(definition="PersianAnal")
    private String content;

    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @DateBridge(resolution=Resolution.DAY)
    private Date creationDate;

    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @DateBridge(resolution=Resolution.DAY)
    private Date modifiedDate;

    private Integer viewsCounter;

    @Field
    @Analyzer(definition="PersianAnal")
    private String keywords;

    private Boolean jqueryNews;

    private Boolean photoNews;
        and setters and getters...

这是我用于搜索的代码:

Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        try {
//          fullTextSession.createIndexer().startAndWait();
            QueryBuilder gb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(NewsEntity.class).get();
            Query query = gb.keyword().
                onFields("title", "newsAbstract", "content").
                matching("test").createQuery();
            org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, NewsEntity.class);
            List<NewsEntity> result = (List<NewsEntity>)hibQuery.list();
            System.out.println(result.size());
        } catch (Exception e) {
//          e.printStackTrace();
        }

        session.getTransaction().commit();
        session.close();

我该怎么办?请帮帮我......

1 个答案:

答案 0 :(得分:0)

我认为我犯了一个愚蠢的错误,因为我在最后一个代码短语中评论了我的索引创建者。我应该在开始时运行一次以创建索引。我很抱歉我花了你的时间。