Reindex真题2

题目

index1中如果包含reindexBatch字段,Reindex到index2中则加一,否则设置为1

直接脚本方式

### 添加脚本
POST _scripts/control_reindex_batch
{
  "script": {
    "lang": "painless",
  "source": """
      if (ctx._source['reindexBatch'] != null) {
        ctx._source['reindexBatch'] += params.increment;
      } else {
        ctx._source['reindexBatch'] = 1;
      }
    """
  }
}

### reindex
POST _reindex
{
  "source": {
    "index": "test_1"
  },
  "dest": {
    "index": "test_3"
  },
  "script": {
        "id": "control_reindex_batch",
        "params": {
          "increment": 1
        }
      }
}

使用ingest,间接脚本方式

PUT _ingest/pipeline/ig3
{
  "description": "",
  "processors": [
    {
      "script": {
        "source": """
            if (ctx.reindexBatch != null) {
              ctx.reindexBatch += params.increment;
            } else {
              ctx.reindexBatch = 1;
            }
      """,
      "params": {
        "increment":1
      }
      }
    }
  ]
}

POST _reindex
{
  "source": {
    "index": "test_1"
  },
  "dest": {
    "index": "test_4",
    "pipeline": "ig3"
  }
}

样例数据

PUT test_1/_bulk
{"index":{"_index":"test_1","_id":0}}
{"foo":[" adsa 123 "," 21232 321"],"firstname":"Li","lastname":"xiaolong"}
{"index":{"_index":"test_1","_id":1}}
{"foo":[" adsa 123 "," 21232 321"],"firstname":"Li","lastname":"xiaolong","reindexBatch":1}

使用script和ingest的区别

  • 1,使用ingest的中脚本,使用字段时是不需要带_source
  • 2, 直接使用script加不加_source需要参考painless文档
  • 3,ingest灵活性差点,但是可以通过_simulate验证脚本的正确性
  • 4,直接使用script,灵活性强,但是不太好验证

你可能感兴趣的:(Reindex真题2)