Elasticsearch,Type Properties vs Type Mappings,有什么区别?

时间:2015-02-12 12:06:57

标签: types elasticsearch mappings

如果有人能帮助理解以下两种形式创建类型之间的真正区别,我将不胜感激:

使用“映射”

PUT / mybestfares_test1

{
    "mappings": {
        "bestfares_data": {
          "dynamic" : false,
            "properties": {
                "airline": {
                    "type": "string",
                    "index": "not_analyzed",
                    "null_value": "N/A"
                },
                "destinationAirport": {
                    "type": "string",
                    "index": "not_analyzed",
                    "null_value": "N/A"
                },
                "originAirport": {
                    "type": "string",
                    "index": "not_analyzed",
                    "null_value": "N/A"
                },
                "sellPrice": {
                    "type": "double",
                    "null_value": 0
                }
            }
        }
    }
}

不使用“Mappings”

PUT / mybestfares_test2 /

{
    "bestfares_data": {
        "dynamic" : false,
        "properties": {
            "airline": {
                "type": "string",
                "index": "not_analyzed",
                "null_value": "N/A"
            },
            "destinationAirport": {
                "type": "string",
                "index": "not_analyzed",
                "null_value": "N/A"
            },
            "originAirport": {
                "type": "string",
                "index": "not_analyzed",
                "null_value": "N/A"
            },
            "sellPrice": {
                "type": "double",
                "null_value": 0
            }
        }
    }
}

如果我获取这两个索引的索引信息,很明显“mybestfares_test2”没有任何“映射”定义,尽管该类型中的每个字段都有特定的设置:

GET / mybestfares_test2 =>

{
   "mybestfares_test2": {
      "mappings": {},
      "settings": {
         "index": {
            "creation_date": "1423741207570",
            "uuid": "ognGDfnTS7i9AVE1L66UgA",
            "number_of_replicas": "1",
            "number_of_shards": "5",
            "version": {
               "created": "1040299"
            },
            "bestfares_data": {
              "dynamic" : false,
               "properties": {
                  "destinationAirport": {
                     "type": "string",
                     "null_value": "N/A",
                     "index": "not_analyzed"
                  },
                  "sellPrice": {
                     "type": "double",
                     "null_value": "0"
                  },
                  "originAirport": {
                     "type": "string",
                     "null_value": "N/A",
                     "index": "not_analyzed"
                  },
                  "airline": {
                     "type": "string",
                     "null_value": "N/A",
                     "index": "not_analyzed"
                  }
               }
            }
         }
      }
   }
}

当然,使用映射创建的索引对映射{...}部分中的字段具有相同的设置 GET / mybestfares_test1

{
   "mybestfares_test1": {
      "mappings": {
         "bestfares_data": {
          "dynamic" : false,
            "properties": {
               "airline": {
                  "type": "string",
                  "index": "not_analyzed",
                  "null_value": "N/A"
               },
               "destinationAirport": {
                  "type": "string",
                  "index": "not_analyzed",
                  "null_value": "N/A"
               },
               "originAirport": {
                  "type": "string",
                  "index": "not_analyzed",
                  "null_value": "N/A"
               },
               "sellPrice": {
                  "type": "double",
                  "null_value": 0
               }
            }
         }
      },
      "settings": {
         "index": {
            "creation_date": "1423741360578",
            "uuid": "rZ8wc2-2TGKVo8ZVd8YIKg",
            "number_of_replicas": "1",
            "number_of_shards": "5",
            "version": {
               "created": "1040299"
            }
         }
      }
   }
}

我想了解这两个索引之间的行为(如果有的话)会有什么不同?

2 个答案:

答案 0 :(得分:0)

如果仔细查看响应,则在没有映射的情况下创建的第二个索引实际上会获得许多其他设置。因此,在这种情况下,您不提供映射。所以第一个是唯一正确的。

答案 1 :(得分:0)

来自https://www.found.no/foundation/elasticsearch-mapping-introduction/

有两种方法可以为Elasticsearch提供映射。最常见的方法是在创建索引期间:

$ curl -XPOST ...:9200/my_index -d '{
    "settings" : {
        # .. index settings
    },
    "mappings" : {
        "my_type" : {
            # mapping for my_type
        }
    }
}'

提供映射的另一种方法是使用Put Mapping API。

$ curl -XPUT 'http://localhost:9200/my_index/my_type/_mapping' -d '
{
    "my_type" : {
        # mapping for my_type
    }
}
'

此API使我们能够更新已存在索引的映射,但在潜在冲突方面存在一些限制。可以将新映射定义添加到现有映射,并且现有类型可以更新其配置,但更改类型被视为冲突,不被接受。但是,可以将ignore_conflicts = true作为参数传递给Mapping API,但这样做并不能保证产生预期结果,因为已经索引的文档不会使用新映射自动重新索引。

因此,建议在创建索引期间指定映射,而不是在大多数情况下使用Put Mapping API。