Elasticsearch必知必會的乾貨知識一:ES索引文檔的CRUD

来源:https://www.cnblogs.com/zuowj/archive/2020/01/18/12209702.html
-Advertisement-
Play Games

​ 若在傳統DBMS 關係型資料庫中查詢海量數據,特別是模糊查詢,一般我們都是使用like %查詢的值%,但這樣會導致無法應用索引,從而形成全表掃描效率低下,即使是在有索引的欄位精確值查找,面對海量數據,效率也是相對較低的,所以目前一般的互聯網公司或大型公司,若要查詢海量數據,最好的辦法就是使用搜索 ...


​ 若在傳統DBMS 關係型資料庫中查詢海量數據,特別是模糊查詢,一般我們都是使用like %查詢的值%,但這樣會導致無法應用索引,從而形成全表掃描效率低下,即使是在有索引的欄位精確值查找,面對海量數據,效率也是相對較低的,所以目前一般的互聯網公司或大型公司,若要查詢海量數據,最好的辦法就是使用搜索引擎,目前比較主流的搜索引擎框架就是:Elasticsearch,故今天我這裡總結了Elasticsearch必知必會的乾貨知識一:ES索引文檔的CRUD,後面陸續還會有其它乾貨知識分享,敬請期待。

  1. ES索引文檔的CRUD(6.X與7.X有區別,6.X中支持一個index創建多個type,而7.X中及以上只支持1個固定的type,即:_doc,API用法上也稍有不同):

    1. Create創建索引文檔【POST index/type/id可選,如果index、type、id已存在則重建索引文檔(先刪除後創建索引文檔,與Put index/type/id 原理相同),如果在指定id情況下需要限制自動更新,則可以使用:index/type/id?op_type=create 或 index/type/id/_create,指明操作類型為創建,這樣當存在的記錄的情況下會報錯】

      POST demo_users/_doc 或 demo_users/_doc/2vJKsm8BriJODA6s9GbQ/_create

      Request Body:

      {
      "userId":1,
      "username":"張三",
      "role":"administrator",
      "enabled":true,
      "createdDate":"2020-01-01T12:00:00"
      }

      Response Body:

      {
      "_index": "demo_users",
      "_type": "_doc",
      "_id": "2vJKsm8BriJODA6s9GbQ",
      "_version": 1,
      "result": "created",
      "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
      },
      "_seq_no": 0,
      "_primary_term": 1
      }
    2. Get獲取索引文檔【Get index/type/id】

      Get demo_users/_doc/123

      Response Body:

      {
      "_index": "demo_users",
      "_type": "_doc",
      "_id": "123",
      "_version": 1,
      "found": true,
      "_source": {
      "userId": 1,
      "username": "張三",
      "role": "administrator",
      "enabled": true,
      "createdDate": "2020-01-01T12:00:00"
      }
      }
    3. Index Put重建索引文檔【PUT index/type/id 或 index/type/id?op_type=index,id必傳,如果id不存在文檔則創建文檔,否則先刪除原有id文檔後再重新創建文檔,version加1】

      Put/POST demo_users/_doc/123 或 demo_users/_doc/123?op_type=index

      Request Body:

      {
      "userId":1,
      "username":"張三",
      "role":"administrator",
      "enabled":true,
      "createdDate":"2020-01-01T12:00:00",
      "remark":"僅演示"
      }

      Response Body:

      {
      "_index": "demo_users",
      "_type": "_doc",
      "_id": "123",
      "_version": 4,
      "result": "updated",
      "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
      },
      "_seq_no": 10,
      "_primary_term": 1
      }
    4. Update更新索引文檔【POST index/type/id/_update 請求體必需是{"doc":{具體的文檔JSON}},如果指定的鍵欄位已存在則更新,如果指定的鍵欄位不存在則附加新的鍵值對,支持多層級嵌套,多次請求,如果有欄位值有更新則version加1,否則提示更新0條 】

      POST demo_users/_doc/123/_update

      Request Body:

      {
        "doc": {
          "userId": 1,
          "username": "張三",
          "role": "administrator",
          "enabled": true,
          "createdDate": "2020-01-01T12:00:00",
          "remark": "僅演示POST更新5",
          "updatedDate": "2020-01-17T15:30:00"
        }
      }

      Response Body:

      {
      "_index": "demo_users",
      "_type": "_doc",
      "_id": "123",
      "_version": 26,
      "result": "updated",
      "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
      },
      "_seq_no": 35,
      "_primary_term": 1
      }
    5. Delete刪除索引文檔【DELETE index/type/id】

      DELETE demo_users/_doc/123

      Response Body:

      {
      "_index": "demo_users",
      "_type": "_doc",
      "_id": "123",
      "_version": 2,
      "result": "deleted",
      "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
      },
      "_seq_no": 39,
      "_primary_term": 1
      }
    6. Bulk批量操作文檔【POST _bulk 或 index/_bulk 或 index/type/_bulk 一次請求支持進行多個索引、多個type的多種不同的CRUD操作,如果操作中有某個出現錯誤不會影響其它操作;】

      POST _bulk

      Request Body:(註意最後還得多一個換行,因為ES是根據換行符來識別多條命令的,如果缺少最後一條換行則會報錯,註意請求體非標準的JSON,每行才是一個JSON,整體頂多可看成是\n區分的JSON對象數組)

      { "index" : { "_index" : "demo_users_test", "_type" : "_doc", "_id" : "1" } }
      { "bulk_field1" : "測試創建index" }
      { "delete" : { "_index" : "demo_users", "_type" : "_doc", "_id" : "123" } }
      { "create" : { "_index" : "demo_users", "_type" : "_doc", "_id" : "2" } }
      { "bulk_field2" : "測試創建index2" }
      { "update" : { "_index" : "demo_users_test","_type" : "_doc","_id" : "1" } }
      { "doc": {"bulk_field1" : "測試創建index1","bulk_field2" : "測試創建index2"} }
      
      

      Response Body:

      {
          "took": 162,
          "errors": true,
          "items": [
              {
                  "index": {
                      "_index": "demo_users_test",
                      "_type": "_doc",
                      "_id": "1",
                      "_version": 8,
                      "result": "updated",
                      "_shards": {
                          "total": 2,
                          "successful": 2,
                          "failed": 0
                      },
                      "_seq_no": 7,
                      "_primary_term": 1,
                      "status": 200
                  }
              },
              {
                  "delete": {
                      "_index": "demo_users",
                      "_type": "_doc",
                      "_id": "123",
                      "_version": 2,
                      "result": "not_found",
                      "_shards": {
                          "total": 2,
                          "successful": 2,
                          "failed": 0
                      },
                      "_seq_no": 44,
                      "_primary_term": 1,
                      "status": 404
                  }
              },
              {
                  "create": {
                      "_index": "demo_users",
                      "_type": "_doc",
                      "_id": "2",
                      "status": 409,
                      "error": {
                          "type": "version_conflict_engine_exception",
                          "reason": "[_doc][2]: version conflict, document already exists (current version [1])",
                          "index_uuid": "u7WE286CQnGqhHeuwW7oyw",
                          "shard": "2",
                          "index": "demo_users"
                      }
                  }
              },
              {
                  "update": {
                      "_index": "demo_users_test",
                      "_type": "_doc",
                      "_id": "1",
                      "_version": 9,
                      "result": "updated",
                      "_shards": {
                          "total": 2,
                          "successful": 2,
                          "failed": 0
                      },
                      "_seq_no": 8,
                      "_primary_term": 1,
                      "status": 200
                  }
              }
          ]
      }
    7. mGet【POST _mget 或 index/_mget 或 index/type/_mget ,如果指定了index或type,則請求報文中則無需再指明index或type,可以通過_source指明要查詢的include以及要排除exclude的欄位】

      POST _mget

      Request Body:

      {
        "docs": [
          {
            "_index": "demo_users",
            "_type": "_doc",
            "_id": "12345"
          },
          {
            "_index": "demo_users",
            "_type": "_doc",
            "_id": "1234567",
            "_source": [
              "userId",
              "username",
              "role"
            ]
          },
          {
            "_index": "demo_users",
            "_type": "_doc",
            "_id": "1234",
            "_source": {
              "include": [
                "userId",
                "username"
              ],
              "exclude": [
                "role"
              ]
            }
          }
        ]
      }

      Response Body:

      {
          "docs":[
              {
                  "_index":"demo_users",
                  "_type":"_doc",
                  "_id":"12345",
                  "_version":1,
                  "found":true,
                  "_source":{
                      "userId":1,
                      "username":"張三",
                      "role":"administrator",
                      "enabled":true,
                      "createdDate":"2020-01-01T12:00:00"
                  }
              },
              {
                  "_index":"demo_users",
                  "_type":"_doc",
                  "_id":"1234567",
                  "_version":7,
                  "found":true,
                  "_source":{
                      "role":"administrator",
                      "userId":1,
                      "username":"張三"
                  }
              },
              {
                  "_index":"demo_users",
                  "_type":"_doc",
                  "_id":"1234",
                  "_version":1,
                  "found":true,
                  "_source":{
                      "userId":1,
                      "username":"張三"
                  }
              }
          ]
      }

      POST demo_users/_doc/_mget

      Request Body:

      {
        "ids": [
          "1234",
          "12345",
          "123457"
        ]
      }

      Response Body:

      {
          "docs":[
              {
                  "_index":"demo_users",
                  "_type":"_doc",
                  "_id":"1234",
                  "_version":1,
                  "found":true,
                  "_source":{
                      "userId":1,
                      "username":"張三",
                      "role":"administrator",
                      "enabled":true,
                      "createdDate":"2020-01-01T12:00:00",
                      "remark":"僅演示"
                  }
              },
              {
                  "_index":"demo_users",
                  "_type":"_doc",
                  "_id":"12345",
                  "_version":1,
                  "found":true,
                  "_source":{
                      "userId":1,
                      "username":"張三",
                      "role":"administrator",
                      "enabled":true,
                      "createdDate":"2020-01-01T12:00:00"
                  }
              },
              {
                  "_index":"demo_users",
                  "_type":"_doc",
                  "_id":"123457",
                  "found":false
              }
          ]
      }
    8. _update_by_query根據查詢條件更新匹配到的索引文檔的指定欄位【POST index/_update_by_query 請求體寫查詢條件以及更新的欄位,更新欄位這裡採用了painless腳本進行靈活更新】

      POST demo_users/_update_by_query

      Request Body:(意思是查詢role=administrator【可能大家看到keyword,這是因為role欄位為text類型,無法直接匹配,需要藉助於子欄位role.keyword,如果有不理解後面會有簡要說明】,更新role為poweruser、remark為remark+採用_update_by_query更新)

      {
          "script":{ "source":"ctx._source.role=params.role;ctx._source.remark=ctx._source.remark+params.remark",
              "lang":"painless",
              "params":{
                  "role":"poweruser",
                  "remark":"採用_update_by_query更新"
              }
          },
          "query":{
              "term":{
                  "role.keyword":"administrator"
              }
          }
      }

      painless寫法請具體參考:painless語法教程

      Response Body:

      {
      "took": 114,
      "timed_out": false,
      "total": 6,
      "updated": 6,
      "deleted": 0,
      "batches": 1,
      "version_conflicts": 0,
      "noops": 0,
      "retries": {
      "bulk": 0,
      "search": 0
      },
      "throttled_millis": 0,
      "requests_per_second": -1,
      "throttled_until_millis": 0,
      "failures": [ ]
      }
    9. _delete_by_query根據查詢條件刪除匹配到的索引文檔【 POST index/_delete_by_query 請求體寫查詢匹配條件】

      POST demo_users/_delete_by_query

      Request Body:(意思是查詢enabled=false)

      {
        "query": {
          "match": {
            "enabled": false
          }
        }
      }

      Response Body:

         {
                 "took":29,
                 "timed_out":false,
                 "total":3,
                 "deleted":3,
                 "batches":1,
                 "version_conflicts":0,
                 "noops":0,
                 "retries":{
                     "bulk":0,
                     "search":0
                 },
                 "throttled_millis":0,
                 "requests_per_second":-1,
                 "throttled_until_millis":0,
                 "failures":[
      
                 ]
            }
    10. search查詢

      1. URL GET查詢(GET index/_search?q=query_string語法,註意中文內容預設分詞器是一個漢字拆分成一個term

        
        A.Term Query:【即分詞片段(詞條)查詢,註意這裡講的包含是指與分詞片段匹配】
        GET /demo_users/_search?q=role:poweruser //指定欄位查詢,即:欄位包含查詢的值
        
        GET /demo_users/_search?q=poweruser //泛查詢(沒有指定查詢的欄位),即查詢文檔中所有欄位包含poweruser的值,只要有一個欄位符合,那麼該文檔將會被返回
        
        B.Phrase Query【即分組查詢】
        操作符有:AND / OR  / NOT 或者表示為: && / || / ! 
        +表示must -表示must_not 例如:field:(+a -b)意為field中必需包含a但不能包含b
        
        GET /demo_users/_search?q=remark:(POST test) 
        GET /demo_users/_search?q=remark:(POST OR test) 
        GET /demo_users/_search?q=remark:"POST test" 
        //分組查詢,即:查詢remark中包含POST 或 test的文檔記錄
        
        GET /demo_users/_search?q=remark:(test AND POST) //remark同時包含test與POST
        GET /demo_users/_search?q=remark:(test NOT POST) //remark包含test但不包含POST
        
        C.範圍查詢
        區間表示:[]閉區間,{}開區間
        如:year:[2019 TO 2020] 或 {2019 TO 2020} 或 {2019 TO 2020] 或 [* TO 2020]
        算數符號
        year:>2019 或 (>2012 && <=2020) 或 (+>=2012 +<=2020)
        
        GET /demo_users/_search?q=userId:>123 //查詢userId欄位大於123的文檔記錄
        
        D.通配符查詢
        ?表示匹配任意1個字元,*表示匹配0或多個字元 例如:role:power* , role:use?
        
        GET /demo_users/_search?q=role:power* //查詢role欄位前面是power,後面可以是0或多個其它任意字元。
        
        可使用正則表達式,如:username:張三\d+
        
        可使用近似查詢偏移量(slop)提高查詢匹配結果【使用~N,N表示偏移量】
        GET /demo_users/_search?q=remark:tett~1 //查詢remark中包含test的文檔,但實際寫成了tett,故使用~1偏移近似查詢,可以獲得test的查詢結果
        
        GET /demo_users/_search?q=remark:"i like shenzhen"~2 //查詢i like shenzhen但實際remark欄位中值為:i like hubei and shenzhen,比查詢值多了 hubei and,這裡使用~2指定可偏移相隔2個term(這裡即兩個單詞),最終也是可以查詢出結果
        
        
      2. DSL POST查詢(POST index/_search)

        POST demo_users/_search

        Request Body:

        {
            "query":{
                "bool":{
                    "must":[
                        {
                            "term":{
                                "enabled":"true"  #查詢enabled=true
                            }
                        },
                        {
                            "term":{
                                "role.keyword":"poweruser" #且role=poweruser
                            }
                        },
                        {
                            "query_string":{
                                "default_field":"username.keyword",
                                "query":"張三" #且 username 包含張三
                            }
                        }
                    ],
                    "must_not":[
        
                    ],
                    "should":[
        
                    ]
                }
            },
            "from":0,
            "size":1000,
            "sort":[
                {
                    "createdDate":"desc"  #根據createdDate倒序
                }
            ],
            "_source":{ #指明返回的欄位,includes需返回欄位,excludes不需要返回欄位
                "includes":[
                    "role",
                    "username",
                    "userId",
                    "remark"
                ],
                "excludes":[
        
                ]
            }
        }
        

具體用法可參見:

【Elasticsearch】query_string的各種用法

Elasticsearch中 match、match_phrase、query_string和term的區別

Elasticsearch Query DSL 整理總結

[布爾查詢Bool Query]

最後附上ES官方的API操作鏈接指引:

Indices APIs:負責索引Index的創建(create)、刪除(delete)、獲取(get)、索引存在(exist)等操作。

Document APIs:負責索引文檔的創建(index)、刪除(delete)、獲取(get)等操作。

Search APIs:負責索引文檔的search(查詢),Document APIS根據doc_id進行查詢,Search APIs]根據條件查詢。

