【问题标题】:Hibernate Search With MultiTenancy多租户的休眠搜索
【发布时间】:2014-12-30 11:56:58
【问题描述】:

我的要求是每个租户的每个索引,我已经将休眠配置为 MultiTenant,我需要将每个租户的数据库索引到不同的索引。我见过动态分片策略。但需求是动态的。它可以有 n 个分片,而不需要关于每个租户存在的任何预先信息。甚至我的索引器也应该以这种方式工作。每当它发现获取索引的休眠请求时,它应该首先索引该租户,然后搜索它。

我该怎么做..??

谁能给我一些例子。 请不要提供休眠文档链接...甚至是用于休眠搜索的 Jboss 文档链接。

【问题讨论】:

  • 所以您说您已经看过并阅读了 Hibernate Search 的动态分片文档。您是否查看过 ShardIdentifierProvider 和文档中给出的示例。动态共享允许您动态创建新的分片,为什么它不适合您的用例?
  • 我读过。它适合它.. Bt 我的要求是我也可以分别对每个租户进行动态分片。
  • 嗨哈迪,我研究了休眠搜索,但我目前面临的问题是当我尝试使用动态分片策略时。在触发搜索查询时。但它没有按代码索引数据如下 FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); fullTextSession.createIndexer().startAndWait();
  • 我也有类似的问题。 MassIndexer 似乎不适用于具有动态分片标识符的 MultiTenant,尽管我使用的 Hibernate 通过 Multi Tenancy MultiTenantConnectionProvider 支持。
  • 我认为在这种情况下使用质量索引器确实行不通。正如您所说,您将无法通过 ThreadLocal 获取租户 ID。您可以尝试通过 FullTextSession#index 进行索引。看看这个例子docs.jboss.org/hibernate/stable/search/reference/en-US/…

标签: lucene hibernate-search multi-tenant


【解决方案1】:

要对特定租户进行索引,您应该在应用程序上下文中添加 Entitymanager 部分的休眠搜索属性,例如:

<bean depends-on="dataSource" id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="generateDdl" value="true"></property>
            <property name="showSql" value="true" />
        </bean>
    </property>
    <property name="packagesToScan" value="xxx.xxxx..xxx." />
    <property name="persistenceUnitName" value="xxxxxx" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.search.default.directory_provider">filesystem</prop>
            <prop key="hibernate.search.default.indexBase">C:\xxxxx\indexes</prop>

        </props>
    </property>
</bean> 

现在您必须使用 org.hibernate.Session 来识别您的租户以执行对特定租户的搜索,如下所示:

EntityManager manager = managerFactory.createEntityManager();

SessionImpl i = (SessionImpl) manager.getDelegate();
SessionFactory session = i.getSessionFactory();

Session s = session.withOptions().tenantIdentifier(xxxxx).openSession();

FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(s);
return fullTextSession; 

这样每次搜索都将使用您提供的特定租户,但对我来说,您似乎还有另一个问题是如何获取租户,在我的情况下,我使用 url 来识别租户,如果您有的话用户选择要使用的租户,在静态变量中获取该信息以在必要时使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 2021-06-19
    • 2014-12-01
    • 2015-03-20
    • 1970-01-01
    • 2014-02-14
    • 2016-05-03
    相关资源
    最近更新 更多