2012-08-01 174 views
1

我剛剛創建了一個使用hibernate-search-4.1.1.Final.jar和所有運行時依賴關係的hibernate全文搜索。 這個應用程序沒有錯誤。 但我的Lucene查詢unsing查詢DSL不會返回任何結果。 我的意思是不返回表中的任何行。 任何人都可以幫助我。Hibernate全文搜索

主要搜索程序 此Java代碼用於執行休眠全文搜索。

public class MainSearch { 
       public static void main(String args[]) { 
      Iterator iterator; 
      Session session = HibernateUtil.getSession(); 
      // FullTextSession fullTextSession = Search.getFullTextSession(session); 

      FullTextSession fullTextSession = Search.getFullTextSession(session); 
      org.hibernate.Transaction tx = fullTextSession.beginTransaction(); 

      // create native Lucene query unsing the query DSL 
      // alternatively you can write the Lucene query using the Lucene query 
      // parser 
      // or the Lucene programmatic API. The Hibernate Search DSL is 
      // recommended though 
      QueryBuilder qb = fullTextSession.getSearchFactory() 
        .buildQueryBuilder().forEntity(Book.class).get(); 
      org.apache.lucene.search.Query query = qb.keyword() 
        .onFields("title", "subtitle", "authors.name").matching("cpp") 
        .createQuery(); 

      // wrap Lucene query in a org.hibernate.Query 
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(
        query, Book.class); 

      // execute search 

      List result = hibQuery.list(); 
      iterator = result.iterator(); 
      while (iterator.hasNext()) { 
       System.out.print(iterator.next() + " "); 
      } 
      System.out.println(); 
      // Check list empty or not 
      if (result.isEmpty()) { 
       System.out.println("Linked list is empty"); 
      } 

      tx.commit(); 
      session.close(); 
     } 
    } 
+0

http://stackoverflow.com/questions/9488094/ hibernate-mssql-fulltext-search-via-contains 解決同樣的問題 – user1817599 2013-01-13 22:13:56

回答

2

您沒有在數據庫中包含任何內容(在您的代碼中)。如果您在代碼之外執行操作,則需要先編制數據庫索引,然後才能進行搜索。要做到這一點,執行此:

FullTextSession fullTextSession = Search.getFullTextSession(session); 
fullTextSession.createIndexer().startAndWait(); 

而且你也不需要打開的事務進行搜索的東西,這樣你就可以刪除org.hibernate.Transaction tx = fullTextSession.beginTransaction();線(和上面的startAndWait()替換它)

參考: http://hibernate.org/search/documentation/getting-started/#indexing(因爲Lucene不知道你的DBMS,反之亦然,Hibernate Search是它們之間的鏈接,並且索引你的數據使它可以被Lucene搜索)