1. 程式人生 > 實用技巧 >ES常用restful語句

ES常用restful語句

目錄

ES常用restful語句

1. 檢視基本配置

1.1 檢視配置

# 獲取單個index(庫的配置)
GET /index/_settings   

# 獲取全部配置,全部庫的配置
GET /_settings/_all     

1.2 檢視所有索引

GET /_cat/indices?v

1.2 檢視索引中所有欄位

GET /index/type/_mapping

2. query DSL(Domain Sepcified Language)

2.1 簡單查詢

GET /index/type/_search?q=id:300421

2.2 獲取所有記錄

# 獲取所有記錄
POST /index/type/_search
{
    "query":{"match_all":{}}
}

# 查詢 es 中所有的文件,並按照某一個條件排序
GET /index/type/_search?q=*&sort=_id:asc&pretty

2.3 根據條件查詢

GET /index/type/_search
{
    "query":{
          "match":{
                    "name":"zhangsan"
          }
    },
    "sort":[
        {
             "age":"desc"
       }
   ]
}

2.4 分頁查詢

GET /index/type/_search
{
    "query":{"match_all":{}},
    "from":1,
    "size":2
}

2.5 指定查詢結果的欄位

GET /index/type/_search
{
    "query":{"match_all":{}},
    "_source":["name","age"]
}

2.6 query filter

GET /index/type/_search
{
    "query":{
          "bool":{
                    "must":{
                             "match":{
                                   "name":"zhangsan"
                             }
                     },
                     "filter":{
                              "range":{
                                      "age":{"gt":25}
                             }
                    }
          }
    },
    "sort":[
        {
             "age":"desc"
       }
   ]
}
GET /index/type/_search
{ 
    "query":{
          "match":{
                  "name":"zhangsan"
            }              
    } 
}

2.8 phrase search(短語搜尋:完全匹配)

GET /index/type/_search
{ 
    "query":{
          "match_phrase":{
                  "name":"zhangsan"
            }              
    } 
}

2.9 highlight search(高亮搜尋)

GET /index/type/_search
{ 
    "query":{
          "match_phrase":{
                  "name":"zhangsan"
            }              
    } ,
   "highlight":{
              "fields":{
                    "name":{}
               }
   }
}

3.新增資料

3.1 插入資料:指定id

PUT /index/type/1 
{
	"name": "張三",
	"age": 20,
	"date": "2019-10-29"
}

3.2 插入資料:使用es預設建立的id

POST /index/type
{
	"name": "李四",
	"age": 29,
	"date": "2019-10-31"
}

4.修改資料

4.1 向已有索引中新增新欄位

PUT /index/type/_mapping
{
    "properties": {
        "name": {
            "type": "text", # 這裡為增量更新,原有欄位一定要保留,否則失敗
            "fields": { # 新增的部分
                "standard": {
                    "type": "text",
                    "analyzer": "standard"
                }
            },
            "analyzer": "my_ik_max_word",
            "search_analyzer": "my_ik_smart"
        }
}

4.2 批量更新某個欄位的值

POST /index/type/_update_by_query
{
   "script": {
        "inline": "ctx._source.name = params.name;ctx._source.age=params.name", 
       "params": {
           "name": "zhangsan",
           "age": ctx._source.age + 1 # 這裡也可以為通過本欄位中某個值通過計算後再賦值
       },
        "lang": "painless" # painless 高效能模式
    },
    "query": { # 這裡為篩選條件,會更新滿足條件的值,不寫或match_all會更新全部
        "terms": {
            "_id": ["1"]
        }
    }
}

5. 刪除資料

5.1 刪除指定id

POST /index/type/_delete_by_query
{
    "query": {
        "terms": {
            "_id": ["123"]
        }
    }
}

備註:這裡只寫了部分情況的,後續會根據學習工作過程中遇到的新需求不斷補充完善。