Menu

1.6 Elasticsearch DSL 批量操作

2017年6月12日 - Elasticsearch

课程环境


数据准备

PUT /product_index/product/1
{
  "product_name": "PHILIPS toothbrush HX6730/02",
  "product_desc": "【3?9 元,前 1000 名赠刷头,6 月 1 日 0 点火爆开抢,618 开门红巅峰 48 小时,抢先加入购物车】飞利浦畅销款,万千好评!深入净齿,智能美白!",
  "price": 399.00,
  "tags": [
    "toothbrush",
    "PHILIPS"
  ]
}

PUT /product_index/product/2
{
  "product_name": "Braun toothbrush 2000 3D",
  "product_desc": "6 月 1 日 16 点秒杀,仅 329 元!限量 1000 支,抢完即止!带压力感应提醒,保护牙龈,高效清洁",
  "price": 499.00,
  "tags": [
    "toothbrush",
    "Braun"
  ]
}

PUT /product_index/product2/1
{
  "product_name": "Braun toothbrush 2000 3D type2",
  "product_desc": "6 月 1 日 16 点秒杀,仅 329 元!限量 1000 支,抢完即止!带压力感应提醒,保护牙龈,高效清洁",
  "price": 499.00,
  "tags": [
    "toothbrush",
    "Braun"
  ]
}

PUT /product_index2/product2/2
{
  "product_name": "iphone7 shell",
  "product_desc": "一说到星空,就有太多美好的记忆,美丽的浩瀚宇宙,有太多说不清的神秘之处,星空太美丽,太绚烂!",
  "price": 36.00,
  "tags": [
    "iphone7",
    "phone",
    "shell"
  ]
}

批量操作的重要性

mget 批量查询

GET /_mget
{
  "docs": [
    {
      "_index": "product_index",
      "_type": "product",
      "_id": 1
    },
    {
      "_index": "product_index2",
      "_type": "product2",
      "_id": 1
    }
  ]
}
GET /product_index/_mget
{
  "docs": [
    {
      "_type": "product",
      "_id": 1
    },
    {
      "_type": "product2",
      "_id": 1
    }
  ]
}
GET /product_index/product/_mget
{
  "ids": [1, 2]
}

bulk 批量增删改

POST /_bulk
{"delete": {"_index": "product_index","_type": "product","_id": "1"}}
POST /_bulk
{ "create": { "_index": "product_index", "_type": "product", "_id": "333" } }
{ "product_name": "iphone7 shell2", "product_desc": "一说到星空,就有太多美好的记忆,美丽的浩瀚宇宙,有太多说不清的神秘之处,星空太美丽,太绚烂!", "price": 36.00, "tags": [ "iphone7", "phone", "shell" ] }
POST /_bulk
{"update":{"_index": "product_index","_type": "product","_id": "1"}}
{"doc":{"product_name": "iphone7 shell2222"}}
标签: