Elasticsearch:使用 Python 构建您的第一个搜索查询
概述
Elasticsearch 简介
Elasticsearch 提供了一系列搜索技术,从文本搜索的行业标准 BM25 开始。它还提供由 AI 模型驱动的语义搜索,可根据上下文和意图改善结果。
Elasticsearch 为多种编程语言提供官方客户端,包括 Python、Rust、Java、JavaScript 等。这些客户端为索引、搜索和集群管理提供完整的 API 支持。它们针对性能进行了优化并与 Elasticsearch 版本保持同步,以确保兼容性和安全性。
让我们开始吧
在此示例中,您将索引几个文档并使用 Python 查询它们。在本指南结束时,您将学习如何将后端应用程序连接到 Elasticsearch 来回答您的查询。
创建 Elastic Cloud 项目
开始使为期 14 天的试用。访问并创建账户后,请按照以下步骤学习如何启动第一个 Elasticsearch Serverless 项目,选择 Elasticsearch。
然后创建 General purpose,给它起一个名字,比如 My Project 并创建它。

让我们创建第一个 Elasticsearch 索引,我们可以将其命名为 my-index。选择创建我的索引。
您已创建了第一个索引!接下来,创建一个 API 密钥,以便您的应用程序可以与 Elasticsearch 通信。选择您的首选语言。对于此示例,请利用 Python。
开始将数据导入 Elasticsearch
在您的终端中,使用 pip 安装 Elasticsearch 客户端:
pip install elasticsearch
从右上角复制您的 API 密钥,并将其与项目 URL 一起添加到客户端的配置中。我们已经可以为索引创建映射,该索引仅有一个文本字段,富有创意地命名为“文本”。
from elasticsearch import Elasticsearch
client = Elasticsearch(
"https://my-project-bff300.es.us-east-1.aws.elastic.cloud:443",
api_key="YOUR-API-KEY"
)
index_name = "my-index"
mappings = {
"properties": {
"text": {
"type": "text"
}
}
}
mapping_response = client.indices.put_mapping(index=index_name, body=mappings)
print(mapping_response)
然后我们最终可以使用批量请求向 Elasticsearch 发送数据了,让我们使用 _bulk 请求索引 3 个文档。在索引数百到数十亿份文档时,也请记住使用批量请求,因为这是在 Elasticsearch 中索引大量文档时的首选方式。
docs = [
{
"text": "Yellowstone National Park is one of the largest national parks in the United States. It ranges from the Wyoming to Montana and Idaho, and contains an area of 2,219,791 acress across three different states. Its most famous for hosting the geyser Old Faithful and is centered on the Yellowstone Caldera, the largest super volcano on the American continent. Yellowstone is host to hundreds of species of animal, many of which are endangered or threatened. Most notably, it contains free-ranging herds of bison and elk, alongside bears, cougars and wolves. The national park receives over 4.5 million visitors annually and is a UNESCO World Heritage Site."
},
{
"text": "Yosemite National Park is a United States National Park, covering over 750,000 acres of land in California. A UNESCO World Heritage Site, the park is best known for its granite cliffs, waterfalls and giant sequoia trees. Yosemite hosts over four million visitors in most years, with a peak of five million visitors in 2016. The park is home to a diverse range of wildlife, including mule deer, black bears, and the endangered Sierra Nevada bighorn sheep. The park has 1,200 square miles of wilderness, and is a popular destination for rock climbers, with over 3,000 feet of vertical granite to climb. Its most famous and cliff is the El Capitan, a 3,000 feet monolith along its tallest face."
},
{
"text": "Rocky Mountain National Park is one of the most popular national parks in the United States. It receives over 4.5 million visitors annually, and is known for its mountainous terrain, including Longs Peak, which is the highest peak in the park. The park is home to a variety of wildlife, including elk, mule deer, moose, and bighorn sheep. The park is also home to a variety of ecosystems, including montane, subalpine, and alpine tundra. The park is a popular destination for hiking, camping, and wildlife viewing, and is a UNESCO World Heritage Site."
}
]
bulk_response = helpers.bulk(client, docs, index=index_name)
print(bulk_response)
您应该能够在 Elasticsearch 中看到这些文档。
使用 Elasticsearch
构建查询
创建一个新脚本(例如 search.py),该脚本定义了一个查询并执行以下搜索请求:
FROM my-index
| WHERE MATCH(text, "yosemite")
| LIMIT 5
在 client.esql.query 中添加此查询:
from elasticsearch import Elasticsearch
client = Elasticsearch(
"https://my-project-bff307.es.us-east-1.aws.elastic.cloud:443",
api_key="YOUR-API-KEY"
)
# Execute the search query
response = client.esql.query(
query="""
FROM my-index
| WHERE MATCH(text, "yosemite")
| LIMIT 5
""",
format="csv"
)
print(response)
检查您的结果:
"优胜美地国家公园是美国的一个国家公园,位于加利福尼亚州,占地面积超过 75 万英亩。作为联合国教科文组织世界遗产,该公园以花岗岩悬崖、瀑布和巨型红杉树而闻名。优胜美地国家公园大多数年份都会接待超过 400 万游客,2016 年的峰值达到 500 万游客。公园里栖息着各种各样的野生动物,包括骡鹿、黑熊以及濒临灭绝的内华达山脉大角羊。该公园拥有 1,200 平方英里的荒野,是攀岩爱好者的热门目的地,有超过 3,000 英尺的垂直花岗岩可供攀爬。它最著名的悬崖是酋长岩(El Capitan),最高处是一块 3,000 英尺高的巨石。”
现在,您可以使用该客户端从任何 Python 后端(如 Flask、Django 等)查询 Elasticsearch 了。请查看 Elasticsearch Python 客户端文档以进一步探索。
后续步骤
感谢您抽出时间利用 Elastic Cloud 来为您的数据设置语义搜索。当您开始使用 Elastic 时,您需要了解在整个环境中部署时作为用户应管理的一些操作、安全和数据组件。
准备好开始了吗?在 Elastic Cloud 上免费试用 14 天,或在 Search AI 101 上尝试 15 分钟的实践学习。