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

时间:2015-10-21 15:45:44

标签: elasticsearch

我试图仅针对特定索引禁用动态映射创建,而不是针对所有索引。出于某种原因,我无法将默认映射与动态' :' false'。 所以,我可以看到两个选项:

  1. 在文件 elasticsearch.yml 中指定属性" index.mapper.dynamic'
  2. 在索引创建时输入" index.mapper.dynamic' ,如此处所述https://www.elastic.co/guide/en/kibana/current/setup.html#kibana-dynamic-mapping
  3. 第一个选项可能只接受值: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" :假的。 我还尝试完全按照文档

    中的描述创建索引
    PUT http://localhost:9200/test_idx
    {
        "index.mapper.dynamic": false
    }
    

    但也有同样的行为。

    也许我错过了什么?

    提前多多感谢!

5 个答案:

答案 0 :(得分:13)

你几乎就在那里:价值需要设置为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"
        }
      }
    }
  }
}

答案 1 :(得分:4)

你必须知道,下面的部分只是意味着ES无法动态创建一个类型。

"mapper" : {
        "dynamic" : false
    }

您应该像这样配置ES:

PUT http://localhost:9200/test_idx/_mapping/test_type
{
  "dynamic":"strict"
}

然后你不能索引没有映射的其他字段,并得到如下错误:

mapping set to strict, dynamic introduction of [hatae] within [data] is not allowed

如果您想存储数据,但不能将该字段设为索引,则可以采用以下设置:

PUT http://localhost:9200/test_idx/_mapping/test_type
{
  "dynamic":false
}

希望这些可以帮助处理同样问题的人:)。

答案 2 :(得分:1)

答案在文档(7x。)中:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/dynamic.html

动态设置控制是否可以添加新字段 是否动态。它接受三种设置:

真实

新检测到的字段将添加到映射中。 (默认)

错误

新检测到的字段将被忽略。这些字段不会被索引,因此 将无法搜索,但仍会出现在的_source字段中 返回的点击数。这些字段不会添加到映射中,新的 字段必须明确添加。

严格

如果检测到新字段,则会引发异常,并且文档 拒绝。必须将新字段明确添加到映射中。

PUT my_index
{
  "mappings": {
    "dynamic": "strict", 
    "properties": {
      "user": { 
        "properties": {
          "name": {
            "type": "text"
          },
          "social_networks": { 
            "dynamic": true,
            "properties": {}
          }
        }
      }
    }
  }
}

答案 3 :(得分:0)

您无法再在ES 7中禁用动态映射,如果您拥有完全非结构化的数据,您可以做的就是完全禁用索引的映射,如下所示:

curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "enabled": false 
  }
}
'

如果您使用的是python,则可以执行以下操作:

from elasticsearch import Elasticsearch

# Connect to the elastic cluster
es=Elasticsearch([{'host':'localhost','port':9200}])

request_body = {
        "mappings": {
                 "enabled": False
    }
}
es.indices.create(index = 'my_index', body = request_body)

答案 4 :(得分:0)

对于 ES 7,如果您想更新现有索引:

PUT customers/_mapping
{
  "dynamic": "strict"
}
相关问题