Hibernate 搜索动态映射

Hibernate Search Dynamic Mapping

我正在尝试通过使用 hibernate-search elastic search 集成将 elastic search 与 hibernate 结合使用。我有使用鉴别器策略的多租户数据,所以如果能用自动添加的租户标识符来索引实体会很棒。到目前为止这似乎有效:

Session session = sessionFactory
                          .withOptions()
                            .tenantIdentifier("parkId-" + p.getId().toString())
                          .openSession();

然而,在索引过程中,elasticsearch 报错是因为 strict_dynamic_mapping_exception:

Response: 
{
  "index": {
    "_index": "entities.productmodel",
    "_type": "entities.ProductModel",
    "_id": "parkId-1_29426",
    "status": 400,
    "error": {
      "type": "strict_dynamic_mapping_exception",
      "reason": "mapping set to strict, dynamic introduction of [__HSearch_TenantId] within [entities.ProductModel] is not allowed"
    }
  }
}

这就是全部尽管事实上我正在覆盖休眠搜索的默认行为并将动态映射设置为 true,如 docs 所示:

configuration.setProperty("hibernate.search.default.elasticsearch.dynamic_mapping", "true");

(其他设置已通过此方法正确设置,所以我知道这不是问题所在。)

知道我错过了什么吗?即使将 dynamic_mapping 设置为 false 也不会导致任何更改 - 弹性搜索仍然抱怨映射设置为严格。我的弹性搜索集群是通过 docker 在本地 运行。

如果您在 ORM 配置中设置了 hibernate.multiTenancy,租户 ID 字段应该是架构的一部分。

有吗?

如果是这样,我们可能在某个地方有错误,测试用例会有所帮助。我们这里有一个测试用例模板:https://github.com/hibernate/hibernate-test-case-templates/tree/master/search/hibernate-search-elasticsearch/hibernate-search-elasticsearch-5 .

确保在每次尝试开发之前重新生成您的架构。您不需要为此设置 dynamic_mapping,但如果您在尝试添加多租户之前生成了架构,并且此后没有更新架构,您将遇到类似这样的错误。

只需删除 Elasticsearch 集群中的索引,或将 属性 hibernate.search.default.elasticsearch.index_schema_management_strategy 设置为 drop-and-create不适用于生产环境,您将丢失所有索引数据)。有关架构生成的详细信息,请参阅 this section of the documentation