小编典典

禁用仅对Elasticsearch上的特定索引创建动态映射?

elasticsearch

我正在尝试仅针对特定索引而不是对所有索引禁用动态映射创建。由于某种原因,我无法将 默认 映射与’dynamic’:’false’
放在一起。因此,在这里我看到了两个选项:

  1. 指定属性 “index.mapper.dynamic” 文件 elasticsearch.yml
  2. “ index.mapper.dynamic” 放在索引创建时,如此处https://www.elastic.co/guide/en/kibana/current/setup.html#kibana-dynamic-mapping所述

第一个选项只能接受以下值:true,false和strict。所以没有办法像我们用的特性模式做来指定具体指标的子集(
“action.auto_create_index”
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-
index_.html#索引创建)。

第二种选择是行不通的。我已经创建了索引

POST http://localhost:9200/test_idx/
{
    "settings" : {
        "mapper" : {
            "dynamic" : false
        }
    },
    "mappings" : {
        "test_type" : {
            "properties" : {
                "field1" : {
                    "type" : "string"
                }
            }
        }
    }
}

然后检查索引设置:

GET http://localhost:9200/test_idx/_settings    
{
    "test_idx" : {
        "settings" : {
            "index" : {
                "mapper" : {
                    "dynamic" : "false"
                },
                "creation_date" : "1445440252221",
                "number_of_shards" : "1",
                "number_of_replicas" : "0",
                "version" : {
                    "created" : "1050299"
                },
                "uuid" : "5QSYSYoORNqCXtdYn51XfA"
            }
        }
    }
}

和映射:

GET http://localhost:9200/test_idx/_mapping
{
    "test_idx" : {
        "mappings" : {
            "test_type" : {
                "properties" : {
                    "field1" : {
                        "type" : "string"
                    }
                }
            }
        }
    }
}

到目前为止,到目前为止,让我们使用未声明的字段索引文档:

POST http://localhost:9200/test_idx/test_type/1
{
    "field1" : "it's ok, field must be in mapping and in source",
    "somefield" : "but this field must be in source only, not in mapping"
}

然后,我再次检查了映射:

GET http://localhost:9200/test_idx/_mapping
{
    "test_idx" : {
        "mappings" : {
            "test_type" : {
                "properties" : {
                    "field1" : {
                        "type" : "string"
                    },
                    "somefield" : {
                        "type" : "string"
                    }
                }
            }
        }
    }
}

如您所见,无论索引设置为“ dynamic”,映射都将扩展:false。我也尝试完全按照文档中的描述创建索引

PUT http://localhost:9200/test_idx
{
    "index.mapper.dynamic": false
}

但有相同的行为。

也许我错过了什么?

在此先多谢!


阅读 478

收藏
2020-06-22

共1个答案

小编典典

您快到了:该值需要设置为strict。正确的用法如下:

PUT /test_idx
{
  "mappings": {
    "test_type": {
      "dynamic":"strict",
      "properties": {
        "field1": {
          "type": "string"
        }
      }
    }
  }
}

再进一步说明一下,如果您想甚至禁止创建新类型,不仅要禁止该索引中的字段,请使用以下命令:

PUT /test_idx
{
  "mappings": {
    "_default_": {
      "dynamic": "strict"
    },
    "test_type": {
      "properties": {
        "field1": {
          "type": "string"
        }
      }
    }
  }
}

没有_default_模板:

PUT /test_idx
{
  "settings": {
    "index.mapper.dynamic": false
  },
  "mappings": {
    "test_type": {
      "dynamic": "strict",
      "properties": {
        "field1": {
          "type": "string"
        }
      }
    }
  }
}
2020-06-22