• Python Elasticsearch DSL 增删改


    连接 Es:

    import elasticsearch
    
    clinet = elasticsearch.Elasticsearch([{"host": "10.44.99.102", "port": 9200}])
    or
    clinet = Elasticsearch(["10.44.99.102:9200"])

    es.indices.create(index='my-index',ignore)

    插入数据:

    es.index(index="my-index",doc_type="test-type",id=01,body={"any":"data01","timestamp":datetime.now()})

    删除数据:

    es.delete(index='indexName', doc_type='typeName', id='idValue')

    条件删除:

    delete_by_query:删除满足条件的所有数据,查询条件必须符合DLS格式
    
    query = {'query': {'match': {'sex': 'famale'}}}# 删除性别为女性的所有文档
    
    query = {'query': {'range': {'age': {'lt': 11}}}}# 删除年龄小于11的所有文档
    
    es.delete_by_query(index='indexName', body=query, doc_type='typeName')

    条件更新:

    update_by_query:修改满足条件的所有数据,查询条件必须符合DLS格式
    
    query = {'query': {'match': {'sex': 'famale'}}}# 修改性别为女性的所有文档
    
    query = {'query': {'range': {'age': {'lt': 11}}}}# 修改年龄小于11的所有文档
    
    es.update_by_query(index='indexName', body=query, doc_type='typeName')

    批量写入删除更新:

    doc = [
    {"index": {}},
    {'name': 'jackaaa', 'age': 2000, 'sex': 'female', 'address': u'北京'},
    {"index": {}},
    {'name': 'jackbbb', 'age': 3000, 'sex': 'male', 'address': u'上海'},
    {"index": {}},
    {'name': 'jackccc', 'age': 4000, 'sex': 'female', 'address': u'广州'},
    {"index": {}},
    {'name': 'jackddd', 'age': 1000, 'sex': 'male', 'address': u'深圳'},
    ]
    doc = [
    {'index': {'_index': 'indexName', '_type': 'typeName', '_id': 'idValue'}}
    {'name': 'jack', 'sex': 'male', 'age': 10 }
    {'delete': {'_index': 'indexName', '_type': 'typeName', '_id': 'idValue'}}
    {"create": {'_index' : 'indexName', "_type" : 'typeName', '_id': 'idValue'}}
    {'name': 'lucy', 'sex': 'female', 'age': 20 }
    {'update': {'_index': 'indexName', '_type': 'typeName', '_id': 'idValue'}}
    {'doc': {'age': '100'}}
    ]
    es.bulk(index='indexName', doc_type='typeName', body=doc)

    批量更新也可以采用如下的方式进行json拼装,最后写入:

    for line in list:
                action = {
                    "_index": self.index_name,
                    "_type": self.index_type,
                    "_id": i, #_id 也可以默认生成,不赋值
                    "_source": {
                        "date": line['date'],
                        "source": line['source'].decode('utf8'),
                        "link": line['link'],
                        "keyword": line['keyword'].decode('utf8'),
                        "title": line['title'].decode('utf8')}
                }
                i += 1
                ACTIONS.append(action)
    success, _ = bulk(self.es, ACTIONS, index=self.index_name, raise_on_error=True)
  • 相关阅读:
    Leetcode: Plus One Linked List
    Leetcode: Sort Transformed Array
    Leetcode: Rearrange String k Distance Apart
    Leetcode: Line Reflection
    Leetcode: Logger Rate Limiter
    Leetcode: Design Snake Game
    Leetcode: Android Unlock Patterns
    Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
    Design Tic-Tac Toe
    Leetcode: Moving Average from Data Stream
  • 原文地址:https://www.cnblogs.com/shangwei/p/14779893.html
Copyright © 2020-2023  润新知