python實現elasticsearch的update操作,不改變原數據,增加字段,或者查詢更新


直接上代碼:

from elasticsearch import Elasticsearch

ES_URL = 'http://172.30.3.57:9200/'

es_client = Elasticsearch(ES_URL)

def get_data_update_data():
    query = {"size": 1000000}
    res = es_client.search(index='nnnlog-2021-04-28', body=query)['hits']['hits']
    # res = es_client.search(index='nnn', body=query)['hits']['hits']
    print(len(res))
    i = 0
    for log in res:
        operator_id = log['_source']['operator_id']
        # operator_id = log['_source']['name']
        operator_name = 'name_' + operator_id.split('-')[1]
        log['_source'].update({'operator_name': operator_name})
        es_client.update( # 此處可能發生es超時,建議異常處理
            index=log['_index'],
            doc_type='_doc',
            id=log['_id'],
            body={'doc':log['_source']} # 用map包裹數據
        )
        i += 1
        print('update: ', i)

if __name__ == "__main__":
    get_data_update_data()

如果是通過查詢來更新的操作,update_by_query,參照一下代碼實現:

# kibana
POST businesslog-2021-05-31/_update_by_query
{
  "query": {
    "term": {
      "operator_id.keyword": "operator-0" 
    }
  },
  "script": {
    "lang": "painless",
    "source": "ctx._source.operator_name=params.operator_name",
    "params": {
      "operator_name": "name_0"
    }
  }
}

# python
query = {
  "query": {
    "term": {
      "operator_id.keyword": "operator-0" 
    }
  },
  "script": {
    "lang": "painless",
    "source": "ctx._source.operator_name=params.operator_name",
    "params": {
      "operator_name": "name_0"
    }
  }
}
res = es_client.update_by_query(index='xxx', body=query)

此類更新可以通過查詢,批量更新,只要符合查詢條件的都可以update,速度很快。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM