休眠搜索错误

发布于 2024-10-16 00:35:58 字数 4107 浏览 4 评论 0原文

有人可以帮忙吗,我正在尝试使用休眠搜索的简单示例,但出现以下错误:

Exception in thread "main" org.hibernate.HibernateException: could not init listeners
    at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205)
    at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1396)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
    at FirstExample.main(FirstExample.java:32)
Caused by: java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:61)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
    at org.hibernate.search.util.HibernateSearchResourceLoader.getLines(HibernateSearchResourceLoader.java:52)
    at org.apache.solr.analysis.StopFilterFactory.inform(StopFilterFactory.java:53)
    at org.hibernate.search.impl.SolrAnalyzerBuilder.buildAnalyzer(SolrAnalyzerBuilder.java:79)
    at org.hibernate.search.impl.InitContext.buildAnalyzer(InitContext.java:185)
    at org.hibernate.search.impl.InitContext.initLazyAnalyzers(InitContext.java:155)
    at org.hibernate.search.impl.SearchFactoryImpl.initDocumentBuilders(SearchFactoryImpl.java:541)
    at org.hibernate.search.impl.SearchFactoryImpl.<init>(SearchFactoryImpl.java:171)
    at org.hibernate.search.event.ContextHolder.getOrBuildSearchFactory(ContextHolder.java:60)
    at org.hibernate.search.event.FullTextIndexEventListener.initialize(FullTextIndexEventListener.java:122)
    at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198)
    at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181)
    at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194)
    ... 3 more

我的项目类:

@Indexed 
@AnalyzerDef(name="customanalyzer",
        tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
        filters = {
                @TokenFilterDef(factory = ISOLatin1AccentFilterFactory.class),
                @TokenFilterDef(factory = LowerCaseFilterFactory.class),
                @TokenFilterDef(factory = StopFilterFactory.class, params = {
                    @Parameter(name="words", value= "org/hibernate/search/test/analyzer/solr/stoplist.properties" ),
                    @Parameter(name="ignoreCase", value="true")
                })
})


public class Item 
{ 
    @DocumentId 
      private Integer id; 

      @Field @Analyzer(definition = "customanalyzer")
      private String title; 

      @Field @Analyzer(definition = "customanalyzer")
      private String description; 

      @Field(index=Index.UN_TOKENIZED, store=Store.YES) 
      private String ean; 

      private String imageURL; 
      //public getters and setters 
}

程序代码:

Session session = null;
        Transaction tx = null;

            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session =sessionFactory.openSession();
            FullTextSession ftem = org.hibernate.search.Search.getFullTextSession(session);

            ftem.beginTransaction();
            List <Item> AllItem=session.createQuery("From Item").list();

            for (Item item : AllItem) { 
                  ftem.index(item);
            } 
            ftem.getTransaction().commit();



            String searchQuery = "title:Batman";

            QueryParser parser = new QueryParser( 
                    "title", 
                    ftem.getSearchFactory().getAnalyzer("customanalyzer")
        ); 
            org.apache.lucene.search.Query luceneQuery; 
            try { 
                 luceneQuery = parser.parse(searchQuery);
            } 
            catch (ParseException e) { 
                  throw new RuntimeException("Unable to parse query: " + searchQuery, e); 
            }

            org.hibernate.Query query = ftem.createFullTextQuery( 
                    luceneQuery, 
                    Item.class); 
            query.setFirstResult(20).setMaxResults(20);
            List results = query.list();

Can someone help, i'm trying a simple example with hibernate search but getting the following error:

Exception in thread "main" org.hibernate.HibernateException: could not init listeners
    at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205)
    at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1396)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
    at FirstExample.main(FirstExample.java:32)
Caused by: java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:61)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
    at org.hibernate.search.util.HibernateSearchResourceLoader.getLines(HibernateSearchResourceLoader.java:52)
    at org.apache.solr.analysis.StopFilterFactory.inform(StopFilterFactory.java:53)
    at org.hibernate.search.impl.SolrAnalyzerBuilder.buildAnalyzer(SolrAnalyzerBuilder.java:79)
    at org.hibernate.search.impl.InitContext.buildAnalyzer(InitContext.java:185)
    at org.hibernate.search.impl.InitContext.initLazyAnalyzers(InitContext.java:155)
    at org.hibernate.search.impl.SearchFactoryImpl.initDocumentBuilders(SearchFactoryImpl.java:541)
    at org.hibernate.search.impl.SearchFactoryImpl.<init>(SearchFactoryImpl.java:171)
    at org.hibernate.search.event.ContextHolder.getOrBuildSearchFactory(ContextHolder.java:60)
    at org.hibernate.search.event.FullTextIndexEventListener.initialize(FullTextIndexEventListener.java:122)
    at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198)
    at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181)
    at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194)
    ... 3 more

My Item Class:

@Indexed 
@AnalyzerDef(name="customanalyzer",
        tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
        filters = {
                @TokenFilterDef(factory = ISOLatin1AccentFilterFactory.class),
                @TokenFilterDef(factory = LowerCaseFilterFactory.class),
                @TokenFilterDef(factory = StopFilterFactory.class, params = {
                    @Parameter(name="words", value= "org/hibernate/search/test/analyzer/solr/stoplist.properties" ),
                    @Parameter(name="ignoreCase", value="true")
                })
})


public class Item 
{ 
    @DocumentId 
      private Integer id; 

      @Field @Analyzer(definition = "customanalyzer")
      private String title; 

      @Field @Analyzer(definition = "customanalyzer")
      private String description; 

      @Field(index=Index.UN_TOKENIZED, store=Store.YES) 
      private String ean; 

      private String imageURL; 
      //public getters and setters 
}

Program code:

Session session = null;
        Transaction tx = null;

            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session =sessionFactory.openSession();
            FullTextSession ftem = org.hibernate.search.Search.getFullTextSession(session);

            ftem.beginTransaction();
            List <Item> AllItem=session.createQuery("From Item").list();

            for (Item item : AllItem) { 
                  ftem.index(item);
            } 
            ftem.getTransaction().commit();



            String searchQuery = "title:Batman";

            QueryParser parser = new QueryParser( 
                    "title", 
                    ftem.getSearchFactory().getAnalyzer("customanalyzer")
        ); 
            org.apache.lucene.search.Query luceneQuery; 
            try { 
                 luceneQuery = parser.parse(searchQuery);
            } 
            catch (ParseException e) { 
                  throw new RuntimeException("Unable to parse query: " + searchQuery, e); 
            }

            org.hibernate.Query query = ftem.createFullTextQuery( 
                    luceneQuery, 
                    Item.class); 
            query.setFirstResult(20).setMaxResults(20);
            List results = query.list();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-10-23 00:35:58

看起来 Hibernate Search 在类路径中找不到 org/hibernate/search/test/analyzer/solr/stoplist.properties 。确保它在那里。

It looks like Hibernate Search can't find org/hibernate/search/test/analyzer/solr/stoplist.properties in the classpath. Make sure it's there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文