#查看健康状态
GET _cat/health?v
#检查分片信息
GET _cat/shards?v
#查看索引信息
GET _cat/indices?v
#新增索引
PUT /test_index
GET /test_index
#删除索引
DELETE /test_index
#新增document
PUT /test_index/my_type/1
{
"name":"test_doc_01",
"remark":"first test elastic search",
"order_no":1
}
PUT /test_index/my_type/2
{
"name":"test_doc_02",
"remark":"second test elastic search",
"order_no":2
}
PUT /test_index/my_type/3
{
"name":"test_doc_03",
"remark":"third test elastic search",
"order_no":3
}
#新增 自动分配id
POST /test_index/my_type
{
"name":"test_doc_04",
"remark":"forth test elastic search",
"order_no":4
}
GET /test_index/my_type/1
#批量查询
GET /_mget
{
"docs" : [
{
"_index" : "test_index",
"_type" : "my_type",
"_id" : "1"
},
{
"_index" : "test_index",
"_type" : "my_type",
"_id" : "2"
}
]
}
GET /test_index/_mget
{
"docs" : [
{
"_type" : "my_type",
"_id" : "1"
},
{
"_type" : "my_type",
"_id" : "2"
}
]
}
GET /test_index/my_type/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
# 强制新增 已存在的document报错
PUT /test_index/my_type/1/_create
{
"name":"new_test_doc_01",
"remark":"first test elastic search",
"order_no":1
}
# 只更新某一些字段 底层执行跟全量更新无差别
POST /test_index/my_type/1/_update
{
"doc":{
"name":" test_doc_01_for_update"
}
}
# bulk操作
POST /_bulk
{ "create" : { "_index" : "test_index" , "_type" : "my_type", "_id" : "1" } }
{ "field_name" : "field value" }
POST /_bulk
{"index":{ "_index" : "test_index", "_type" : "my_type" , "_id" : "2"}}
{"field_name" : "field value 2"}
POST /_bulk
{ "delete" : { "_index" : "test_index", "_type" : "my_type", "_id" : "2" } }
# 全数据搜索
GET /test_index/my_type/_search
GET /test_index/my_type/_search?q=remark:test&sort=order_no:desc
#query DSL(特殊领域语言)
GET /test_index/my_type/_search
{
"query": {
"match": {
"remark": "test"
}
},
"sort": [
{
"order_no": {
"order": "asc"
}
}
]
}
#query filter 作为dsl的补充,过滤时候不做匹配分数计算
GET /person/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 27
}
}
}
}
}
}
# 分页
GET /test_index/my_type/_search
{
"query" : { "match_all" : {} },
"from" : 1,
"size" : 1,
"sort" : [
{ "order_no" : "asc" }
]
}
# 聚合操作
PUT /products_index/_mapping
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
GET /products_index/phone_type/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
GET /products_index/phone_type/_search
{
"size": 0,
"query": {
"match": {
"name": "PLUS"
}
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
GET /products_index/phone_type/_search
{
"query": {
"match": {
"name": "plus"
}
},
"aggs": {
"avg_by_price": {
"avg": {
"field": "price"
}
}
}
}
GET /products_index/phone_type/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
GET /products_index/phone_type/_search
{
"query": {
"match_all": {}
},
"_source": "price",
"aggs": {
"range_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 500000,
"to": 600000
},
{
"from": 600001,
"to": 800000
},
{
"from": 800001,
"to": 1000000
}
]
},
"aggs": {
"avg_by_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
常用元数据
_index _type _id _source _version
# external_version
PUT /products_index/phone_type/1?version=5&version_type=external
{
"name" : "iphone x",
"remark" : "256G",
"price" : 899900,
"producer" : "apple",
"tags" : [ "256G" ]
}
# 分页搜索
GET /_search?from=0&size=10