Elasticsearch 实战(2)

28 篇文章 整理于 2026-11-15

用 Docker 启动 Elasticsearch 8.19.25 单节点,创建索引、写入两条文档,再用 match 搜出第一条结果。

Elasticsearch 8.19.25 快速开始

用 Docker 启动一个 Elasticsearch 8.19.25 节点,写两条文档,再用 match 搜出其中一条。只需要 Docker 和终端。先下载官方镜像,后面的请求都在容器里执行。实验用 --network none,不映射端口,容器里用 127.0.0.1:9200 访问 Elasticsearch。这里关闭认证,仅用于这个隔离练习。

启动单节点

docker pull docker.elastic.co/elasticsearch/elasticsearch:8.19.25
docker run --pull=never --rm -d --name es-quickstart --network none --hostname localhost --memory 2g \
  -e 'discovery.type=single-node' -e 'xpack.security.enabled=false' \
  -e 'ES_JAVA_OPTS=-Xms512m -Xmx512m' \
  docker.elastic.co/elasticsearch/elasticsearch:8.19.25

这里显式指定 --hostname localhost。实测省略它时,容器在隔离网络里无法解析默认主机名,启动报 UnknownHostException。节点启动需要一点时间,下面命令如果暂时连不上,过几秒再跑。看到 timed_out 为 false、number_of_nodes 为 1 就可以继续。

docker exec es-quickstart curl -fsS --max-time 12 \
  'http://127.0.0.1:9200/?filter_path=version.number&pretty' \
  'http://127.0.0.1:9200/_cluster/health?wait_for_status=yellow&timeout=8s&filter_path=status,timed_out,number_of_nodes&pretty'
{
  "version" : {
    "number" : "8.19.25"
  }
}
{
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1
}

建索引,写入两条文档

给 learn-search 建一个主分片,副本数设为 0,适合这个单节点实验。title 用 text 类型,接下来可以做全文查询。

docker exec -i es-quickstart curl -fsS -X PUT \
  'http://127.0.0.1:9200/learn-search?pretty' \
  -H 'Content-Type: application/json' --data-binary @- <<'JSON'
{
  "settings": {"number_of_shards": 1, "number_of_replicas": 0},
  "mappings": {"properties": {"title": {"type": "text"}}}
}
JSON
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "learn-search"
}

用 _bulk 一次写入两条文档。请求体是 NDJSON,每条文档占两行,先写操作和 ID,再写文档内容,最后一行也要换行。refresh=wait_for 会等这批写入对搜索可见再返回,下一条查询可以直接跑。

docker exec -i es-quickstart curl -fsS \
  'http://127.0.0.1:9200/learn-search/_bulk?refresh=wait_for&filter_path=errors,items.index._id,items.index.result,items.index.status&pretty' \
  -H 'Content-Type: application/x-ndjson' --data-binary @- <<'NDJSON'
{"index":{"_id":"1"}}
{"title":"Kafka consumer offsets"}
{"index":{"_id":"2"}}
{"title":"Elasticsearch search basics"}
NDJSON
{
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_id" : "1",
        "result" : "created",
        "status" : 201
      }
    },
    {
      "index" : {
        "_id" : "2",
        "result" : "created",
        "status" : 201
      }
    }
  ]
}

用 match 搜一次

在 title 里查大写的 CONSUMER。实测只命中文档 1,它的标题里写的是小写 consumer。这里用的是 text 字段默认的标准分析器,索引内容和 match 的查询文本都经过分析。

docker exec -i es-quickstart curl -fsS \
  'http://127.0.0.1:9200/learn-search/_search?filter_path=hits.total,hits.hits._id,hits.hits._source&pretty' \
  -H 'Content-Type: application/json' --data-binary @- <<'JSON'
{"query":{"match":{"title":"CONSUMER"}}}
JSON
{
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "hits" : [
      {
        "_id" : "1",
        "_source" : {
          "title" : "Kafka consumer offsets"
        }
      }
    ]
  }
}

练完用下面的命令停止容器。启动时用了 --rm,容器退出后会被 Docker 移除。

docker stop es-quickstart

下一步可以读本站的 match 全文检索和创建映射。下方原目录的 Docker 安装篇基于 7.4.2,照着做时留意版本。

参考 Elastic 8.19 文档的 Docker 安装、refresh 参数和 match 查询。

文章目录

28 篇
阅读前准备与来源说明

Elasticsearch 实战(2)