Skip to main content
 首页 » 编程设计

Elaticsearch 更新文档示例

2022年07月19日137think

Elasticsearch提供了更新功能,包括单条更新和批量更新。

1. 更新单个文档

POST hockey/_update/1 
{ 
  "script": { 
    "lang": "painless", 
    "source": "ctx._source.last = params.last", 
    "params": { 
      "last": "hockey" 
    } 
  } 
} 

2. 更新多个字段

POST /seats/_update/3 
{ 
  "script": { 
    "source": "ctx._source.sold = true; ctx._source.cost = params.sold_cost", 
    "lang": "painless", 
    "params": { 
      "sold_cost": 26 
    } 
  } 
} 

更可读的新脚本:

POST hockey/_update/1 
{ 
  "script": { 
    "lang": "painless", 
    "source": """ 
      ctx._source.last = params.last; 
      ctx._source.nick = params.nick 
    """, 
    "params": { 
      "last": "gaudreau", 
      "nick": "hockey" 
    } 
  } 
} 

3. 批量更新文档

POST addresses/_update_by_query 
{ 
    "script": 
    { 
        "lang": "painless", 
        "source":""" 
          ctx._source.data.url = ctx._source.data.url.replace('http', 'https'); 
          ctx._source.data.type = ctx._source.tcode.substring(0, 3) 
        """ 
    }, 
    "query": 
    { 
        "query_string": 
        { 
            "query": "http://*", 
            "analyze_wildcard": true 
        } 
    } 
} 

4. 总结

本文通过几个实例展示如何更新Elasticsearch文档信息。


本文参考链接:https://blog.csdn.net/neweastsun/article/details/111826343
阅读延展