0

我为休眠搜索构建了一个示例项目,它可以正常工作,没有任何异常,但是当我搜索一个字符串时,我有一些带有该字符串的对象,它返回空。我不知道我该怎么办!!!有人可以帮助我...项目是基于 maven 的,我使用 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();

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

4

1 回答 1

0

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

于 2012-05-12T11:27:30.247 回答