Elasticsearch(五)基礎的 CRUD

新增文檔

語法格式:

PUT /Index/Type/id
{
  "json數據"
}

Elasticsearch 會自動建立 Index 和 Type,不需要提前創建,而且 Elasticsearch 默認會對 Document 的每個 field 都建立倒排索引,讓其可以被搜索。

PUT /store/product/1
{
  "name": "gaolujie yagao",
  "desc": "gaoxiao meibai",
  "price": 30,
  "producer": "gaolujie producer",
  "tags": [
    "meibai",
    "fangzhu"
  ]
}

PUT /store/product/2
{
  "name": "jiajieshi yagao",
  "desc": "youxiao fangzhu",
  "price": 25,
  "producer": "jiajieshi producer",
  "tags": [
    "fangzhu"
  ]
}

PUT /store/product/3
{
  "name": "zhonghua yagao",
  "desc": "caoben zhiwu",
  "price": 40,
  "producer": "zhonghua producer",
  "tags": [
    "qingxin"
  ]
}

新增返回結果:

{
  "_index": "store",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}
查詢文檔

語法格式:

GET /Index/Type/id

執行 GET /store/product/1,返回結果:

{
  "_index": "store",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}
修改文檔
替換文檔
PUT /store/product/1
{
  "name": "jiaqiangban gaolujie yagao",
  "desc": "gaoxiao meibai",
  "price": 30,
  "producer": "gaolujie producer",
  "tags": [
    "meibai",
    "fangzhu"
  ]
}

返回結果:

{
  "_index": "store",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

查詢:GET /store/product/1,返回結果:

{
  "_index": "store",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "name": "jiaqiangban gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}

替換方式有一個不好,即必須帶上所有的 field,才能去進行信息的修改。

更新文檔(更新 != 替換)
POST /store/product/1/_update
{
  "doc": {
    "name": "jiaqiangban2 gaolujie yagao"
  }
}

返回結果:

{
  "_index": "store",
  "_type": "product",
  "_id": "1",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

查詢:GET /store/product/1,返回結果:

{
  "_index": "store",
  "_type": "product",
  "_id": "1",
  "_version": 3,
  "found": true,
  "_source": {
    "name": "jiaqiangban2 gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}
刪除文檔

語法格式:

DELETE /Index/Type/id

執行 DELETE /store/product/1,返回結果:

{
  "found": true,
  "_index": "store",
  "_type": "product",
  "_id": "1",
  "_version": 4,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容