【问题标题】:Elasticsearch mapping not working as expectedElasticsearch 映射未按预期工作
【发布时间】:2016-02-10 19:18:13
【问题描述】:

具有以下映射:

curl -X PUT 'localhost:9200/cambio_indice?pretty=true' -d '{
  "mappings" : {
      "el_tipo" : {
       "properties" : {
            "name" : { "type" : "string" },
            "age" : { "type" : "integer" },
            "read" : { "type" : "integer" }
}}}}'

如果我添加以下代码,即使它与映射不匹配(read 缺失),它也能完美运行,但 ES 不会抱怨。

curl -X PUT 'localhost:9200/cambio_indice/el_tipo/1?pretty=true' -d '{
    "name" : "Eduardo Inda",
    "age" : 23
}'

如果我添加以下条目,它也可以工作。

curl -X PUT 'localhost:9200/cambio_indice/el_tipo/2?pretty=true' -d '{
    "jose" : "stuff",
    "ramon" : 23,
    "garcia" : 1
}'

映射似乎没有对我添加的元素生效。当我尝试映射我的类型时,我做错了什么?

【问题讨论】:

  • 您的期望是什么?
  • 输入时失败,或者显示错误you are trying to add a propierty that is not mapped

标签: elasticsearch


【解决方案1】:

这是 Elasticsearch 的默认行为,在大多数情况下都是可取的。但是对于您的情况,如果您不想允许对映射中未定义的字段进行索引,则需要更新映射并将其"dynamic" 属性设置为"strict"。基本上,您的映射定义应如下所示:

{
  "mappings": {
    "el_tipo": {
      "dynamic": "strict",
      "properties": {
        "name": {
          "type": "string" 
        },
        "age": {
          "type": "integer" 
        },
        "read": {
          "type": "integer" 
        }
      }
    }
  }
}

然后,如果您尝试索引“jose”、“ramon”或“garcia”等字段,Elasticsearch 将抛出一条适当的消息,指出禁止动态添加这些字段。

【讨论】:

    【解决方案2】:

    根据 ES 的文档:

    默认情况下,当数据添加到以前未创建的索引下时,Elasticsearch 会提供自动索引和映射。换句话说,可以将数据添加到 Elasticsearch 中,而无需事先定义索引和映射。这非常方便,因为 Elasticsearch 会自动适应提供给它的数据 - 此外,如果某些条目有额外的字段,Elasticsearch 无模式特性允许它们被索引而不会出现任何问题。

    因此,您添加的新字段将自动添加到您的映射中。

    更多信息请见this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多