[基本命令] : curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/ <PATH>?<QUERY_STRING>' -d '<BODY>' // VERB HTTP方法: GET, POST, PUT, HEAD, DELETE // PROTOCOL http或者https协议 (只有在Elasticsearch前面有https代理的时候可用) // HOST Elasticsearch集群中的任何一个节点的主机名, 如果是在本地的节点, 那么就叫localhost // PORT Elasticsearch HTTP服务所在的端口, 默认为9200 // PATH API路径 (例如_count将返回集群中文档的数量), // - PATH可以包含多个组件, 例如_cluster/stats或者_nodes/stats/jvm // QUERY_STRING 一些可选的查询请求参数, 例如?pretty参数将使请求返回更加美观易读的JSON数据 // BODY 一个JSON格式的请求主体 (如果请求需要的话) // -i 显示响应头 => pretty 将使请求返回更加美观易读的JSON数据 # 查看 : curl 'http://localhost:9200/' : curl 'http://localhost:9200/?pretty'{ "name" : "localhost.localdomain", "cluster_name" : "elasticsearch", "cluster_uuid" : "75n5rGvyRi-6r5__H_ZA_g", "version" : { "number" : "7.2.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "508c38a", "build_date" : "2019-06-20T15:54:18.811730Z", "build_snapshot" : false, "lucene_version" : "8.0.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search"} # 关闭 : curl -XPOST 'http://localhost:9200/_shutdown'[] : curl -H "Content-Type: application/json" -XPUT 'http://192.168.10.10:9200/' -d '{}'
索引
[] # 查看所有索引 : GET /_cat/indices # 计算集群中的文档数量 : GET /_count?pretty -d '{"query": {"match_all": {}}}'{ "count" : 14325106, "_shards" : { "total" : 72, "successful" : 72, "skipped" : 0, "failed" : 0 }}[]
// JSON文档来表示一个用户对象
{
"email": "john@smith.com",
"first_name": "John",
"last_name": "Smith",
"info": {
"bio": "Eco-warrior and defender of the weak",
"age": 25,
"interests": [ "dolphins", "whales" ]
},
"join_date": "2014/05/01"
}
//
: PUT 新增更新文档
: GET/POST 查询搜索文档
: DELETE 删除文档
: HEAD 检查文档是否存在
[索引]
# 列出所有索引
: /_cat/indices?v
# 新建文档
: PUT /索引名/类型名/编号 {"字段": 值}
: curl -H "Content-Type: application/json" -XPUT 'http://192.168.10.10:9200/megacorp/employee/1' -d '{"first_name" : "John"}'
: PUT /megacorp/employee/1 {"first_name" : "John"}
// "_index":"索引","_type":"类型","_id":"文档编号"
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
[搜索]
# 直接查询文档 [/索引/类型/文档编号 , 直接定位唯一文档]
: GET /索引/类型/文档编号
{
"_index": "索引",
"_type": "类型",
"_id": "文档编号",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"字段": 值
}
}
# 简单搜索, 查询该索引该类型的索引文档 [/索引/类型/_search]
: _search
// GET /索引/类型/_search
// GET /索引/类型/_search?pretty
# 查询字符串(query string)搜索, 字段 first_name值为John 数据的文档
: GET /megacorp/employee/_search?q=first_name:John
# DSL语句查询 (以JSON形式传入请求内容)
: curl -H "Content-Type: application/json" \
-XGET 'http://192.168.10.10:9200/megacorp/employee/_search?pretty'\
-d '{ "query" : { "match" : { "first_name" : "John" } } }'
: GET /megacorp/employee/_search
{
"query" : {
"match" : {
"first_name" : "John"
}
}
}
# 复杂查询, DSL
// 添加过滤器 (filter)
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"last_name" : "smith"
}
}
}
}
}
# 全文搜索
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
# 短语搜索
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}