1. 概念
REST 指的是一组架构约束条件和原则 RESTful 是满足这些约束条件和原则的应用程序或设计 参考文档: https://www.elastic.co/guide/en/elasticsearch/reference/6.6/query-dsl.html
2. Web REST原则
Web 应用程序最重要的 REST 原则是,客户端和服务器之间的交互在请求之间是无状态的。从客户端到服务器的每个请求都必须包含理解请求所必需的信息。 如果服务器在请求之间的任何时间点重启,客户端不会得到通知。所有资源都共享统一的接口,以便在客户端和服务器之间传输状态。 使用的是标准的 HTTP 方法,比如 GET、PUT、POST 和 DELETE。
3.REST基本操作
GET: 获取对象的当前状态 PUT: 改变对象的状态 POST: 创建对象 DELETE:删除对象 HEAD:获取头信息
4. curl 创建方法
创建索引库: curl -XPUT 'http://10.10.10.46:9200/test/' curl -XPUT 'http://10.10.10.46:9200/test2/'
成功标志:
[elk@localhost root]$ curl -XPUT 'http://10.10.10.46:9200/test/'
{"acknowledged":true,"shards_acknowledged":true,"index":"test"}
新增数据:
curl -H "Content-Type: application/json" -XPOST http://10.10.10.46:9200/test/user/1 -d '{"name" : "jack","age" : 28}'
curl -H "Content-Type: application/json" -XPUT http://10.10.10.46:9200/test/user/1 -d '{"name" : "jack","age" : 28}'
curl -H "Content-Type: application/json" -XPOST http://10.10.10.46:9200/test/user/1 -d '{"name" : "john"}'
curl -H "Content-Type: application/json" -XPOST http://10.10.10.46:9200/test/user/ -d '{"name" : "thmoas"}'
curl -H "Content-Type: application/json" -XPUT http://10.10.10.46:9200/test/user/2?op_type=create -d '{"name" : "lucy","age" : 28}'
curl -H "Content-Type: application/json" -XPUT http://10.10.10.46:9200/test/user/3/_create -d '{"name" : "lily","age" : 18}'
5.PUT和POST区别
创建操作都可以使用POST,也可以使用PUT 区别在于POST是作用在一个集合资源之上的;PUT操作是作用在一个具体资源之上的; 如果资源使用数据库自增主键,没有指明标识,这个时候必须使用POST
6.curl查询方法
curl -XGET http://10.10.10.46:9200/blog/article/_search?pretty&q=content:一 curl -XGET 'http://10.10.10.46:9200/test/user/1?_source=name&pretty' curl -XGET http://10.10.10.46:9200/test/user/_search?pretty
curl -H "Content-Type: application/json" -XPUT http://10.10.10.46:9200/test/user/4/_create -d '{"name" : "qiqi","age" : 18}'
curl -H "Content-Type: application/json" -XPUT 'http://10.10.10.46:9200/blog/article/1?pretty' -d'{"title": "elktest", "content": "YZYX成立于2019年5月12日专注于青少年教育"}';
curl -H "Content-Type: application/json" -XGET http://10.10.10.46:9200/test/user/_search -d '{"query":{"match":{"name":"qiqi"}}}'
8.MGET查询
curl -XPUT '
curl -H "Content-Type: application/json" -XPOST http://10.10.10.46:9200/test2/user/1 -d '{"name" : "Green","age" : 28}'
curl -H "Content-Type: application/json" -XGET http://10.10.10.46:9200/_mget?pretty -d
'{"docs":[{"_index":"test","_type":"user","_id":2,"_source":"name"},{"_index":"test2","_type":"user","_id":1}]}'
curl -H "Content-Type: application/json" -XGET http://10.10.10.46:9200/test/user/_mget?pretty -d '{"docs":[{"_id":1},{"_id":2}]}'
9.更新操作(必须POST)
curl -H "Content-Type: application/json" -XPOST http://10.10.10.46:9200/test/user/1/_update -d '{"doc":{"name":"baby","age":18}}'
curl -H "Content-Type: application/json" -XPOST http://10.10.10.46:9200/test5/user/2/_update -d '{"doc":{"sexy":"oldman"}}';
10.删除操作
curl -XDELETE http://10.10.10.46:9200/test/user/1
11.补充释义
curl,全称CommandLine URL 或 CommandLine Uniform Resource Locator,顾名思义,curl命令是在命令行方式下工作,利用URL的语法进行数据的传输或者文件的传输。 -X 指定http 的请求方法, 有 HEAD GET POST PUT DELETE -d 指定要传输的数据 -H 指定http请求头信息
本文深入探讨了RESTful API的设计原则及其在Elasticsearch中的应用,详细讲解了如何使用curl命令进行索引、查询和管理操作,涵盖了创建索引、新增数据、查询方法及更新和删除操作。

214

被折叠的 条评论
为什么被折叠?



