首页 > 解决方案 > ElasticSearch - 如何为每个别名创建索引模板/映射并分别对每个别名执行搜索

问题描述

是弹性存储每个别名的索引模板的任何方式。我的意思是创建具有多个别名(alias1、alias2 ..)的索引并将不同的模板附加到每个别名。然后对特定别名执行索引/搜索文档。

我这样做的原因是由于文档有多种不同的数据结构(最多 50 种)。

到目前为止我所做的是:

 1. PUT /dynamic_index

 2. POST /_aliases
       { "actions" : [
        { "add" : { "index" : "dynamic_index", "alias" : "alias_type1" } },
        { "add" : { "index" : "dynamic_index", "alias" : "alias_type2" } },
        { "add" : { "index" : "dynamic_index", "alias" : "alias_type3" } }
        ]}
 3. 

    PUT_template/template1 {
      "index_patterns": [
        "dynamic_index"
      ],
      "mappings": {
        "dynamic_templates": [
          {
            "strings_as_keywords": {
              "match_mapping_type": "string",
              "mapping": {
                "type": "text",
                "analyzer": "standard",
                "copy_to": "_all",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "normalizer": "lowercase_normalizer"
                  }
                }
              }
            }
          }
        ],
        "properties": {
          "source": {
            "type": "keyword"
          }
        }
      },
      "aliases": {
        "alias_type1": {
        }
      }
    }

4. same way to alias_type2 , alias_type3 but different fields ...

索引/搜索:尝试按别名创建和搜索文档,例如:

   POST alias_type1/_doc
    {
      "source": "foo" 
     , .....
      
    }

    POST alias_type2/_doc
    {
      "source": "foo123" 
     , .....
      
    }
    
    GET alias_type1/_search
    {
      "query": {
        "match_all": {}
      }
    }
  GET alias_type2/_search
    {
      "query": {
        "match_all": {}
      }
    }

我实际上看到的是,即使我为每个别名索引文档,在搜索时我看不到每个别名的结果,所有结果在 alias_type1,2 甚至索引上都是相同的。

有什么方法可以根据每种类型的搜索/索引文档(别名)在每个别名上实现分离逻辑?

有任何想法吗 ?

标签: elasticsearchelastic-stack

解决方案


对于指向同一索引的别名,您不能有单独的映射!别名就像指向索引的虚拟链接,因此如果您的别名指向相同的索引,您将得到相同的结果。如果您想根据数据结构进行不同的映射,则需要创建多个索引。

更新 您还可以使用基于字段的自定义路由以获取更多信息,您可以在此处查看 Elastic 官方文档。


推荐阅读