ElasticSearch update data

1. Create a test index


PUT test
{
  "mappings": {
    "test1": {
      "properties": {
        "num1": {
          "type": "integer"
        },
        "num2": {
          "type": "integer"
        },
        "num3": {
          "type": "nested",
          "properties": {
            "num31": {
              "type": "integer"
            },
            "num32": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}

2. Add test data


PUT test/test1/1
{
  "num1": 11,
  "num2": 12,
  "num3": [
    {
      "num31": 131,
      "num32": 132
    },
    {
      "num31": 133,
      "num32": 134
    }
  ]
}

PUT test/test1/2
{
  "num1": 21,
  "num2": 12,
  "num3": [
    {
      "num31": 231,
      "num32": 232
    },
    {
      "num31": 233,
      "num32": 234
    }
  ]
}

3. View data


GET test/test1/_search

4. Update data according to id


POST test/test1/1/_update
{
  "doc": {
    "num1": 10
  }
}

5. Use update_by_query to update data


POST test/test1/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.num1=111;ctx._source.num2=222"
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "num2": {
              "value": "12"
            }
          } 
        } 
      ]
    }
  }
}

6. Use update_by_query to append a single piece of data for the nested type


POST test/test1/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.num3.add(params.new_data)",
    "params": {
      "new_data": {
        "num31":1111,
        "num32":2222
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "num2": {
              "value": "22"
            }
          } 
        } 
      ]
    }
  }
}

7. Use update_by_query to append multiple pieces of data for nested type


POST test/test1/_update_by_query
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.num3.addAll(params.new_datas)",
    "params": {
      "new_datas": [
        {
          "num31":1111,
          "num32":2222
        },
        {
          "num31":3333,
          "num32":4444
        },
        {
          "num31":5555,
          "num32":6666
        }
      ]
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "num2": {
              "value": "12"
            }
          } 
        } 
      ]
    }
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325196158&siteId=291194637