python操作Elasticsearch数据库

Elasticsearch(ES),ES是一个开源的高扩展的分布式全站搜索引擎,是整个Elastic Stack技术栈的核心。它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。

安装好ES之后,9200端口为浏览器访问的http协议restfu端口

Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档。

python操作Elasticsearch数据库

cpp 复制代码
#引入es库:
 
from elasticsearch import Elasticsearch
 
#链接到对应的es
es = Elasticsearch(host = "192.168.1.1",
http_auth=(username, password))
 
 
#  精准查询
def es_search_body(value, key):
    """
    将传参封装为es查询的body,可根据实际需求,判断是否新增此函数
    :param value:
    :param key:
    :return:
    """
    body = {
        "query": {
            "match": {
                key: value
            }
        }
    }
    return body
 
#使用es进行搜索
es.search(index=your_index, body=es_search_body(value, key))
 
#使用es进行删除
es.delete_by_query(index=your_index, body=es_search_body(value, key))
 
相关推荐
老赵全栈实战21 分钟前
Pydantic配置管理最佳实践(一)
python
Elasticsearch6 小时前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
jiayou646 小时前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
阿尔的代码屋6 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
李广坤1 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama