elasticSearch 通过curl命令存入数据

 

PUT是幂等方法,而POST并不是。

  PUT用于更新操作,POST用于新增操作比较合适。

  PUT,DELETE操作是幂等的,所谓幂等就是指不管进行多少次操作,结果都一样。

        比如,我用PUT修改一篇文章,然后在做同样的操作,每次操作后的结果并没有不同,DELETE也是一样。

  POST操作不是幂等,比如常见的POST重复加载问题:当我们多次发出同样的POST请求后,其结果是创建出了若干的资源。

  还有一点需要注意的是,创建操作可以使用POST,也可以使用PUT。区别在于POST是作用在一个集合资源之上的(/articles),而PUT操作是作用在一个具体资源之上的(/articles/123),比如说很多资源使用数据库自增主键作为标识信息,而创建的资源的标识信息到底是什么只能由服务端提供,这个时候就必须使用POST。

curl -XPOST http://master:9200/zhouls/user/1 -d '{"name" : "john"  , "age" : 28}'
zhouls是索引,user是类型,1是id
curl -XPOST http://master:9200/zhouls/user -d '{"name" : "john"}'
如果,我们不指定id,则会自动随机生成
curl -XPUT http://master:9200/zhouls/user/2?op_type=create -d '{"name" : "john"  , "age" : 28}'
[hadoop@master elasticsearch-2.4.0]$ curl -XPUT http://master:9200/zhouls/user/2?op_type=create -d '{"name" : "john"  , "age" : 28}'
{"_index":"zhouls","_type":"user","_id":"2","_version":1,"_shards":{"total":2,"successful":2,"failed":0},
"created":true}
[hadoop@master elasticsearch-2.4.0]$ curl -XPUT http://master:9200/zhouls/user/2/_create -d '{"name" : "john"  , "age" : 28}'
{"error":{"root_cause":[{"type":"document_already_exists_exception","reason":"[user][2]: document already exists","index":"zhouls","shard":"2"}],"type":"document_already_exists_exception","reason":"[user][2]: document already exists","index":"zhouls","shard":"2"},"status":409}
PUT是不允许,已经创建过的id,再来创建。

猜你喜欢

转载自blog.csdn.net/quliuwuyiz/article/details/88707450
今日推荐