Hibernate全文搜索

Hibernate全文搜索,hibernate,search,full-text-search,hibernate-search,Hibernate,Search,Full Text Search,Hibernate Search,我刚刚使用hibernate-search-4.1.1.Final.jar和所有运行时依赖项创建了一个hibernate全文搜索。 此应用程序中没有错误。 但是我的Lucene查询取消查询DSL不会返回任何结果。 我的意思是不返回表中的任何行。 谁能帮帮我吗 主搜索程序 此Java代码用于执行hibernate全文搜索 public class MainSearch { public static void main(String args[]) {

我刚刚使用hibernate-search-4.1.1.Final.jar和所有运行时依赖项创建了一个hibernate全文搜索。 此应用程序中没有错误。 但是我的Lucene查询取消查询DSL不会返回任何结果。 我的意思是不返回表中的任何行。 谁能帮帮我吗

主搜索程序 此Java代码用于执行hibernate全文搜索

   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()
行)

参考:(因为Lucene不知道您的DBMS,反之亦然,Hibernate搜索是它们之间的链接,索引您的数据是Lucene可以搜索的原因)

同样的问题解决了