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

ElasticSearch學(xué)習(xí)之文檔API相關(guān)操作

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

前言

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

本文偏實(shí)戰(zhàn)一些,好了, 廢話不多說(shuō)直接開(kāi)整吧~

創(chuàng)建文檔

這里沿用之前的例子,使用class_1的索引,我們先看下它的索引結(jié)構(gòu):

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"
        }
      }
    }
  }
}

通過(guò)結(jié)構(gòu)可以看到,它主要有兩個(gè)字段namenum,那么我們?cè)趺赐镞吿砑訑?shù)據(jù)呢?

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

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

下面,帶大家一個(gè)一個(gè)看

單個(gè)數(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隨機(jī)生成了

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對(duì)象{}需要在同一行

{
  "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

很簡(jiǎn)單,去掉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全量更新單個(gè)數(shù)據(jù):使用_doc路由+PUT請(qǐng)求+id參數(shù)
  • 按照ID全量更新單個(gè)數(shù)據(jù)并進(jìn)行樂(lè)觀鎖控制:使用_doc路由+PUT請(qǐng)求+if_seq_no&if_primary_term參數(shù)+id參數(shù)
  • 按照ID部分更新單個(gè)數(shù)據(jù)(包含屬性添加):使用_update路由+POST請(qǐng)求+id參數(shù)
  • 按照ID全量更新批量數(shù)據(jù):使用_bulk路由+PUT請(qǐng)求/POST請(qǐng)求+index關(guān)鍵字+_id屬性
  • 按照ID部分更新批量數(shù)據(jù)(包含屬性添加):使用_bulk路由+PUT請(qǐng)求/POST請(qǐng)求+update關(guān)鍵字+_id屬性
  • 按照條件修改數(shù)據(jù):使用_update_by_query路由+POST請(qǐng)求+ctx._source[字段名稱]=字段值
  • 按照條件給數(shù)據(jù)新增屬性:使用_update_by_query路由+POST請(qǐng)求+ctx._source[字段名稱]=字段值
  • 按照條件給數(shù)據(jù)移除屬性:使用_update_by_query路由+POST請(qǐng)求+ctx._source.remove(字段名稱)

同樣的,帶大家一個(gè)個(gè)來(lái)看~

按照ID單個(gè)

全量更新

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
}

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

基于樂(lè)觀鎖全量更新

跟上條件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)操作失敗了,因?yàn)闂l件不符合 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}

這個(gè)應(yīng)該好理解

部分更新

需要修改為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的索引結(jié)構(gòu):

{
  "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"
        }
      }
    }
  }
}

可以看到多了一個(gè)字段:

{
"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"
}
}

大家可以試著運(yùn)行一下,然后再查下索引

刪除文檔

按照ID & 單個(gè)刪除

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é)束語(yǔ)

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

相關(guān)文章

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

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

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

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

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

    利用Java讀取二進(jìn)制文件實(shí)例詳解

    這篇文章主要給大家介紹了利用Java讀取二進(jìn)制文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 完美解決gson將Integer默認(rèn)轉(zhuǎn)換成Double的問(wèn)題

    完美解決gson將Integer默認(rèn)轉(zhuǎn)換成Double的問(wèn)題

    下面小編就為大家?guī)?lái)一篇完美解決gson將Integer默認(rèn)轉(zhuǎn)換成Double的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • 詳解lombok @Getter @Setter 使用注意事項(xiàng)

    詳解lombok @Getter @Setter 使用注意事項(xiàng)

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

    Java基礎(chǔ)之java泛型通配符詳解

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

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

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

    Java中Session的詳解

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

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

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

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

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

最新評(píng)論

怀远县| 乃东县| 简阳市| 龙游县| 吉安县| 高台县| 神木县| 德清县| 大渡口区| 安平县| 铜山县| 庆阳市| 贵定县| 稷山县| 平乡县| 武邑县| 金昌市| 彩票| 淮南市| 兴城市| 朝阳县| 商南县| 临桂县| 德庆县| 县级市| 灵山县| 韶关市| 阳原县| 民乐县| 桓仁| 青铜峡市| 博湖县| 南雄市| 玛纳斯县| 襄樊市| 达拉特旗| 新疆| 西峡县| 丽水市| 花莲市| 长宁区|