类型中的elasticsearch文档必须具有相同的字段结构

时间:2015-05-02 23:34:23

标签: elasticsearch

我正在研究elasticsearch指南中的示例。对于包含以下对象的类型:

{
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }

我可以将对象编入索引:

{ “middle_name”:“lee”, “年龄”:36 }

因此它缺少字段,而另一个字段则不在另一个字段中。

所有这些字段是否仍被编入索引?或者每个文档都必须有相同的结构。

1 个答案:

答案 0 :(得分:3)

默认情况下,Elasticsearch使用dynamic mapping,这意味着第一次看到新字段时,它会为其创建映射。

在您的情况下,字段middle_name是新的,因此Elasticsearch将自动为您创建字段映射:

{
"middle_name": {
  "type":"string"
  }
}

所以是的,额外字段将被编入索引。并非所有文件都必须包含所有字段。

但是,所有相同的字段在所有文档中必须具有相同的类型。也就是说,如果您尝试将文档{"age":26}和文档{"age":"old"}编入索引,则会出现错误,因为在第一种情况下,字段是数字,在第二种情况下是字符串:

{
   "error": "MapperParsingException[failed to parse [age]]; nested: NumberFormatException[For input string: \"adsf\"]; ",
   "status": 400
}

通过玩Marvel Sense,这一切都很容易尝试。通过在Elasticsearch文件夹中运行bin/plugin -i elasticsearch/marvel/latest来安装它,然后转到http://localhost:9200/_plugin/marvel/sense/index.html并尝试运行以下命令自行尝试:

put typetest
put typetest/doc/2
{
  "age":25
}
put typetest/doc/2
{
  "age":"old"
}
相关问题