一、介绍
curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用url可以简单实现常见的get/post请求。简单的认为是可以在命令行下面访问url的一个工具。在centos的默认库里面是有curl工具的,如果没有请yum安装即可。
curl
-X 指定http的请求方法有 HEAD GET POST PUT DELETE
-d 指定要传输的数据
-H 指定http请求头信息
二、Index APIs
2.1、index create
aliases
索引别名
mappings
字段映射
settings
索引配置(副本数量、分片数量)
索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号
-
示例
-
settings设置
# 形式一
curl -XPUT 'http://10.143.228.25:9200/student?pretty' -H "Content-Type: application/json" -d '{"settings":{"number_of_shards":3,"number_of_replicas":0}}'
# 形式二
curl -XPUT 'http://10.143.228.25:9200/student?pretty' -H "Content-Type: application/json" -d '{"settings":{"index":{"number_of_shards":3,"number_of_replicas":2}}}'
json示例
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
response示例:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "student"
}
- mapping设置
curl -XPUT 'http://10.143.228.25:9200/student?pretty' -H "Content-Type: application/json" -d '{"settings":{"number_of_shards":3},"mappings":{"properties":{"field1":{"type":"text"}}}}'
json示例
{
"settings" : {
"number_of_shards" : 3
},
"mappings" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
response示例:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "student"
}
- aliases设置
curl -XPUT 'http://10.143.228.25:9200/student?pretty' -H "Content-Type: application/json" -d '{"aliases":{"alias_1":{},"alias_2":{"filter":{"term":{"user":"kimchy"}},"routing":"kimchy"}}}'
json示例
{
"aliases" : {
"alias_1" : {},
"alias_2" : {
"filter" : {
"term" : {"user" : "kimchy" }
},
"routing" : "kimchy"
}
}
}
response示例:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "student"
}
- 推荐形式
一次性创建索引,并配置settings、mapping、aliases。
curl -XPUT 'http://10.143.228.25:9200/student?pretty' -H "Content-Type: application/json" -d '{"settings":{"number_of_shards":3,"number_of_replicas":2},"mappings":{"properties":{"id":{"type":"long","index":true},"name":{"type":"text","index":true},"age":{"type":"integer"},"address":{"type":"text"},"birthday":{"type":"date","index":true}}},"aliases":{"alias_1":{}}}'
json示例
{
"settings": {
"number_of_shards" : 3,
"number_of_replicas" : 2
},
"mappings": {
"properties": {
"id": {
"type": "long",
"index": true
},
"name": {
"type": "text",
"index": true
},
"age": {
"type": "integer"
},
"address": {
"type": "text"
},
"birthday": {
"type": "date",
"index": true
}
}
},
"aliases": {
"alias_1": {}
}
}
response示例:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "student"
}
- 扩展阅读
2.2、index delete
curl -XDELETE 'http://10.143.228.25:9200/student?pretty'
response示例:
{
"acknowledged" : true
}
- 扩展阅读
2.3、delete index aliases
curl -XDELETE 'http://10.143.228.25:9200/student/_alias/alias_1?pretty'
response示例:
{
"acknowledged" : true
}
- 扩展阅读
2.4、flush
支持如下几种形式
POST /<index>/_flush
GET /<index>/_flush
POST /_flush
GET /_flush
示例一(单个索引):
curl -XPOST 'http://10.143.228.25:9200/student/_flush?pretty'
response示例:
{
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
}
}
示例一(多个索引):
curl -XPOST 'http://10.143.228.25:9200/student,company/_flush?pretty'
response示例:
{
"_shards" : {
"total" : 4,
"successful" : 4,
"failed" : 0
}
}
- 扩展阅读
2.5、flush
支持如下几种形式
POST /<index>/_flush
GET /<index>/_flush
POST /_flush
GET /_flush
示例一(单个索引):
curl -XPOST 'http://10.143.228.25:9200/student/_flush?pretty'
response示例:
{
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
}
}
示例一(多个索引):
curl -XPOST 'http://10.143.228.25:9200/student,company/_flush?pretty'
response示例:
{
"_shards" : {
"total" : 4,
"successful" : 4,
"failed" : 0
}
}
- 扩展阅读
2.6、get-field-mapping
支持如下几种形式
GET /_mapping/field/<field>
GET /<index>/_mapping/field/<field>
示例一:单个字段
curl -XGET 'http://10.143.228.25:9200/student/_mapping/field/name?pretty'
response示例:
{
"student" : {
"mappings" : {
"name" : {
"full_name" : "name",
"mapping" : {
"name" : {
"type" : "text"
}
}
}
}
}
}
示例二:多个字段
curl -XGET 'http://10.143.228.25:9200/student/_mapping/field/name,age?pretty'
response示例:
{
"student" : {
"mappings" : {
"name" : {
"full_name" : "name",
"mapping" : {
"name" : {
"type" : "text"
}
}
},
"age" : {
"full_name" : "age",
"mapping" : {
"age" : {
"type" : "integer"
}
}
}
}
}
}
- 扩展阅读
2.7、get-index
支持如下几种形式
GET /<index>
curl -XGET 'http://10.143.228.25:9200/student'
response示例:
{
"student" : {
"aliases" : {
"alias_1" : { }
},
"mappings" : {
"properties" : {
"address" : {
"type" : "text"
},
"age" : {
"type" : "integer"
},
"birthday" : {
"type" : "date"
},
"id" : {
"type" : "long"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1590209646606",
"number_of_shards" : "3",
"number_of_replicas" : "2",
"uuid" : "l8Ih8QYSQTub3uNAmqWRrA",
"version" : {
"created" : "7030299"
},
"provided_name" : "student"
}
}
}
}
- 扩展阅读
2.8、get-settings
支持如下几种形式
GET /<index>/_settings
GET /<index>/_settings/<setting>
# 单个
curl -XGET 'http://10.143.228.25:9200/student/_settings?pretty'
# 多个
curl -XGET 'http://10.143.228.25:9200/student,company/_settings?pretty'
response示例:
{
"student" : {
"settings" : {
"index" : {
"creation_date" : "1590209646606",
"number_of_shards" : "3",
"number_of_replicas" : "2",
"uuid" : "l8Ih8QYSQTub3uNAmqWRrA",
"version" : {
"created" : "7030299"
},
"provided_name" : "student"
}
}
}
}
response示例:
{
"student" : {
"settings" : {
"index" : {
"creation_date" : "1590209646606",
"number_of_shards" : "3",
"number_of_replicas" : "2",
"uuid" : "l8Ih8QYSQTub3uNAmqWRrA",
"version" : {
"created" : "7030299"
},
"provided_name" : "student"
}
}
},
"company" : {
"settings" : {
"index" : {
"creation_date" : "1589788313386",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "ySjkNiAEQySBPH7eZ0HaYw",
"version" : {
"created" : "7030299"
},
"provided_name" : "company"
}
}
}
}
- 扩展阅读
2.9、put-mapping
Adds new fields to an existing index or changes the search settings of existing fields.
支持如下几种形式
PUT /<index>/_mapping
PUT /_mapping
- 更新某个索引
curl -XPUT 'http://10.143.228.25:9200/student/_mapping?pretty' -H "Content-Type: application/json" -d '{"properties":{"address":{"type":"text"},"age":{"type":"integer"},"birthday":{"type":"date"},"id":{"type":"long"},"name":{"type":"text"}}}'
{
"properties": {
"address": {
"type": "text"
},
"age": {
"type": "integer"
},
"birthday": {
"type": "date"
},
"id": {
"type": "long"
},
"name": {
"type": "text"
}
}
}
response示例:
{
"acknowledged" : true
}
- 扩展阅读
2.10、more APIs
下面的是我的公众号二维码图片,欢迎关注。