Aggregations:負責針對索引的文檔各維度的聚合(Aggregation)。

cat APIs:負責查詢索引相關的各類信息查詢。

Cluster APIs:負責集群相關的各類信息查詢。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1. 什麼是,以及怎麼用畫中畫 Windows 10 Creators Update以後UWP提供了一個新的視圖模式CompactOverlay,中文翻譯成 緊湊的覆蓋層 ?反正大部分時間我們都會稱它為 畫中畫模式 。 上圖中右上角即為進入畫中畫模式的微軟“電影和電視”應用。 可以調用 "Appli ...
  • string aa = DateTime.Now.ToShortDateString();//"2019/9/23" string bb = DateTime.Now.ToShortTimeString();//"上午 10:21" string ff = DateTime.Now.ToLongDa ...
  • C#實現的從小到大的冒泡排序: public void BubbleSort(int[] array) { int length = array.Length; for (int i = 0; i < length - 1; i++) { for (int j = length - 1; j > i ...
  • C#實現(Delegate)的委托就不多說了,直接上代碼,看代碼中的註釋: namespace Delegate { delegate void DGSayiHi(string name);//聲明委托 delegate void DGDo(string name); class Program { ...
  • 在公司的電腦虛擬機上安裝了centos 6.5 ,然後我把他克隆下來用在家裡電腦的虛擬機上,打開後查看ip,發現只有迴環地址lo,沒有eth0, 於是重啟網路 輸入 service network restart 發現 報錯Bringing up interface eth0: Device eth ...
  • 參考 鏈接:https://gitbook.cn/books/5a33782c5778440a9d906017/index.html ...
  • 對長期奮戰在一線的後端開發人員來說,都知道redis有兩種持久化方式RDB和AOF,雖說大家都知道這兩種方式大概運作方式,但想必有實操瞭解得不會太多。 這裡是自己實操兩種持久化方式的一點點記錄。 先看以下摘錄自redis官網原文解釋(當然原文是English,這裡用google翻譯過了。) Redi ...
  • 參考鏈接:https://www.zhihu.com/question/333417513 https://www.oschina.net/p/hbase hadoop環境搭建:https://blog.csdn.net/hliq5399/article/details/78193113/ goog ...
