Elasticsearch05-批量增删改查

批量查询

# 查询不同文档
GET /_mget
{
  "docs": [
    {
      "_index": "product",
      "_id":2
    },
        {
      "_index": "fields_test",
      "_id":1
    }
    ]
}

# 查询同一个文档
GET /product/_mget
{
  "docs": [
    {
      "_id": 2
    },
    {
      "_id": 3
    }
  ]
}



# 查询同一个文档
GET /product/_mget
{
  "ids":[2,3]
}




GET /product/_mget
{
  "docs": [
    {
      "_id": 2,
      "_source": false #不查询字段
    },
    {
      "_id": 3,
      "_source": [   #只显示'name'、'price'字段
        "name",
        "price"
      ]
    },
    {
      "_id": 4,
      "_source": { #include包含哪些字段
        "include": [
          "name"
        ],
        "exclude":[   #exclude排除哪些字段
          "price"
          ]
      }
    }
  ]
}

创建

# 手动生成id,如果存在就updated
PUT /test_index/_doc/1/ 
{
  "field":"test"
}


# 手动生成id,如果存在就报错
PUT /test_index/_doc/3/_create
{
  "field":"test"
}

# 手动生成id,如果存在就报错
PUT /test_index/_create/1/
{
  "field":"test"
}


#自动生产id(guid),"_id" : "YsONtnsBwRRHNrIJssjh"
POST /test_index/_doc
{
  "field":"test"
}

增、删、改

POST /_bulk
{ "delete": { "_index": "product2",  "_id": "1" }}
{ "create": { "_index": "product2",  "_id": "2" }}
{ "name":    "_bulk create 2" }
{ "create": { "_index": "product2",  "_id": "12" }}
{ "name":    "_bulk create 12" }
{ "index":  { "_index": "product2",  "_id": "3" }}
{ "name":    "index product2 " }
{ "index":  { "_index": "product2",  "_id": "13" }}
{ "name":    "index product2" }
{ "update": { "_index": "product2",  "_id": "4","retry_on_conflict" : "3"} }
{ "doc" : {"test_field2" : "bulk test1"} }

说明:
    a.retry_on_conflict 报错重试3次,针对version的乐观锁
    b.使用_bulk,body不能换行,只能一行
    c._bulk?filter_path=items.*.error  只显示失败的
    d.index存在的数据全量替换 不存在的新创建
    e.这种方式操作优点:不会占用内存

ES并发冲突问题(悲观锁和乐观锁)

1.悲观锁:各种情况,都加锁,读写锁、行级锁、表级锁。使用简单,但是并发能力很低
2.乐观锁:并发能力高,操作麻烦,每次no-query操作都需要比对version

# 修改的时候带上版本号
POST /version_index/_doc/3?version=8&version_type=external
{
  "field":"test"
}
原文地址:https://www.cnblogs.com/bigdata-familyMeals/p/15231947.html