Elasticsearch 入门实战(3)--REST API 使用一(CAT,Index,Document,Ingest,SQL APIs)
本文主要介绍 Elasticsearch REST API 中的 CAT,Index,Document,Ingest APIs,相关的环境及软件信息如下:CentOS 7.6.1810、Elasticsearch 8.13.4。
1、REST API 语法
curl -X <VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
说明:
参数 | 说明 |
<VERB> | 请求方法,如:GET,POST,PUT,HEAD 或 DELETE |
<PROTOCOL> | 协议,http 或 https |
<HOST> | 主机 |
<PORT> | Elasticsearch 服务端口,默认为9200 |
<PATH> | API 端点 |
<QUERY_STRING> | 请求参数 |
<BODY> | JSON 格式的请求体 |
2、Compact and aligned text (CAT) APIs
JSON 对计算机来说很棒……即使它被格式化输出,尝试在数据中找到关系也是很乏味的。人的眼睛,特别是在查看终端时,需要紧凑而对齐的文本。紧凑和对齐的文本(CAT)API旨在满足这种需求。cat API 是提供给人在 Kibana 控制台或命令行中使用的,不适合应用程序调用。
2.1、列出所有可用的命令
curl -X GET "http://10.49.196.11:9200/_cat"
2.2、命令通用参数
所有命令都接受以下通用参数:
Verbose:对应查询字符串为 v,用于打印头信息
Help:对应查询字符串 help,用于查看可输出的列
2.3、cat count API(查看文档数量)
语法:
GET /_cat/count/<target>
GET /_cat/count
样例:
curl -X GET "http://10.49.196.11:9200/_cat/count?v" #查看所有文档数量 curl -X GET "http://10.49.196.11:9200/_cat/count/my*?v" #查看以my开头的索引的文档数量
2.4、cat health API(查看集群健康状况)
语法:
GET /_cat/health
样例:
curl -X GET "http://10.49.196.11:9200/_cat/health?v=true"
2.5、cat nodes API(查看集群节点信息)
语法:
GET /_cat/nodes
样例:
curl -X GET "http://10.49.196.11:9200/_cat/nodes?v"
2.6、cat indices API(查看索引信息)
语法:
GET /_cat/indices/<target>
GET /_cat/indices
样例:
curl -X GET "http://10.49.196.11:9200/_cat/indices?v"
3、Index APIs
3.1、Create index API(创建索引)
语法:
PUT /<index>
样例:同时设置了 setting 和 mapping 信息;setting 里面包含索引的配置信息,mapping 里包含字段设置的详细信息。
curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index' -d ' { "settings": { "index": { "number_of_shards": 2, "number_of_replicas": 1 } }, "mappings": { "properties": { "age": { "type": "integer" }, "name": { "type": "keyword" }, "poems": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "about": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "success": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } } }'
3.2、Delete index API(删除索引)
语法:
DELETE /<index>
样例:
curl -X DELETE 'http://10.49.196.11:9200/poet-index'
3.3、Get index API(查看索引信息)
语法:
GET /<target>
样例:
curl -X GET "http://10.49.196.11:9200/*" curl -X GET "http://10.49.196.11:9200/_all" curl -X GET 'http://10.49.196.11:9200/poet-index' #查询单个索引
3.4、Get mapping API(查询 mapping 信息)
语法:
GET /_mapping
GET /<target>/_mapping
样例:
curl -X GET 'http://10.49.196.11:9200/poet-index/_mapping' #查询所有字段信息 curl -X GET 'http://10.49.196.11:9200/poet-index/_mapping/field/age,name' #查询某些字段信息
3.5、Update mapping API(修改 mapping 信息)
语法:
PUT /<target>/_mapping
样例:
curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_mapping' -d ' { "properties": { "success": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "desc": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } }'
可以新增字段,已有的字段只能修改字段的 search_analyzer 属性。
3.6、Get index settings API(查询 setting 信息)
语法:
GET /<target>/_settings
GET /<target>/_settings/<setting>
样例:
curl -X GET 'http://10.49.196.11:9200/poet-index/_settings' #查询所有参数 curl -X GET 'http://10.49.196.11:9200/poet-index/_settings/index.number_of_replicas' #查询单个参数 curl -X GET 'http://10.49.196.11:9200/poet-index/_settings?include_defaults=true' #返回中包含默认参数信息
3.7、Update index settings API(修改 setting 信息)
语法:
PUT /<target>/_settings
样例:
curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_settings' -d ' "index" : { "number_of_replicas" : 2 } '
默认只能更新动态参数,如果需要更新静态参数(只能创建时设置或关闭时更新),需要添加 reopen=true 参数,该参数会先关闭索引然后打开索引;动静态参数各有那些可参考官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html。
3.8、Analyze API(查看分词器分词结果)
语法:
GET /_analyze POST /_analyze GET /<index>/_analyze POST /<index>/_analyze
样例:
curl -X GET -H 'Content-Type:application/json' 'http://10.49.196.11:9200/_analyze' -d ' { "analyzer" : "ik_max_word", "text" : "唐代伟大的现实主义文学作家" }'
4、Document APIs
4.1、Index API(创建/覆盖文档)
语法:
PUT /<target>/_doc/<_id> POST /<target>/_doc/ PUT /<target>/_create/<_id> POST /<target>/_create/<_id>
样例1,没有对应 id 的文档就创建,有就覆盖更新所有字段(相当于先删除再新增)并更新版本号。
curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_doc/1' -d '
{
"age": 31,
"name": "李白",
"poems": "静夜思",
"about": "字太白"
}'
样例2,不设置 id 将新增,id 将自动生成。
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_doc' -d '
{
"age": 31,
"name": "杜甫",
"poems": "登高",
"about": "字子美",
"success": "唐代伟大的现实主义文学作家,唐诗思想艺术的集大成者"
}'
样例3,新增文档,需要设置 id。
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_create/1' -d ' { "age": 30, "name": "李白", "poems": "静夜思", "about": "字太白", "success": "创造了古代浪漫主义文学高峰、歌行体和七绝达到后人难及的高度" }'
4.2、Delete API(删除文档)
语法:
DELETE /<index>/_doc/<_id>
样例:
curl -X DELETE 'http://10.49.196.11:9200/poet-index/_doc/1'
4.3、Get API(根据 id 查询文档)
语法:
GET <index>/_doc/<_id> HEAD <index>/_doc/<_id> GET <index>/_source/<_id> HEAD <index>/_source/<_id>
样例:
curl -X GET 'http://10.49.196.11:9200/poet-index/_doc/1'
4.4、Update API(修改文档)
只更新明确设置的字段。
语法:
POST /<index>/_update/<_id>
样例:
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_update/100?docAsUpsert=true' -d ' { "doc": { "age": 32, "poems": "望庐山瀑布" }, "doc_as_upsert": false }'
doc_as_upsert 设置为 true 时,文档不存在时将插入。
4.5、Bulk API(批量操作)
语法:
POST /_bulk
POST /<target>/_bulk
样例:
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/_bulk' -d ' {"create":{"_index": "poet-index", "_id": "333"}} #新增操作 {"age": 30, "name": "杜甫11", "poems": "登高","about": "字子美","success": "唐代伟大的现实主义文学作家,唐诗思想艺术的集大成者"} {"update": {"_index": "poet-index", "_id": "11"}} #修改操作 {"doc": {"age": 38}} {"index":{"_index": "poet-index", "_id": "22"}} #新增或覆盖操作 {"age": 30, "name": "杜甫12", "poems": "登高", "about": "字子美", "success": "唐代伟大的现实主义文学作家,唐诗思想艺术的集大成者"} {"delete": {"_index": "poet-index", "_id": "12"}} #删除操作 '
注:最后的空行是需要的,否则会报错。
4.6、Delete by query API(查询删除)
根据查询结果删除数据。
语法:
POST /<target>/_delete_by_query
样例:
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_delete_by_query' -d ' { "query": { "term": { "name": { "value": "李白" } } } }'
4.7、Update By Query API(查询更新)
根据查询结果更新数据,如果不指定查询条件将更新所有数据。
语法:
POST /<target>/_update_by_query
样例:
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/poet-index/_update_by_query' -d ' { "script": { "source": "ctx._source.age=66" }, "query": { "term": { "name": { "value": "杜甫" } } } }'
5、Ingest APIs
5.1、Create or update pipeline API(创建或修改管道)
语法:
PUT /_ingest/pipeline/<pipeline>
样例:
curl -X PUT -H 'Content-Type:application/json' 'http://10.49.196.11:9200/_ingest/pipeline/my-pipeline' -d ' { "description" : "My optional pipeline description", "processors" : [{ "set": { "field": "test-field", "value": "test-value" } }] }'
5.2、Delete pipeline API(删除管道)
语法:
DELETE /_ingest/pipeline/<pipeline>
样例:
curl -X DELETE 'http://10.49.196.11:9200/_ingest/pipeline/my-pipeline'
5.3、Simulate pipeline API(模拟管道处理)
语法:
POST /_ingest/pipeline/<pipeline>/_simulate GET /_ingest/pipeline/<pipeline>/_simulate POST /_ingest/pipeline/_simulate GET /_ingest/pipeline/_simulate
样例1,模拟已定义的管道:
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/_ingest/pipeline/my-pipeline/_simulate' -d ' { "docs": [{ "_index": "my-index", "_id": "123", "_source": { "test-field": "aa" } }] }'
样例2,管道定义在请求体里:
curl -X POST -H 'Content-Type:application/json' 'http://10.49.196.11:9200/_ingest/pipeline/_simulate' -d ' { "pipeline": { "description": "My optional pipeline description", "processors": [{ "set": { "field": "test-field", "value": "test-value" } }] }, "docs": [{ "_index": "my-index", "_id": "123", "_source": { "test-field": "aa" } }] }'
6、SQL APIs
6.1、SQL search API(SQL) 查询
语法:
GET _sql
POST _sql
例子:
curl -X GET -H 'Content-Type:application/json' 'http://10.1.101.64:9200/_sql' -d ' { "query": "SELECT * FROM \"poet-index\" WHERE name=? limit 3", "params": ["杜甫"] }'
详细的 Elasticsearch REST API 使用说明,请参考官网文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html。