一、集群(Cluster)信息相关API
1、查看集群健康信息
GET /_cat/health?v
2、查看集群中的节点信息
GET /_cat/nodes?v
3、查看集群中的索引信息
GET /_cat/indices?v
更多的查看和监视ES的API参见[官网文档]
二、索引(Index)相关API
1、创建一个新的索引
PUT /school
返回
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "school"
}
2、删除一个索引
DELETE /school
返回
{
"acknowledged" : true
}
更多的索引操作参见ES [官网文档]
三、映射(Mapping)相关API
1、创建索引的mapping
PUT /school/_mapping
{
"properties": {
"student": {
"properties": {
"name": {
"type": "text"
}
}
}
}
}
返回
{
"acknowledged" : true
}
2、查看索引的mapping
GET /school/_mapping/
返回
{
"school" : {
"mappings" : {
"properties" : {
"student" : {
"properties" : {
"name" : {
"type" : "text"
}
}
}
}
}
}
}
更多的mapping相关操作参加 [官网文档]
四、文档(document)相关API
1、新增一个文档
PUT /school/student/1
{
"name": "zhangsan",
"age" : "12"
}
返回
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
2、更新一个文档
POST /school/student/1 # 这里的1必须是索引中已经存在id,否则就会变成新增文档操作
{
"name": "lisi",
"age" : "12"
}
返回
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
3、删除一个文档
DELETE /school/student/1 #id必须存在
返回
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 3,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
4、查询单个文档
GET /school/student/1
返回
{
"_index" : "school",
"_type" : "student",
"_id" : "1",
"_version" : 4,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "zhangsan",
"age" : "12"
}
}
5、查询多好文档
GET /megacorp/employee/_search
{
"query": {
"match_all": {}
}
}
返回
{
"took" : 0,
"timed_out" : false,
…………
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests" : [
"forestry"
]
}
}
]
}
}
6、匹配查询match
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith" //搜索last_name有Smith
}
}
}
返回
{
"took" : 3,
"timed_out" : false,
…………
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.4700036,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 0.4700036,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.4700036,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
}
7、过滤查询filter
GET /megacorp/employee/_search
{
"query": {
"bool": {
"filter": [{
"range": {
"age": {
"gt": 30 //gt大于 lt小于 gte、ge大于等于 lte、le 小于等于
}
}
}]
}
}
}
返回
{
"took" : 0,
"timed_out" : false,
…………
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.0,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "3",
"_score" : 0.0,
"_source" : {
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests" : [
"forestry"
]
}
}
]
}
}
8、全文搜索about
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
返回
{
"took" : 10,
"timed_out" : false,
…………
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.4167401,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.4167401,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.4589591,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
}
9、短语搜索 match_phrase
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
返回
{
...
"hits": {
"total": 1,
"max_score": 0.23013961,
"hits": [
{
...
"_score": 0.23013961,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
}
]
}
}
10、高亮搜索
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
返回
{
...
"hits": {
"total": 1,
"max_score": 0.23013961,
"hits": [
{
...
"_score": 0.23013961,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
},
"highlight": {
"about": [
"I love to go <em>rock</em> <em>climbing</em>"
]
}
}
]
}
}
11、聚合 aggs
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
返回
{
...
"hits": { ... },
"aggregations": {
"all_interests": {
"buckets": [
{
"key": "music",
"doc_count": 2
},
{
"key": "forestry",
"doc_count": 1
},
{
"key": "sports",
"doc_count": 1
}
]
}
}
}