zoukankan      html  css  js  c++  java
  • python操作Elasticsearch增删改查/删除索引

    一、python连接es

    package: pip install elasticsearch

    es = Elasticsearch(
                ['address'],
                http_auth=('user_name', 'password'),
                port=9200,
                use_ssl=False
            )
    无密码,自己搭建的
    es = Elasticsearch(
    ["host:9200/"], # 连接集群,以列表的形式存放各节点的IP地址

    )
     

    二、基本操作之创建索引,集合

    mappings = {
                "mappings": {
                    "example_name_test": {                            
                        "properties": {
                            "email_id": {
                                "type": "text",
                                "index": "true"
                            },
                            "company_name": {
                                "type": "keyword",  # keyword不会进行分词,text会分词
                                "index": "true"  # 不建索引
                            },
                            "company_id": {
                                "type": "keyword",  # keyword不会进行分词,text会分词
                                "index": "true"  # 不建索引
                            },
                            "result": {
                                "type": "object",
                                "properties": {
                                    "client": {"type": "text", "index": "true"},
                                }
                            },
                            "create_time": {
                                "type": "keyword",
                                "index": 'true'
                            }
      
                        }
                    }
                }
            }
    
            res = self.es.indices.create(index='example_name_test', body=mappings) 

         print(res)
    #  example_name_test 为名字
    内容长度如果较多使用text,较少使用keyword,一般使用keyword的比较多,因为text查询极其占用内存
    如果添加时间字段建议使用时间戳,类型用keyword

    二 插入数据

    res = es.index(index=name, doc_type=name, body=body)
    print(res)

    三、查询

    #普通查询
    dsl = {'query': {'match': {'_id': 'AjDxVXABXWez-Pv8B-Ib'}},
                }
    
    # 查询全部
    dsl = {'query': {'match_all': {}}}
    
    # 多条件查询
    dsl = {
                 "query": {
                        "bool": {
                            "must": [
                                {
                                    "term": {
                                        "字段1": '1000001214'
                                    }
                                },
    
                                {
                                    "term": {
                                        "字段2": content
                                    }
                                },
    
                            ]
                        }
                    },
            }
    
    # 查询翻页
    dsl = {
                  "query": {    
                        "match": {"code" : code}
                      },
                    "from":0,   # 从第几个开始
                    "size":100  # 返回条数
                    }
    
    # 去重查询
    dsl = {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "email_id": email.m_mem_id
                                }
                            },
                            {
                                "term": {
                                    "country": email.country
                                }
                            }
                        ]
                    }
                }, "collapse": {
                    "field": "去重字段"
                }
          }
    # 字段排序
    dsl = {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "email_id": email.m_mem_id
                                }
                            },
                            {
                                "term": {
                                    "country": email.country
                                }
                            }
                        ]
                    }
                },
                'sort':[
                    {'排序字段':{
                        'order':'desc'
                    }}
                ]
            }
    
    # 执行语句
    res = es.search(index='创建索引时起的名字', body=dsl)
    # 第二种方式  size建议在10000以内,大于10000报错
    res = es.search(index='创建索引时起的名字', body=dsl, scroll='5m',size=5000)

    四、删除

    # 删除无非就是根据查询条件删除
    dsl=查询条件参数
    es.delete_by_query(index='名字', body=dsl)
    # 删除索引
    # res = es.indices.delete('hot_keyword')  # 删除索引

    五、更新

    updateBody = {
                'doc': {
                    "字段名": 更新的内容
                }
            }
            res = self.es.update(index='索引名字', doc_type='索引名字', id='集合里面的_id', body=updateBody)

    更为详细的参考教程后续补充

  • 相关阅读:
    T4设计时模板调试
    MVC开发T4代码生成之一文本模板基础
    经典选项卡
    IE 调试工具 Utilu IE Collection:IE5.5、6.0、7.0, 8.0…各版本浏览器兼容性测试工具
    滚动读行!
    自定义标签的用法、支持IE6
    jQuery 参数传递例子
    IMG在IE6下不能HOVER的解决办法!
    点击渐变弹出层
    操作滚动条滚动到指定位置
  • 原文地址:https://www.cnblogs.com/itBlogToYpl/p/12331525.html
Copyright © 2011-2022 走看看