一周排行
    -Advertisement-
    Play Games
  • C#TMS系統代碼-基礎頁面BaseCity學習 本人純新手,剛進公司跟領導報道,我說我是java全棧,他問我會不會C#,我說大學學過,他說這個TMS系統就給你來管了。外包已經把代碼給我了,這幾天先把增刪改查的代碼背一下,說不定後面就要趕鴨子上架了 Service頁面 //using => impo ...
  • 委托與事件 委托 委托的定義 委托是C#中的一種類型,用於存儲對方法的引用。它允許將方法作為參數傳遞給其他方法,實現回調、事件處理和動態調用等功能。通俗來講,就是委托包含方法的記憶體地址,方法匹配與委托相同的簽名,因此通過使用正確的參數類型來調用方法。 委托的特性 引用方法:委托允許存儲對方法的引用, ...
  • 前言 這幾天閑來沒事看看ABP vNext的文檔和源碼,關於關於依賴註入(屬性註入)這塊兒產生了興趣。 我們都知道。Volo.ABP 依賴註入容器使用了第三方組件Autofac實現的。有三種註入方式,構造函數註入和方法註入和屬性註入。 ABP的屬性註入原則參考如下: 這時候我就開始疑惑了,因為我知道 ...
  • C#TMS系統代碼-業務頁面ShippingNotice學習 學一個業務頁面,ok,領導開完會就被裁掉了,很突然啊,他收拾東西的時候我還以為他要旅游提前請假了,還在尋思為什麼回家連自己買的幾箱飲料都要叫跑腿帶走,怕被偷嗎?還好我在他開會之前拿了兩瓶芬達 感覺感覺前面的BaseCity差不太多,這邊的 ...
  • 概述:在C#中,通過`Expression`類、`AndAlso`和`OrElse`方法可組合兩個`Expression<Func<T, bool>>`,實現多條件動態查詢。通過創建表達式樹,可輕鬆構建複雜的查詢條件。 在C#中,可以使用AndAlso和OrElse方法組合兩個Expression< ...
  • 閑來無聊在我的Biwen.QuickApi中實現一下極簡的事件匯流排,其實代碼還是蠻簡單的,對於初學者可能有些幫助 就貼出來,有什麼不足的地方也歡迎板磚交流~ 首先定義一個事件約定的空介面 public interface IEvent{} 然後定義事件訂閱者介面 public interface I ...
  • 1. 案例 成某三甲醫預約系統, 該項目在2024年初進行上線測試,在正常運行了兩天後,業務系統報錯:The connection pool has been exhausted, either raise MaxPoolSize (currently 800) or Timeout (curren ...
  • 背景 我們有些工具在 Web 版中已經有了很好的實踐,而在 WPF 中重新開發也是一種費時費力的操作,那麼直接集成則是最省事省力的方法了。 思路解釋 為什麼要使用 WPF?莫問為什麼,老 C# 開發的堅持,另外因為 Windows 上已經裝了 Webview2/edge 整體打包比 electron ...
  • EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...