python ES连接服务器的方法

连接Elasticsearch(ES)服务器是进行数据搜索和分析的常用操作。Elasticsearch是一个基于Lucene的搜索引擎,提供了RESTful API来进行索引、搜索和管理数据。

以下是一个详细的Python代码示例,展示如何连接到Elasticsearch服务器并执行一些基本操作。这个示例使用了官方的elasticsearch-py客户端库。

1. 安装Elasticsearch客户端库

首先,你需要安装elasticsearch库。如果你还没有安装,可以使用pip进行安装:

bash 复制代码
bash复制代码
​
pip install elasticsearch

2. 连接到Elasticsearch服务器

以下是一个完整的Python脚本,展示了如何连接到Elasticsearch服务器,创建索引,添加文档,并进行搜索。

python 复制代码
from elasticsearch import Elasticsearch, helpers  
  
# 配置Elasticsearch连接  
es = Elasticsearch(  
    ['http://localhost:9200'],  # Elasticsearch服务器地址和端口  
    http_auth=('username', 'password'),  # 如果需要认证,填写用户名和密码  
    use_ssl=False,  # 如果使用HTTPS,设置为True  
    verify_certs=False  # 如果使用HTTPS且自签名证书,设置为False  
)  
  
# 检查连接是否成功  
if es.ping():  
    print("Successfully connected to Elasticsearch!")  
else:  
    print("Could not connect to Elasticsearch")  
    exit()  
  
# 创建索引  
index_name = 'my_index'  
if not es.indices.exists(index=index_name):  
    # 定义索引的映射(Schema)  
    mappings = {  
        'properties': {  
            'title': {'type': 'text'},  
            'content': {'type': 'text'},  
            'author': {'type': 'keyword'}  
        }  
    }  
    # 创建索引  
    es.indices.create(index=index_name, body={'mappings': mappings})  
    print(f"Index '{index_name}' created successfully.")  
else:  
    print(f"Index '{index_name}' already exists.")  
  
# 添加文档  
documents = [  
    {"_id": 1, "title": "Elasticsearch Basics", "content": "Learn the basics of Elasticsearch.", "author": "John Doe"},  
    {"_id": 2, "title": "Advanced Elasticsearch", "content": "Go deeper into Elasticsearch features.", "author": "Jane Smith"},  
    {"_id": 3, "title": "Elasticsearch Performance", "content": "Optimize Elasticsearch for performance.", "author": "Alice Johnson"}  
]  
  
# 使用bulk API批量添加文档  
actions = [  
    {  
        "_index": index_name,  
        "_id": doc['_id'],  
        "_source": doc  
    }  
    for doc in documents  
]  
  
helpers.bulk(es, actions)  
print("Documents added successfully.")  
  
# 搜索文档  
search_body = {  
    "query": {  
        "match": {  
            "content": "Elasticsearch"  
        }  
    }  
}  
  
response = es.search(index=index_name, body=search_body)  
print("Search results:")  
for hit in response['hits']['hits']:  
    print(hit['_source'])  
  
# 清理(可选):删除索引  
# es.indices.delete(index=index_name)  
# print(f"Index '{index_name}' deleted successfully.")

3.代码解释

  1. 连接配置:

    • Elasticsearch(['http://localhost:9200']):连接到运行在本地主机上的Elasticsearch服务器,默认端口为9200。

    • http_auth=('username', 'password'):如果Elasticsearch服务器需要认证,填写用户名和密码。

    • use_sslverify_certs:如果连接使用HTTPS,可以启用这些选项。

  2. 检查连接:

    • 使用es.ping()方法检查连接是否成功。
  3. 创建索引:

    • 使用es.indices.exists(index=index_name)检查索引是否存在。

    • 使用es.indices.create(index=index_name, body={'mappings': mappings})创建索引,并定义文档的映射。

  4. 添加文档:

    • 使用helpers.bulk(es, actions)批量添加文档到索引中。
  5. 搜索文档:

    • 使用es.search(index=index_name, body=search_body)进行搜索,并打印搜索结果。
  6. 清理(可选):

    • 使用es.indices.delete(index=index_name)删除索引。

4.注意事项

  • 服务器地址:确保Elasticsearch服务器正在运行,并且地址和端口配置正确。

  • 认证:如果Elasticsearch服务器需要认证,确保提供正确的用户名和密码。

  • SSL :如果连接使用HTTPS,请正确配置use_sslverify_certs选项。

相关推荐
ZH15455891314 小时前
Flutter for OpenHarmony Python学习助手实战:API接口开发的实现
python·学习·flutter
小宋10214 小时前
Java 项目结构 vs Python 项目结构:如何快速搭一个可跑项目
java·开发语言·python
爱吃生蚝的于勒4 小时前
【Linux】进程信号之捕捉(三)
linux·运维·服务器·c语言·数据结构·c++·学习
The森4 小时前
Linux IO 模型纵深解析 01:从 Unix 传统到 Linux 内核的 IO 第一性原理
linux·服务器·c语言·经验分享·笔记·unix
期待のcode4 小时前
Redis的主从复制与集群
运维·服务器·redis
翼龙云_cloud4 小时前
腾讯云代理商: Linux 云服务器搭建 FTP 服务指南
linux·服务器·腾讯云
一晌小贪欢5 小时前
Python 爬虫进阶:如何利用反射机制破解常见反爬策略
开发语言·爬虫·python·python爬虫·数据爬虫·爬虫python
躺平大鹅5 小时前
5个实用Python小脚本,新手也能轻松实现(附完整代码)
python
yukai080085 小时前
【最后203篇系列】039 JWT使用
python