2

我最近开始使用filtered aliasesElastic Search (此处的文档),但有一个我不知道如何处理的用例。

用例

我在 ElasticSearch 中为事实编制索引的每个文档都将有一个名为“tenantId”的字段(以及一些其他字段,例如“type”、“id”等)。现在所有文档都驻留在同一个索引中,所以每个租户我想确保我创建了一个过滤的别名。现在,我想在创建租户本身并方便使用“tenantId”后立即创建过滤别名。

问题

当我尝试使用他们的 java 客户端以编程方式创建别名时,出现以下异常:

Caused by: org.elasticsearch.index.query.QueryParsingException: 
     [mdm-master] Strict field resolution and no field mapping 
     can be found for the field with name [tenantId] 

研究更多,我发现我可能可以使用动态模板来实现这一点。所以我创建了一个模板,将其保存在 下config/templates,重新创建了我的索引并再次尝试了同样的事情。再次遇到同样的异常。在阅读更多here文档(页面底部3行)时,我发现即使我尝试将以下属性更改index.query.parse.allow_unmapped_fields为true(我还没有尝试过),对于过滤的别名,它也会强制它错误的。

现在的问题是,我该如何处理我的用例?我不知道相应类型的映射,但我所知道的事实是,我索引的每个文档,无论类型如何,总是有一个名为的字段tenantId,这就是我想要创建过滤别名的内容。

编辑

我发现了几个有用的链接。不确定这是固定在哪个版本上。 模板中过滤的别名不会从别名索引中继承映射 #8473 index.query.parse.allow_unmapped_fields 设置似乎不允许别名过滤器中未映射的字段 #8431

第二次编辑

发现了 ElasticSearch 的一个开放错误,具有完全相同的问题。等待 ES 开发人员回复。使用模板映射在空索引上创建过滤别名失败 #10038

非常感谢所有帮助!几天以来,我一直在尝试解决这个问题,但没有运气:(。

以下是我用来添加过滤别名的代码,以及默认映射 json 模板

模板

{
  "template-1": {
    "template": "*",
    "mappings": {
      "_default_": {
        "properties": {
          "type": {
            "type": "string",
            "index": "not_analyzed"
          },
          "id": {
            "type": "string",
            "index": "not_analyzed"
          },
          "tenantId": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
}

JAVA客户端

(你现在可以忽略“Observable”相关的东西)

public Observable<Boolean> createAlias(String tenantId) {

        FilterBuilder filter = FilterBuilders.termFilter("tenantId", tenantId);
        ListenableActionFuture<IndicesAliasesResponse> response = client.admin().indices().prepareAliases().addAlias("mdm-master", tenantId, filter).execute();
        return Observable.from(response)
                .map((IndicesAliasesResponse apiResponse) -> {
                    return apiResponse.isAcknowledged();
                });
    }
4

1 回答 1

2

我是在 ES Github Failure to create Filtered Alias on empty index with template mappings #10038上发布最新一期的人。我现在发现的最快的解决方法(除了降级到 1.3,这个问题不存在),是在创建别名之前用字段索引文档。

如果您有一个包含多个租户的索引,您应该只需要在创建索引时为一个包含必填字段的文档建立一次索引,然后您应该能够创建别名。

如果您尝试我在 GitHub 问题中发布的复制案例,但在创建别名之前,请运行以下命令:

curl -XPOST  'http://localhost:9200/repro/dummytype/1' -d '{
   "testfield": "dummyvalue"
}'

然后您应该能够在字段上添加过滤别名testfield

编辑 - 对第一条评论的回答: 我认为在模板中使用映射时这是一种疏忽。创建索引时,会将模板应用于匹配索引。我认为这里的问题是模板的通用映射部分在获得文档索引之前并未实际应用。如果您将问题中的模板更改为以下内容,则可以观察到此行为:

curl -XPUT 'http://localhost:9200/_template/repro' -d '{
  "template": "repro",
  "settings": {
    "index.number_of_shards": 1,
    "index.number_of_replicas": 0
  },
  "mappings": {
    "dummytype": {
      "properties": {
        "testfield": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}'

然后,您可以创建索引并添加过滤后的别名,而无需为任何文档编制索引。

正如我所说,我认为这是 ES 模板应用程序中的一个错误。

于 2015-03-11T22:33:31.090 回答