最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ElasticSearch學習之文檔API相關操作

 更新時間:2023年01月31日 14:30:39   作者:程序員皮卡秋  
這篇文章主要為大家介紹了ElasticSearch學習之文檔API相關操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

本節(jié)主要給大家講一下文檔API相關操作。在學習之前,建議大家先回顧前幾節(jié)內容,讓自己有一個整體的認知,不要把概念混淆了。我們前幾節(jié)都在講索引,它是和文檔掛鉤的,文檔我們可以理解為數(shù)據(jù),數(shù)據(jù)有增刪改查,本節(jié)就主要跟大家講一下文檔的增刪改查操作。

本文偏實戰(zhàn)一些,好了, 廢話不多說直接開整吧~

創(chuàng)建文檔

這里沿用之前的例子,使用class_1的索引,我們先看下它的索引結構:

GET /class_1

返回:

{
  "class_1" : {
    "aliases" : {
      "class" : { }
    },
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "num" : {
          "type" : "long"
        }
      }
    },
    "settings" : {
      "index" : {
        "refresh_interval" : "3s",
        "number_of_shards" : "3",
        "provided_name" : "class_1",
        "creation_date" : "1670812583980",
        "number_of_replicas" : "1",
        "uuid" : "CTD3dM-fQm-KFEVl4nAgRQ",
        "version" : {
          "created" : "7060299"
        }
      }
    }
  }
}

通過結構可以看到,它主要有兩個字段namenum,那么我們怎么往里邊添加數(shù)據(jù)呢?

創(chuàng)建文檔分為以下幾種情況:

  • 創(chuàng)建單個數(shù)據(jù)指定ID:使用_doc路由+PUT請求+id參數(shù)
  • 創(chuàng)建單個數(shù)據(jù)不指定ID:使用_doc路由+POST請求
  • 創(chuàng)建單個數(shù)據(jù)指定ID并進行ID唯一性控制:使用_doc路由+PUT請求+id參數(shù)+op_type=create參數(shù)
  • 創(chuàng)建批量數(shù)據(jù)指定ID:使用_bulk路由+PUT請求/POST請求+create關鍵字+_id屬性
  • 創(chuàng)建批量數(shù)據(jù)不指定ID:使用_bulk路由+PUT請求/POST請求+create關鍵字

下面,帶大家一個一個看

單個數(shù)據(jù)

指定ID

PUT /class_1/_doc/1
{
  "name":"a",
  "num": 5
}

創(chuàng)建成功返回:

{
  "_index" : "class_1",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 3
}

不指定ID

PUT /class_1/_doc/
{
  "name":"b",
  "num": 6
}
{
  "error" : "Incorrect HTTP method for uri [/class_1/_doc/?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}

創(chuàng)建失敗了,告訴我們這里要使用POST

POST /class_1/_doc/
{
  "name":"b",
  "num": 6
}
{
  "_index" : "class_1",
  "_type" : "_doc",
  "_id" : "h2Fg-4UBECmbBdQA6VLg",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 3
}

可以看到不指定id情況下,創(chuàng)建的文檔_id隨機生成了

ID唯一性控制

PUT /class_1/_doc/1?op_type=create
{
  "name":"c",
  "num": 7
}
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[1]: version conflict, document already exists (current version [1])",
        "index_uuid" : "CTD3dM-fQm-KFEVl4nAgRQ",
        "shard" : "2",
        "index" : "class_1"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[1]: version conflict, document already exists (current version [1])",
    "index_uuid" : "CTD3dM-fQm-KFEVl4nAgRQ",
    "shard" : "2",
    "index" : "class_1"
  },
  "status" : 409
}

可以看到,創(chuàng)建失敗了,返回document already exists

批量數(shù)據(jù)

指定ID

PUT class_1/_bulk
{ "create":{ "_id": 2 } }
{"name":"d","num": 8}
{ "create":{ "_id": 3 } }
{ "name":"e","num": 9}
{ "create":{ "_id": 4 } }
{"name":"f","num": 10}

tip: 這里要注意,不能有空行,json對象{}需要在同一行

{
  "took" : 25,
  "errors" : false,
  "items" : [
    {
      "create" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 4,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 4,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "4",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 4,
        "status" : 201
      }
    }
  ]
}

不指定ID

很簡單,去掉id屬性就好了~

PUT class_1/_bulk
{ "create":{  } }
{"name":"g","num": 8}
{ "create":{  } }
{ "name":"h","num": 9}
{ "create":{  } }
{"name":"i","num": 10}
{
  "took" : 30,
  "errors" : false,
  "items" : [
    {
      "create" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "iGFt-4UBECmbBdQAnVJe",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 3,
        "_primary_term" : 4,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "iWFt-4UBECmbBdQAnVJg",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 4,
        "_primary_term" : 4,
        "status" : 201
      }
    },
    {
      "create" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "imFt-4UBECmbBdQAnVJg",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 5,
        "_primary_term" : 4,
        "status" : 201
      }
    }
  ]
}

