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
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容