zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    MySQL动态添删改列字段
    关于javascript在子页面中函数无法调试问题的解决
    <T> T[] toArray(T[] a);
    MERGE INTO
    eclipse不能新建server
    关于tomcat7下websocket不能使用
    myeclipse启动tomcat报错cannot find a free socket for debugger
    checkbox提交多组数据到action
    Struts2 Action中的方法命名不要以get开头
    浅谈C#中的接口和抽象类
  • 原文地址:https://www.cnblogs.com/shangwei/p/14779893.html
Copyright © 2011-2022 走看看