修改文檔

文檔修改分以下幾種情況:

  • 按照ID全量更新單個數(shù)據(jù):使用_doc路由+PUT請求+id參數(shù)
  • 按照ID全量更新單個數(shù)據(jù)并進行樂觀鎖控制:使用_doc路由+PUT請求+if_seq_no&if_primary_term參數(shù)+id參數(shù)
  • 按照ID部分更新單個數(shù)據(jù)(包含屬性添加):使用_update路由+POST請求+id參數(shù)
  • 按照ID全量更新批量數(shù)據(jù):使用_bulk路由+PUT請求/POST請求+index關鍵字+_id屬性
  • 按照ID部分更新批量數(shù)據(jù)(包含屬性添加):使用_bulk路由+PUT請求/POST請求+update關鍵字+_id屬性
  • 按照條件修改數(shù)據(jù):使用_update_by_query路由+POST請求+ctx._source[字段名稱]=字段值
  • 按照條件給數(shù)據(jù)新增屬性:使用_update_by_query路由+POST請求+ctx._source[字段名稱]=字段值
  • 按照條件給數(shù)據(jù)移除屬性:使用_update_by_query路由+POST請求+ctx._source.remove(字段名稱)

同樣的,帶大家一個個來看~

按照ID單個

全量更新

PUT /class_1/_doc/1
{
  "name":"k",
  "num": 5
}
{
  "_index" : "class_1",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 3
}

再次修改:

PUT /class_1/_doc/1
{
  "name":"k",
  "num": 6
}
{
  "_index" : "class_1",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 3
}

大家觀察一下這個_version字段,發(fā)現(xiàn)它的版本號是遞增的,也就是說會隨著我們的修改而變化

基于樂觀鎖全量更新

跟上條件if_seq_no,if_primary_term

PUT /class_1/_doc/1?if_seq_no=3&if_primary_term=3
{
  "name":"l",
  "num": 6
}
{
  "_index" : "class_1",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 3
}

再操作一下:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[1]: version conflict, required seqNo [3], primary term [3]. current document has seqNo [4] and primary term [3]",
        "index_uuid" : "CTD3dM-fQm-KFEVl4nAgRQ",
        "shard" : "2",
        "index" : "class_1"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[1]: version conflict, required seqNo [3], primary term [3]. current document has seqNo [4] and primary term [3]",
    "index_uuid" : "CTD3dM-fQm-KFEVl4nAgRQ",
    "shard" : "2",
    "index" : "class_1"
  },
  "status" : 409
}

發(fā)現(xiàn)操作失敗了,因為條件不符合 required seqNo [3], primary term [3],上一步操作完之后seqNo和primary term [4]

部分更新

PUT /class_1/_update/1
{
  "doc":{
      "name":"m",
      "num": 1
  }
}

按照ID批量

全量更新

PUT class_1/_bulk
{ "create":{ "_id": 2 } }
{"name":"d","num": 8}
{ "create":{ "_id": 3 } }
{ "name":"e","num": 9}
{ "create":{ "_id": 4 } }
{"name":"f","num": 10}

這個應該好理解

部分更新

需要修改為update并添加屬性

PUT class_1/_bulk
{ "update":{ "_id": 2 } }
{ "doc":{"name":"d","num": 8}}
{ "update":{ "_id": 3 } }
{ "doc":{ "name":"e","num": 9}}
{ "update":{ "_id": 4 } }
{ "doc":{"name":"f","num": 10}}

返回:

{
  "took" : 32,
  "errors" : false,
  "items" : [
    {
      "update" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 6,
        "_primary_term" : 4,
        "status" : 200
      }
    },
    {
      "update" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 7,
        "_primary_term" : 4,
        "status" : 200
      }
    },
    {
      "update" : {
        "_index" : "class_1",
        "_type" : "_doc",
        "_id" : "4",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 8,
        "_primary_term" : 4,
        "status" : 200
      }
    }
  ]
}

按照條件修改

修改字段

查找name=d的數(shù)據(jù)修改為num=10,name=e

POST class_1/_update_by_query
{
"query": {
  "match": {
    "name": "d"
  }
},
"script": {
  "source": "ctx._source['num']='10';ctx._source['name']='e'",
  "lang": "painless"
}
}

返回:

{
  "took" : 108,
  "timed_out" : false,
  "total" : 1,
  "updated" : 1,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

增加字段

POST class_1/_update_by_query
{
"query": {
  "match": {
    "name": "e"
  }
},
"script": {
  "source": "ctx._source['desc']=['hhhh']",
  "lang": "painless"
}
}
{
  "took" : 344,
  "timed_out" : false,
  "total" : 2,
  "updated" : 2,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

接著我們查下class_1的索引結構:

{
  "class_1" : {
    "aliases" : {
      "class" : { }
    },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "desc" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "num" : {
          "type" : "long"
        }
      }
    },
    "settings" : {
      "index" : {
        "refresh_interval" : "3s",
        "number_of_shards" : "3",
        "provided_name" : "class_1",
        "creation_date" : "1670812583980",
        "number_of_replicas" : "1",
        "uuid" : "CTD3dM-fQm-KFEVl4nAgRQ",
        "version" : {
          "created" : "7060299"
        }
      }
    }
  }
}

