加载中

入门指南

本页面指导您完成 Python 客户端的安装过程,向您展示如何实例化客户端,以及如何使用它执行基本的 Elasticsearch 操作。

  • Python 3.10 或更高版本
  • pip,通常随 Python 一起默认安装

要安装客户端的最新版本,请运行以下命令

python -m pip install elasticsearch
		
python -m pip install "elasticsearch[async]"
		

请参考 安装 页面了解更多信息。

您可以使用 API 密钥和 Elasticsearch 端点连接到 Elastic Cloud。

import os
from elasticsearch import Elasticsearch

client = Elasticsearch(
    "https://...",
    api_key=os.environ["ELASTIC_API_KEY"],
)
		
  1. Elasticsearch 端点
import os
from elasticsearch import AsyncElasticsearch

client = AsyncElasticsearch(
    "https://...",
    api_key=os.environ["ELASTIC_API_KEY"],
)
		
  1. Elasticsearch 端点

您的 Elasticsearch 端点可以在部署的 My deployment 页面上找到

Finding Elasticsearch endpoint

您可以在安全性下的 Management 页面上生成 API 密钥。

Create API key

有关其他连接选项,请参考 连接 部分。

是时候使用 Elasticsearch 了!本节将带您了解 Elasticsearch 的基本且最重要的操作。有关更多操作和更高级的示例,请参考 示例 页面。

以下是如何创建 my_index 索引的方法

client.indices.create(index="my_index")
		
await client.indices.create(index="my_index")
		

(可选)您可以先通过自定义映射定义特征的期望类型。

mappings = {
    "properties": {
        "foo": {"type": "text"},
        "bar": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256,
                }
            },
        },
    }
}

client.indices.create(index="my_index", mappings=mappings)
		
mappings = {
    "properties": {
        "foo": {"type": "text"},
        "bar": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256,
                }
            },
        },
    }
}

await client.indices.create(index="my_index", mappings=mappings)
		

这会使用 index API 索引一个文档

client.index(
    index="my_index",
    id="my_document_id",
    document={
        "foo": "foo",
        "bar": "bar",
    }
)
		
await client.index(
    index="my_index",
    id="my_document_id",
    document={
        "foo": "foo",
        "bar": "bar",
    }
)
		

您还可以使用 bulk 辅助函数一次性索引多个文档

from elasticsearch import helpers

def generate_docs():
    for i in range(10):
        yield {
            "_index": "my_index",
            "foo": f"foo {i}",
            "bar": "bar",
        }

helpers.bulk(client, generate_docs())
		
from elasticsearch import helpers

async def generate_docs():
    for i in range(10):
        yield {
            "_index": "my_index",
            "foo": f"foo {i}",
            "bar": "bar",
        }

async def bulk_example():
    await helpers.async_bulk(client, generate_docs())
		

这些辅助函数是执行批量导入的推荐方法。虽然也可以直接使用 client.bulk 执行批量导入,但辅助函数可以处理重试、分块导入等功能。有关更多详细信息,请参考 客户端辅助函数 页面。

您可以使用以下代码获取文档

client.get(index="my_index", id="my_document_id")
		
await client.get(index="my_index", id="my_document_id")
		

以下是如何使用 Python 客户端创建单个 match 查询的方法

client.search(index="my_index", query={
    "match": {
        "foo": "foo"
    }
})
		
await client.search(index="my_index", query={
    "match": {
        "foo": "foo"
    }
})
		

以下是如何更新文档的方法,例如添加新字段

client.update(
    index="my_index",
    id="my_document_id",
    doc={
        "foo": "bar",
        "new_field": "new value",
    }
)
		
await client.update(
    index="my_index",
    id="my_document_id",
    doc={
        "foo": "bar",
        "new_field": "new value",
    }
)
		
client.delete(index="my_index", id="my_document_id")
		
await client.delete(index="my_index", id="my_document_id")
		
client.indices.delete(index="my_index")
		
await client.indices.delete(index="my_index")
		
© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.