开源项目 GitHub Trending Weekly 2026-09-28T12:01 正文 ok 467 words

elastic/elasticsearch:免费开源的分布式 RESTful 搜索引擎

英文标题:🔥 elastic/elasticsearch

Elasticsearch 本地开发集群可通过 `elastic` 用户名及 `ES_LOCAL_PASSWORD` 环境变量中的密码,使用语言客户端进行基本认证连接。开发者可借助 REST API 向索引写入单个文档,或通过 `_bulk` API 以 NDJSON 格式批量写入数据。已建立索引的文档可近实时用于搜索,并可在 Kibana 的 Dev Tools、Discover 和 Data Views 中进行调试、探索、筛选及可视化。该流程覆盖了从客户端连接、数据摄取到查询和仪表盘构建的基础开发路径。
打开原文相关 2 · 机会 4
media53.7 KB

中文

要通过语言客户端连接到本地开发 Elasticsearch 集群,可以使用基本认证:用户名为 elastic,密码存储在 ES_LOCAL_PASSWORD 环境变量中。

你将使用以下连接信息:

例如,使用 Python elasticsearch 客户端进行连接:

import os
from elasticsearch import Elasticsearch

username = 'elastic'
password = os.getenv('ES_LOCAL_PASSWORD') # 你在环境变量中设置的值

client = Elasticsearch(
    "http://localhost:9200",
    basic_auth=(username, password)
)

print(client.info())

使用 Dev Tools Console

Kibana 的开发者控制台提供了一种便捷方式,可用于试验和测试请求。要访问控制台,请打开 Kibana,然后进入 Management > Dev Tools。

你可以通过 REST API 发送 JSON 对象(文档)来将数据写入 Elasticsearch。无论是结构化文本、非结构化文本、数值数据还是地理空间数据,Elasticsearch 都能高效地存储和索引这些数据,以支持快速搜索。

对于日志和指标等带时间戳的数据,通常会将文档添加到数据流中。一个数据流由多个自动生成的后备索引组成。

要向索引中添加单个文档,请提交一个指向该索引的 HTTP POST 请求。

POST /customer/_doc/1
{
  "firstname": "Jennifer",
  "lastname": "Walters"
}

如果 customer 索引不存在,此请求会自动创建该索引;同时添加一个 ID 为 1 的新文档,并存储和索引 firstname 与 lastname 字段。

新文档会立即在集群中的任何节点上可用。你可以通过指定文档 ID 的 GET 请求来获取它:

要在一次请求中添加多个文档,请使用 _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"}

已建立索引的文档可在近实时状态下用于搜索。以下搜索会匹配 customer 索引中名字为 Jennifer 的所有客户。

GET customer/_search
{
  "query" : {
    "match" : { "firstname": "Jennifer" }
  }
}

你可以使用 Kibana 中的 Discover 以交互方式搜索和筛选数据。随后,你可以开始创建可视化内容,并构建和分享仪表盘。

首先,创建一个连接至一个或多个 Elasticsearch 索引、数据流或索引别名的 data view。

  1. 前往 Management > Stack Management > Kibana > Data Views。

  2. 选择 Create data view。

  3. 为数据视图输入名称,并输入与一个或多个索引匹配的模式,例如 customer。

  4. 选择 Save data view to Kibana。

要开始探索,请前往 Analytics > Discover。

English / 原文

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.

You’ll use the following connection details:

For example, to connect with the Python elasticsearch client:

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.

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.

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.

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:

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.

PUT customer/_bulk
{ "create": { } }
{ "firstname": "Monica","lastname":"Rambeau"}
{ "create": { } }
{ "firstname": "Carol","lastname":"Danvers"}
{ "create": { } }
{ "firstname": "Wanda","lastname":"Maximoff"}
{ "create": { } }
{ "firstname": "Jennifer","lastname":"Takeda"}

Indexed documents are available for search in near real-time. The following search matches all customers with a first name of Jennifer in the customer index.

GET customer/_search
{
  "query" : {
    "match" : { "firstname": "Jennifer" }
  }
}

You can use Discover in Kibana to interactively search and filter your data. From there, you can start creating visualizations and building and sharing dashboards.

To get started, create a data view that connects to one or more Elasticsearch indices, data streams, or index aliases.

  1. Go to Management > Stack Management > Kibana > Data Views.

  2. Select Create data view.

  3. Enter a name for the data view and a pattern that matches one or more indices, such as customer.

  4. Select Save data view to Kibana.

To start exploring, go to Analytics > Discover.