python操作Elasticsearch的基本操作

一、python连接es

package: pip install elasticsearch

es = Elasticsearch(
            ['address'],
            http_auth=('user_name', 'password'),
            port=9200,
            use_ssl=False
        )

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

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

三、查询

#普通查询
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)

五、更新

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

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

猜你喜欢

转载自www.cnblogs.com/itBlogToYpl/p/12331525.html