ElasticSearch 封装工具

ElasticSearch 工具

# -*- coding:UTF-8 -*-

# author:user
# contact: test@test.com
# datetime:2021/8/16 14:30
# software: PyCharm

"""
文件说明:
    ES工具
"""


import pandas as pd
from elasticsearch import Elasticsearch
from elasticsearch import helpers

class EsUtil(object):
    def __init__(self, ip='127.0.0.1'):
        self.ip = ip
        # 无状态密码登录
        self.es = Elasticsearch([ip], port=9200)
        # 用户名密码状态
        # self.es = Elasticsearch([ip], http_auth=("elastic", "password"), port=9200)

    def create_index(self, index_name, mapping):
        """
        mapping示例:
        "mappings":{
           "properties": {
              "question": {
                  "type": "text",
                  "analyzer": "ik_smart",
                  "search_analyzer": "ik_smart",
                  "index": "true"
                  },
              "answer": {
                  "type": "text",
                  "index": "false"
                  },
              "id": {
              "type": "integer"
                }
              }
            }
        """
        if self.es.indices.exists(index_name):
            raise ValueError(index_name+" 已经存在")
        self.es.indices.create(index=index_name, body=mapping)

    def exit_index(self, index_name):
        return self.es.indices.exists(index_name)

    def delete_index(self, index_name):
        self.es.indices.delete(index=index_name)

    def insert_data(self, index_name, idx, body):
        """
        body = {'question': '法外狂徒-张三','answer':'ssss', 'id': 1}
        """
        return self.es.index(index=index_name, id=idx, body=body)

    def insert_batch_data(self, action):
        """
        action = [{"_index": "repu","_id":"3", "_type":"_doc","_source":{"question": "安大略大","answer":"暗淡看","id":3}},
                  {"_index": "repu", "_id":"4", "_type":"_doc","_source":{"question": "安大略大","answer":"暗淡看","id":4}},
                  {"_index": "repu", "_id":"5", "_type":"_doc","_source":{"question": "安大略大","answer":"暗淡看","id":5}}]
        """
        return helpers.bulk(self.es, action)

    def delete_by_query(self, index_name, body):
        """
        body = {'query': {'match_all': {}}}
        body = {'query': {'match': {'id': 1}}}
        """
        self.es.delete_by_query(index=index_name,body=body)

    def get_data_by_id(self, index_name, idx):
        return self.es.get(index=index_name, id=idx)

    def get_data_by_body(self, index_name, body):
        """
        1. 精确查找 terms
        body = {'query': {'terms': {'question':['张三', '李四']}}}
        2. 查询id和question包含:法外狂徒-张三
        body = {'query': {'multi_match': {'query': '法外狂徒-张三','fields': ['question','id']}}}
        3. match: 匹配question包含 ‘法外狂徒-张三’的所有数据
        body = {'query':{'match': {'question':'法外狂徒-张三'}}}
        4. 精确查找 term  查询question='安大略'的所有数据
        body = {'query': {'term': {'question': '安大略'}}}
        """
        return self.es.search(index=index_name, body=body)

    def update_by_id(self, index_name, idx, body):
        """
        body = {"doc":{"question": "略大","answer":"暗淡看","id":3}}
        """
        return self.es.update(index=index_name,id=idx,body=body)

    def es_info(self):
        return self.es.info()



if __name__ == '__main__':
    obj = EsUtil()
    print(obj.es_info())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发呆的比目鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值