2012-05-12 61 views
0

我建立了一個Hibernate搜索的示例項目,它沒有任何異常,但它正常工作,但當我搜索一個字符串,我有一些對象與該字符串,它將返回空。我不知道該怎麼辦!有人可以幫助我... 項目是基於Maven的,我使用hibernate註釋來進行hibernate和hibernate搜索。 這裏是我的hibernate.cfg.xml文件:Hibernate搜索將不會返回正確的結果

<?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(); 

什麼我應該怎麼做?請幫我....

回答

0

我想我犯了一個愚蠢的錯誤,我在最後一段代碼中評論了我的索引創建者。我應該在開始時爲其創建索引運行一次。我再次抱歉,我花了你的時間。