Update only specific field value in elasticsearch

后端 未结 6 1086
轻奢々
轻奢々 2020-12-28 12:22

Is it possible to update some specific fields value in elasticsearch with out overwriting other fields. ?

6条回答
  •  一整个雨季
    2020-12-28 12:52

    If you would like to update the existing field value only then you must try this solution:

    POST IndexName/_update_by_query
    {
      "script": {
        "source": """
    
       if (ctx._source?.Field != null) 
        {  
            ctx._source.remove('Field');
            ctx._source.put('Field', 'Value');
        }   
        """,
        "lang": "painless"
      },
      "query": {
        "terms": {
            "_id": [
              1 (Replace with Document ID)
            ]
          }
      }
    }
    

    If you would like to add new field with value then you must try this solution:

    POST IndexName/_update_by_query
    {
      "script": {
        "source": """
    
       if (ctx._source?.NewField == null) 
        {  
            ctx._source.hf.put('NewField', 'Value');
        }   
        """,
        "lang": "painless"
      },
      "query": {
        "terms": {
            "_id": [
              1 (Replace with Document ID)
            ]
          }
      }
    }
    

提交回复
热议问题