Hibernate Search - 默认情况下如何列出所有记录

时间:2014-07-14 10:21:45

标签: lucene hibernate-search

以下问题表明*作为搜索字符串应该起作用并返回所有记录。 这是有道理的,但不起作用。

Hibernate Search - How to list all records by default

我尝试了以下代码:

@Test
@SuppressWarnings("unchecked")
public void searchCountry() {

    String searchTerms = "*";
    FullTextSession fullTextSession = org.hibernate.search.Search
            .getFullTextSession(sessionFactory.getCurrentSession());
    String[] fieldsToMatch = new String[] { "code" };

    QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36,
            fieldsToMatch, new StandardAnalyzer(Version.LUCENE_36));
    try {
        org.apache.lucene.search.Query luceneQuery = parser
                .parse(searchTerms);
        FullTextQuery jpaQuery = fullTextSession.createFullTextQuery(
                luceneQuery, Country.class);
        jpaQuery.setFirstResult(0);

        jpaQuery.setMaxResults(10);
        Date startDate = new Date();
        System.out.println("Before HQL =" + startDate);
        List<Country> tempCountry = jpaQuery.list();
        Date endDate = new Date();
        System.out.println("after HQL =" + endDate);
        System.out.println("DIFF ="
                + (endDate.getTime() - startDate.getTime()));
        System.out.println(tempCountry.size());
        System.out.println(tempCountry);
    } catch (ParseException e) {
        System.out.println("Error parsing lucene query: " + searchTerms);

        System.out.println();
        e.printStackTrace();
        System.out.println();
    }
}

这是我得到的错误:

org.apache.lucene.queryParser.ParseException: Cannot parse '*': '*' or '?' not allowed as first character in WildcardQuery
Error parsing lucene query: *

at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:211)
at com.zycus.imaster.lucene.test.MDTTest.searchCountry(MDTTest.java:123)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.lucene.queryParser.ParseException: '*' or '?' not allowed as first character in WildcardQuery
at org.apache.lucene.queryParser.QueryParser.getWildcardQuery(QueryParser.java:990)
at org.apache.lucene.queryParser.MultiFieldQueryParser.getWildcardQuery(MultiFieldQueryParser.java:201)
at org.apache.lucene.queryParser.MultiFieldQueryParser.getWildcardQuery(MultiFieldQueryParser.java:196)
at org.apache.lucene.queryParser.QueryParser.Term(QueryParser.java:1421)
at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1319)
at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1245)
at org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1234)
at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:206)
... 30 more

1 个答案:

答案 0 :(得分:1)

AllContext org.hibernate.search.query.dsl.QueryBuilder.all()

查询匹配所有文档(通常与布尔查询混合)。

刚刚找到解决我问题的方法。以下是任何想知道同一问题的示例代码。

@Test
public void testSearchUsingQueryBuilder() {
    FullTextSession fullTextSession = org.hibernate.search.Search
            .getFullTextSession(sessionFactory.getCurrentSession());
    QueryBuilder builder = fullTextSession.getSearchFactory()
            .buildQueryBuilder().forEntity(Country.class).get();
    org.apache.lucene.search.Query query = builder.all().createQuery();
    FullTextQuery jpaQuery = fullTextSession.createFullTextQuery(query,
            Country.class);
    System.out.println("Before HQL =" + startDate);
    List<Country> tempCountry = jpaQuery.list();
}