elastic / elasticsearch
elastic / elasticsearch
Elasticsearch is a distributed search and analytics engine, scalable data store and vector database optimized for speed and relevance on production-scale workloads. Elasticsearch is the foundation of Elastic’s open Stack platform. Search in near real-time over massive datasets, perform vector searches, integrate with generative AI applications, and much more. Elasticsearch 是一个分布式搜索和分析引擎、可扩展的数据存储和向量数据库,针对生产规模工作负载下的速度和相关性进行了优化。Elasticsearch 是 Elastic 开源 Stack 平台的基础。它支持在海量数据集上进行近乎实时的搜索、执行向量搜索、与生成式 AI 应用程序集成等等。
Use cases enabled by Elasticsearch include: Retrieval Augmented Generation (RAG), Vector search, Full-text search, Logs, Metrics, Application performance monitoring (APM), Security logs … and more! To learn more about Elasticsearch’s features and capabilities, see our product page. To access information on machine learning innovations and the latest Lucene contributions from Elastic, more information can be found in Search Labs. Elasticsearch 支持的用例包括:检索增强生成 (RAG)、向量搜索、全文搜索、日志、指标、应用程序性能监控 (APM)、安全日志等!要了解有关 Elasticsearch 功能和特性的更多信息,请参阅我们的产品页面。要获取有关机器学习创新和 Elastic 对 Lucene 最新贡献的信息,可以在 Search Labs 中找到更多详情。
Get started
入门指南
The simplest way to set up Elasticsearch is to create a managed deployment with Elasticsearch Service on Elastic Cloud. If you prefer to install and manage Elasticsearch yourself, you can download the latest version from elastic.co/downloads/elasticsearch. 设置 Elasticsearch 最简单的方法是通过 Elastic Cloud 上的 Elasticsearch Service 创建托管部署。如果您更喜欢自行安装和管理 Elasticsearch,可以从 elastic.co/downloads/elasticsearch 下载最新版本。
Run Elasticsearch locally
在本地运行 Elasticsearch
Warning: DO NOT USE THESE INSTRUCTIONS FOR PRODUCTION DEPLOYMENTS. This setup is intended for local development and testing only. Quickly set up Elasticsearch and Kibana in Docker for local development or testing, using the start-local script. ℹ️ For more detailed information about the start-local setup, refer to the README on GitHub.
警告: 请勿将这些说明用于生产环境部署。此设置仅用于本地开发和测试。使用 start-local 脚本可以在 Docker 中快速设置 Elasticsearch 和 Kibana,以便进行本地开发或测试。ℹ️ 有关 start-local 设置的更多详细信息,请参阅 GitHub 上的 README。
Prerequisites: If you don’t have Docker installed, download and install Docker Desktop for your operating system. If you’re using Microsoft Windows, then install Windows Subsystem for Linux (WSL). 先决条件: 如果您尚未安装 Docker,请为您的操作系统下载并安装 Docker Desktop。如果您使用的是 Microsoft Windows,请安装适用于 Linux 的 Windows 子系统 (WSL)。
Trial license: This setup comes with a one-month trial license that includes all Elastic features. After the trial period, the license reverts to Free and open - Basic. Refer to Elastic subscriptions for more information. 试用许可: 此设置附带一个包含所有 Elastic 功能的为期一个月的试用许可。试用期结束后,许可将恢复为“免费且开放 - 基础版”。有关更多信息,请参阅 Elastic 订阅。
Run start-local: To set up Elasticsearch and Kibana locally, run the start-local script: curl -fsSL https://elastic.co/start-local | sh
运行 start-local: 要在本地设置 Elasticsearch 和 Kibana,请运行 start-local 脚本:curl -fsSL https://elastic.co/start-local | sh
This script creates an elastic-start-local folder containing configuration files and starts both Elasticsearch and Kibana using Docker. After running the script, you can access Elastic services at the following endpoints: Elasticsearch: http://localhost:9200, Kibana: http://localhost:5601. The script generates a random password for the elastic user, which is displayed at the end of the installation and stored in the .env file.
该脚本会创建一个包含配置文件的 elastic-start-local 文件夹,并使用 Docker 启动 Elasticsearch 和 Kibana。运行脚本后,您可以通过以下端点访问 Elastic 服务:Elasticsearch: http://localhost:9200,Kibana: http://localhost:5601。脚本会为 elastic 用户生成一个随机密码,该密码显示在安装结束时,并存储在 .env 文件中。
Caution: This setup is for local testing only. HTTPS is disabled, and Basic authentication is used for Elasticsearch. For security, Elasticsearch and Kibana are accessible only through localhost. 注意: 此设置仅用于本地测试。HTTPS 已禁用,Elasticsearch 使用基本身份验证。出于安全考虑,Elasticsearch 和 Kibana 仅可通过 localhost 访问。
API access
API 访问
An API key for Elasticsearch is generated and stored in the .env file as ES_LOCAL_API_KEY. Use this key to connect to Elasticsearch with a programming language client or the REST API. From the elastic-start-local folder, check the connection to Elasticsearch using curl:
Elasticsearch 的 API 密钥已生成并存储在 .env 文件中,变量名为 ES_LOCAL_API_KEY。使用此密钥可以通过编程语言客户端或 REST API 连接到 Elasticsearch。在 elastic-start-local 文件夹中,使用 curl 检查与 Elasticsearch 的连接:
source .env
curl $ES_LOCAL_URL -H "Authorization: ApiKey ${ES_LOCAL_API_KEY}"
To use the password for the elastic user, set and export the ES_LOCAL_PASSWORD environment variable. For example:
要使用 elastic 用户的密码,请设置并导出 ES_LOCAL_PASSWORD 环境变量。例如:
source .env
export ES_LOCAL_PASSWORD
Send requests to Elasticsearch
向 Elasticsearch 发送请求
You send data and other requests to Elasticsearch through REST APIs. You can interact with Elasticsearch using any client that sends HTTP requests, such as the Elasticsearch language clients and curl. 您可以通过 REST API 向 Elasticsearch 发送数据和其他请求。您可以使用任何发送 HTTP 请求的客户端与 Elasticsearch 进行交互,例如 Elasticsearch 语言客户端和 curl。
Using curl: Here’s an example curl command to create a new Elasticsearch index, using basic auth: 使用 curl: 以下是使用基本身份验证创建新 Elasticsearch 索引的 curl 命令示例:
curl -u elastic:$ES_LOCAL_PASSWORD \
-X PUT \
http://localhost:9200/my-new-index \
-H 'Content-Type: application/json'
Using a language client: To connect to your local dev Elasticsearch cluster with a language client, you can use basic authentication with the elastic username and the password stored in the ES_LOCAL_PASSWORD environment variable.
使用语言客户端: 要使用语言客户端连接到本地开发 Elasticsearch 集群,您可以使用 elastic 用户名和存储在 ES_LOCAL_PASSWORD 环境变量中的密码进行基本身份验证。
You’ll use the following connection details: 您将使用以下连接详细信息:
- Elasticsearch endpoint:
http://localhost:9200 - Username:
elastic - Password:
$ES_LOCAL_PASSWORD(Value you set in the environment variable)
For example, to connect with the Python elasticsearch client: 例如,使用 Python elasticsearch 客户端进行连接:
import os
from elasticsearch import Elasticsearch
username = 'elastic'
password = os.getenv('ES_LOCAL_PASSWORD') # Value you set in the environment variable
client = Elasticsearch(
"http://localhost:9200",
basic_auth=(username, password)
)
print(client.info())
Using the Dev Tools Console: Kibana’s developer console provides an easy way to experiment and test requests. To access the console, open Kibana, then go to Management > Dev Tools. 使用开发工具控制台: Kibana 的开发控制台提供了一种实验和测试请求的简便方法。要访问控制台,请打开 Kibana,然后转到 Management > Dev Tools。
Add data
添加数据
You index data into Elasticsearch by sending JSON objects (documents) through the REST APIs. Whether you have structured or unstructured text, numerical data, or geospatial data, Elasticsearch efficiently stores and indexes it in a way that supports fast searches. 您可以通过 REST API 发送 JSON 对象(文档)将数据索引到 Elasticsearch 中。无论您拥有结构化还是非结构化文本、数值数据还是地理空间数据,Elasticsearch 都能以支持快速搜索的方式高效地存储和索引这些数据。
For timestamped data such as logs and metrics, you typically add documents to a data stream made up of multiple auto-generated backing indices. 对于日志和指标等带时间戳的数据,您通常会将文档添加到由多个自动生成的后备索引组成的数据流中。
To add a single document to an index, submit an HTTP post request that targets the index. 要将单个文档添加到索引中,请提交一个针对该索引的 HTTP POST 请求。
POST /customer/_doc/1
{
"firstname": "Jennifer",
"lastname": "Walters"
}
This request automatically creates the customer index if it doesn’t exist, adds a new document that has an ID of 1, and stores and indexes the firstname and lastname fields. The new document is available immediately from any node in the cluster. You can retrieve it with a GET request that specifies its document ID:
如果 customer 索引不存在,此请求会自动创建它,添加一个 ID 为 1 的新文档,并存储和索引 firstname 和 lastname 字段。新文档可立即从集群中的任何节点访问。您可以使用指定文档 ID 的 GET 请求检索它:
GET /customer/_doc/1
To add multiple documents in one request, use the _bulk API. Bulk data must be newline-delimited JSON (NDJSON). Each line must end in a newline character (\n), including the last line.
要在一个请求中添加多个文档,请使用 _bulk API。批量数据必须是换行符分隔的 JSON (NDJSON)。每一行必须以换行符 (\n) 结尾,包括最后一行。
PUT customer/_bulk
{ "create": { } }
{ "firstname": "Monica","lastname":"Rambeau"}
{ "create": { } }
{ "firstname": "Carol","lastname":"Danvers"}
{ "create": { } }
{ "firstname": "Wanda","lastname":"Maximoff"}
{ "create": { } }
{ "firstname": "Jennifer","lastname":"Takeda"}
Search
搜索
Indexed documents are available for search in near real-time. The following search matches all customers with a first name of Jen. 索引后的文档可近乎实时地进行搜索。以下搜索匹配所有名字为 Jen 的客户。