python操作ES的增、删、改、查(单条数据、批量加载、根据id更新、根据id删除)

废话不多说直接上干货!

# ES相关包
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk     


class ElasticSearchClient(object):  # 启动ES
    @staticmethod
    def get_es_servers():
        es_servers = [{
            "host": "localhost",
            "port": "9200"
        }]
        es_client = Elasticsearch(hosts=es_servers)
        return es_client

class LoadElasticSearch(object):  # 在ES中加载、存储和处理数据
    def __init__(self):
        #self.index = 'my-index-cleaned' #"my-index-yzm-1"
        self.index = "my-index-yzm-1"
        
        #self.doc_type = "test-type" #"test-type-yzm-1"
        self.doc_type = "test-type-yzm-1"
        
        self.es_client = ElasticSearchClient.get_es_servers()
        self.set_mapping()

    def set_mapping(self):
        """
        设置mapping
       
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中进行Elasticsearch的操作,你可以使用Elasticsearch的官方库elasticsearch-py来实现。下面是一个简单的例子来演示如何进行操作: 引用: 首先,你需要导入Elasticsearch库并连接到Elasticsearch服务器: ```python from elasticsearch import Elasticsearch # 连接到Elasticsearch服务器 es = Elasticsearch([ES_IP], http_auth=('elastic', '123456'), port=ES_PORT) ``` 引用: 数据: - 询所有数据: ```python body = { "size": 111, # 最大显示数量,es默认展示10条 "query": { "match_all": {} } } res = es.search(index='my-index', body=body, request_timeout=30) ``` - 询具体某个字段: ```python body = { "size": 10000, # 最大显示数量 "query": { "match": { "text": { "query": search_key, "analyzer": "ik_smart", # 用来指定搜索的词语按那种拆词粒度拆词 "operator": "or", # 按拆分后的词询时,词与词之间是 and 还是 or 的关系 "minimum_should_match": "75%" # 该参数用来控制应该匹配的分词的最少数量,至少匹配几个词才召回询的结果 } } } } res = es.search(index='my-index', body=body, request_timeout=30) ``` 引用: 获取拆分后的词: 你可以使用Elasticsearch的analyze API来获取拆分后的词,例如: ```python body = { "text": "惠普 p2015dn", "analyzer": "ik_max_word" } res = es.indices.analyze(index='my-index', body=body) key_list = [dic['token'] for dic in res['tokens']] print(key_list) # ['惠普', 'p2015dn', 'p', '2015', 'dn'] ``` 以上是关于在Python中使用Elasticsearch进行的基本操作。你可以根据具体的需求使用不同的询方式,并根据返回的结果进行相应的处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python 使用 Elasticsearch ](https://blog.csdn.net/Waller_/article/details/109810964)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值