可以看到多了一個字段:

{
"desc" : {
    "type" : "text",
    "fields" : {
    "keyword" : {
        "type" : "keyword",
        "ignore_above" : 256
    }
    }
}
}

移除字段

POST class_1/_update_by_query
{
"query": {
  "match": {
    "name": "e"
  }
},
"script": {
  "source": "ctx._source.remove('desc')",
  "lang": "painless"
}
}

大家可以試著運行一下,然后再查下索引

刪除文檔

按照ID & 單個刪除

DELETE /class_1/_doc/2
{
  "_index" : "class_1",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 5,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 12,
  "_primary_term" : 4
}

按照ID & 批量刪除

PUT class_1/_bulk
{ "delete":{"_id":"2" } }
{ "delete":{"_id":"3" } }

按照條件刪除

POST class_1/_delete_by_query
{
"query":{
  "match_all":{
    "name": "e"
  }
}
}

結束語

本節(jié)主要講了ES中的文檔API操作,還遺留一個查詢操作, 該部分內容較多,放到后邊給大家講,更多關于ElasticSearch文檔API操作的資料請關注腳本之家其它相關文章!

相關文章

  • Java行為型模式中命令模式分析

    Java行為型模式中命令模式分析

    在軟件設計中,我們經(jīng)常需要向某些對象發(fā)送請求,但是并不知道請求的接收者是誰,也不知道被請求的操作是哪個,我們只需在程序運行時指定具體的請求接收者即可,此時可以使用命令模式來進行設計
    2023-02-02
  • Spring配置使用之Bean生命周期詳解

    Spring配置使用之Bean生命周期詳解

    這篇文章主要介紹了Spring配置使用之Bean生命周期詳解,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • 利用Java讀取二進制文件實例詳解

    利用Java讀取二進制文件實例詳解

    這篇文章主要給大家介紹了利用Java讀取二進制文件的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用java具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。
    2017-08-08
  • 完美解決gson將Integer默認轉換成Double的問題

    完美解決gson將Integer默認轉換成Double的問題

    下面小編就為大家?guī)硪黄昝澜鉀Qgson將Integer默認轉換成Double的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • 詳解lombok @Getter @Setter 使用注意事項

    詳解lombok @Getter @Setter 使用注意事項

    這篇文章主要介紹了詳解lombok @Getter @Setter 使用注意事項,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Java基礎之java泛型通配符詳解

    Java基礎之java泛型通配符詳解

    Java 泛型(generics)是 JDK 5 中引入的一個新特性, 泛型提供了編譯時類型安全檢測機制,該機制允許開發(fā)者在編譯時檢測到非法的類型,今天通過本文給大家介紹java泛型通配符的相關知識,感興趣的朋友一起看看吧
    2021-07-07
  • @NotEmpty、@NotBlank、@NotNull的區(qū)別

    @NotEmpty、@NotBlank、@NotNull的區(qū)別

    這篇文章主要介紹了@NotEmpty、@NotBlank、@NotNull的區(qū)別,需要的朋友可以參考下
    2016-09-09
  • Java中Session的詳解

    Java中Session的詳解

    這篇文章主要介紹了了解java中的session的相關問題,什么是session,session怎么用等,具有一定參考價值,需要的朋友可以了解下。
    2021-10-10
  • 詳解spring cloud hystrix請求緩存(request cache)

    詳解spring cloud hystrix請求緩存(request cache)

    這篇文章主要介紹了詳解spring cloud hystrix請求緩存(request cache),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 聊聊Java并發(fā)中的Synchronized

    聊聊Java并發(fā)中的Synchronized

    這篇文章主要介紹了聊聊Java并發(fā)中的Synchronized,介紹了同步的基礎,原理,鎖的相關內容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11

最新評論

军事| 年辖:市辖区| 尚志市| 顺平县| 山东| 河北区| 乌兰县| 奉节县| 旬邑县| 绩溪县| 江口县| 青铜峡市| 千阳县| 泽普县| 巴彦县| 五寨县| 隆尧县| 同江市| 原阳县| 彭山县| 栖霞市| 名山县| 峨眉山市| 太白县| 嘉祥县| 萝北县| 巴马| 新余市| 绥芬河市| 兰州市| 崇阳县| 大名县| 甘德县| 吉水县| 亚东县| 沙洋县| 乌海市| 衢州市| 工布江达县| 新丰县| 平罗县|