Hibernate全文搜索
问题描述:
我刚刚创建了一个使用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();
}
}
答
您没有在数据库中包含任何内容(在您的代码中)。如果您在代码之外执行操作,则需要先编制数据库索引,然后才能进行搜索。要做到这一点,执行此:
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搜索)
http://*.com/questions/9488094/ hibernate-mssql-fulltext-search-via-contains 解决同样的问题 – user1817599 2013-01-13 22:13:56