在弹性搜索中添加文档时 PUT 与 POST

Vis*_*eet 4 elasticsearch

我是 Elasticsearch 的新手,并试图在弹性索引中添加文档。我在这里对 PUT 和 POST 感到困惑,因为它们在以下场景中产生相同的结果:

curl -H "Content-Type: application/json" -XPUT "localhost:9200/products/mobiles/1?pretty" -d"
{
"name": "iPhone 7",
"camera": "12MP",
"storage": "256GB",
"display": "4.7inch",
"battery": "1,960mAh",
"reviews": ["Incredibly happy after having used it for one week", "Best iPhone so far", "Very expensive, stick to Android"]
}
"
Run Code Online (Sandbox Code Playgroud)

对比

curl -H "Content-Type: application/json" -XPOST "localhost:9200/products/mobiles/1?pretty" -d"
{
"name": "iPhone 7",
"camera": "12MP",
"storage": "256GB",
"display": "4.7inch",
"battery": "1,960mAh",
"reviews": ["Incredibly happy after having used it for one week", "Best iPhone so far", "Very expensive, stick to Android"]
}
"
Run Code Online (Sandbox Code Playgroud)

Lin*_*nPy 6

  • POST : 用于实现 id 的自动生成。
  • PUT :当你想指定一个 id 时使用。

看到这个