首页 > 解决方案 > Elasticsearch 别名和索引(相同的 id?)

问题描述

我有一个关于 Elasticsearch 的问题。

如果我有 2 个索引 IndexA 和 IndexB。

我将id为“1”的文档docA放在IndexA中,并将id为“1”的文档docB放在IndexB中。文档具有相同的文档类型但正文不同(结构相同但值不同)。

如果我创建一个指向 IndexA 和 IndexB 的别名“alias1”并执行以下获取请求,会发生什么?

响应 = es_client.get(index="alias1", doc_type="doc_type", id="1")

标签: elasticsearch

解决方案


它很容易测试,但简而言之,您不能为使用指向多个索引的文档和别名发出 GET !

PUT index1/doc/1
{ "test": 1 }

PUT index2/doc/1
{ "test": 2 }

POST _aliases
{
    "actions" : [
        { "add" : { "index" : "index1", "alias" : "alias1" } },
        { "add" : { "index" : "index2", "alias" : "alias1" } }
    ]
}

GET alias1/doc/1

=>

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Alias [alias1] has more than one indices associated with it [[index2, index1]], can't execute a single index op"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Alias [alias1] has more than one indices associated with it [[index2, index1]], can't execute a single index op"
  },
  "status": 400
}

推荐阅读