我正在尝试仅对特定索引禁用动态映射创建,而不是对所有索引。由于某种原因,我无法将默认映射设置为“dynamic”:“false”。 因此,据我所知,这里留下了两个选项:

  1. 在文件elasticsearch.yml中指定属性'index.mapper.dynamic'
  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#index-creation 的模式所做的那样)。

第二个选项不起作用。 我已经创建了索引

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 
} 

但得到了相同的行为。

也许我错过了什么?

提前非常感谢!

请您参考如下方法:

您已经快完成了:该值需要设置为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" 
        } 
      } 
    } 
  } 
} 


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!