专业的编程技术博客社区

网站首页 > 博客文章 正文

掌握Elasticsearch!从入门到实战的全面指南,附详细代码解析

baijin 2025-03-01 12:48:36 博客文章 13 ℃ 0 评论

在大数据时代,Elasticsearch凭借其强大的全文搜索和分析功能,成为了许多企业和开发者的首选搜索引擎。如果你想全面掌握Elasticsearch的基础使用,今天这篇文章将会为你详尽解析。无论你是初学者,还是想要快速上手的技术高手,这里都有你需要的信息和代码实例!

一、Elasticsearch简介

Elasticsearch是一个分布式搜索和分析引擎,基于Apache Lucene开发。它能够处理海量数据的实时搜索、分布式计算和数据分析,广泛应用于日志分析、全文检索和实时监控等领域。

官网地址:Elasticsearch

二、环境搭建与安装

1.下载与安装

首先,从Elasticsearch官网上下载适合你操作系统的安装包。

# 下载并解压Elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.0-linux-x86_64.tar.gz
tar -xzf elasticsearch-7.15.0-linux-x86_64.tar.gz
cd elasticsearch-7.15.0/

2.启动Elasticsearch

解压后的目录中,运行以下命令启动Elasticsearch:

# 启动Elasticsearch
./bin/elasticsearch

Elasticsearch成功启动后,你可以在浏览器中访问以下地址验证:

http://localhost:9200

你应该会看到类似下面的JSON响应:

{
  "name" : "your-node-name",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "your-cluster-id",
  "version" : {
    "number" : "7.15.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "some-hash",
    "build_date" : "2021-09-16T03:05:29.143308Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

三、基础操作

1.创建索引

在Elasticsearch中,数据存储在索引(Index)中。我们使用PUT请求创建一个新的索引。

# 创建名为'my_index'的索引
curl -X PUT "localhost:9200/my_index"

Elasticsearch会返回如下结果,表示索引创建成功:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "my_index"
}

2.插入文档

文档(Document)是Elasticsearch存储和检索数据的基本单元。我们可以使用POST请求向索引中插入文档。

# 向'my_index'索引中插入一条文档
curl -X POST "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "user": "Tom",
  "post_date": "2024-06-16",
  "message": "Hello Elasticsearch!"
}
'

Elasticsearch会返回如下结果,表示文档插入成功:

{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

3.检索文档

我们使用GET请求从索引中检索文档。以下示例检索ID为1的文档。

# 检索ID为1的文档
curl -X GET "localhost:9200/my_index/_doc/1"

Elasticsearch会返回该文档的详情:

{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "Tom",
    "post_date" : "2024-06-16",
    "message" : "Hello Elasticsearch!"
  }
}

4.搜索文档

Elasticsearch强大的地方在于其搜索能力。我们可以使用GET请求,并在请求体中使用DSL查询语句来实现复杂的搜索。

# 搜索包含"message"字段为"Hello"的文档
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "message": "Hello"
    }
  }
}
'

Elasticsearch返回搜索结果列表:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "user" : "Tom",
          "post_date" : "2024-06-16",
          "message" : "Hello Elasticsearch!"
        }
      }
    ]
  }
}

四、数据的更新与删除

1.更新文档

使用POST请求和 _update 子路径来更新文档。

# 更新ID为1的文档,修改"user"字段
curl -X POST "localhost:9200/my_index/_update/1" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "user": "Jerry"
  }
}
'

Elasticsearch返回更新结果:

{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

2.删除文档

使用DELETE请求删除文档。

# 删除ID为1的文档
curl -X DELETE "localhost:9200/my_index/_doc/1"

Elasticsearch返回删除结果:

{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

五、进阶操作

1.批量操作

使用POST请求和 _bulk 子路径进行批量操作,可以插入、更新或删除多个文档。

# 批量插入文档
curl -X POST "localhost:9200/my_index/_bulk" -H 'Content-Type: application/x-ndjson' -d'
{ "index" : { "_id" : "2" } }
{ "user" : "Alice", "post_date" : "2024-06-16", "message" : "Elasticsearch is cool!" }
{ "index" : { "_id" : "3" } }
{ "user" : "Bob", "post_date" : "2024-06-16", "message" : "Elasticsearch rocks!" }
'

Elasticsearch返回批量操作结果:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表