简体   繁体   English

如何在没有底层数据库的情况下使用Hibernate Search?

[英]How to use Hibernate Search without an underlying database?

Is it possible to use hibernate search features without using hibernate core for the database stuff? 是否有可能使用hibernate搜索功能而不使用hibernate核心作为数据库的东西?

I want to create a Bean with standard Hibernate-Search annotations and store everything by hand for testing purposes (and I don't want to create the Hibernate Tables for testing). 我想创建一个带有标准Hibernate-Search注释的Bean,并手动存储所有内容用于测试目的(我不想创建用于测试的Hibernate表)。

For a class to be annotated with @Indexed I need it to be an @Entity, but because of that I get an error because I didn't specify my database stuff for this class. 对于要使用@Indexed注释的类,我需要它是一个@Entity,但由于这个原因我得到一个错误,因为我没有为这个类指定我的数据库内容。

My Question is: 我的问题是:

How do I use Hibernate Search only for the Lucene management? 如何仅将Hibernate Search用于Lucene管理?

Ultimately your hibernate needs to know the database configurations.. When you notice the hibernate log, Hibernate engine will first register types and as second step it will look for hibernate configuration properties and it will parse and validate in next steps.. My point here is hibernate must have be informed database information... By the way you can also post your error details for further assistance.. 最终你的hibernate需要知道数据库配置。当你注意到hibernate日志时,Hibernate引擎将首先注册类型,第二步它将查找hibernate配置属性,它将在后续步骤中进行解析和验证。我的观点是hibernate必须通知数据库信息...顺便提一下,您还可以发布您的错误详细信息以获得进一步的帮助..

Cheeers! Cheeers!

If everything else fails, you can use one of in-memory databases . 如果其他一切都失败了,您可以使用其中一个 内存 数据库 They will self-destruct after you run your test. 在您进行测试后,它们会自毁。

I think the question is here what is your ultimate goal? 我认为问题在于你的最终目标是什么? Do you want to use Hibernate Search in production w/o Hibernate ORM or are you just looking for some testing solutions? 您是否希望在没有Hibernate ORM的情况下使用Hibernate Search,或者您只是在寻找一些测试解决方案?

You can actually use Hibernate Search standalone w/o Hibernate ORM. 您实际上可以使用Hibernate Search独立的Hibernate ORM。 However, you would have to implement several SPI contracts. 但是,您必须实现多个SPI合约。 It's cumbersome, but possible. 这很麻烦,但可能。 In fact Infinispan Query (the query capabilities of Infinispan - https://github.com/infinispan/infinispan/tree/master/query ) does exactly that. 事实上,Infinispan Query(Infinispan的查询功能 - https://github.com/infinispan/infinispan/tree/master/query )正是如此。 It uses the engine of Hibernate Search w/o the ORM parts. 它使用Hibernate Search的引擎,没有ORM部分。 The Search team is on the process to make it much easier to use Search without ORM. 搜索团队正在使用ORM来更轻松地使用搜索。 Keep an eye on upcoming releases. 密切关注即将发布的版本。

If you are just looking for some way to test Hibernate Search + ORM, then you are much better off to use an in memory database. 如果您只是想找一些方法来测试Hibernate Search + ORM,那么最好使用内存数据库。 That is the easiest way to do unit testing in this case. 在这种情况下,这是进行单元测试的最简单方法。

That's how its done (if you have a database, but you don't want to store the indexed stuff into it): 这就是它的完成方式(如果你有一个数据库,但你不想将索引的东西存储到它中):

Create a dummy Entity class with all the information you need and do it like this (Example from my code with a Suggest class) 创建一个虚拟的Entity类,其中包含您需要的所有信息,并按照这样的方式执行(示例来自我的代码,带有Suggest类)

        Transaction tx = fullTextSession.beginTransaction();

        fullTextSession.purgeAll(Suggest.class);

        SuggestionFinder suggestionFinder = context
                .getBean(SuggestionFinder.class);
        List<Suggest> toDelete = new ArrayList<>();
        int i = 0;
        for (Suggest sugg : suggestionFinder.findSuggestionsForIndex()) {
            fullTextSession.save(sugg);
            toDelete.add(sugg);
            fullTextSession.index(sugg);
            if (i++ % 1000 == 0) {
                fullTextSession.flushToIndexes();
                for (Suggest cur : toDelete) {
                    fullTextSession.delete(cur);
                }
                toDelete.clear();
            }
        }
        fullTextSession.flushToIndexes();
        for (Suggest cur : toDelete) {
            fullTextSession.delete(cur);
        }
        toDelete.clear();

        fullTextSession.getSearchFactory().optimize(Suggest.class);

        tx.commit();

EDIT: 编辑:

I have implemented a Hibernate-Search Standalone version: 我已经实现了Hibernate-Search Standalone版本:

https://github.com/Hotware/Hibernate-Search-JPA/tree/master/hibernate-search-standalone https://github.com/Hotware/Hibernate-Search-JPA/tree/master/hibernate-search-standalone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM