Elasticsearch映射无法按预期工作 [英] Elasticsearch mapping not working as expected

查看:54
本文介绍了Elasticsearch映射无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下映射:

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不会抱怨.

If I add the following code it works perfectly even though it doesn't match with the mapping (read is missing) but ES doesn't complain.

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

如果我添加以下条目,它也将起作用.

And if I add the following entry, it also works.

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

似乎该映射未对我添加的元素生效.尝试映射我的类型时,我做错了什么?

It seems that the mapping is not taking effect on the elements I'm adding. I'm doing something wrong when I try to map my type?

推荐答案

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

This is the default behaviour of Elasticsearch and is desirable in most of the cases. But for your case, if you do not want to allow indexing of fields not defined in your mapping, you need to update the mapping and set its "dynamic" property to "strict". Basically, your mapping definition should look like below:

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

然后,如果您尝试为"jose","ramon"或"garcia"之类的字段建立索引,Elasticsearch将抛出一条适当的消息,提示禁止动态添加这些字段.

Then if you try to index fields like "jose", "ramon" or "garcia", Elasticsearch will throw with an appropriate message saying that the dynamic addition of these fields is prohibited.

这篇关于Elasticsearch映射无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