从零开始使用 Elasticsearch(8.14.0)搭建全文搜索引擎

Elasticsearch 是目前最常用的全文搜索引擎。它可以快速地存储、搜索和分析海量数据,广泛应用于维基百科、Stack Overflow、Github 等网站。

Elasticsearch 的底层是开源库 Lucene。直接使用 Lucene 需要写大量代码,而 Elasticsearch 对其进行了封装,提供了 REST API,使其开箱即用。

本文将详细讲解如何使用最新版本的 Elasticsearch 8.14.0 搭建自己的全文搜索引擎。

一、安装

Elasticsearch 需要 Java 环境。首先,确保你的机器上安装了 Java。如果没有,请先安装 Java,并正确设置环境变量 JAVA_HOME

1. 下载和安装 Elasticsearch

可以从 Elasticsearch 的官方网站下载最新版本的 Elasticsearch:

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.14.0-linux-x86_64.tar.gz
$ tar -xzf elasticsearch-8.14.0-linux-x86_64.tar.gz
$ cd elasticsearch-8.14.0/

2. 启动 Elasticsearch

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

$ ./bin/elasticsearch

如果遇到错误 "max virtual memory areas vm.max_map_count [65530] is too low",请运行以下命令解决:

$ sudo sysctl -w vm.max_map_count=262144

正常启动后,Elasticsearch 会在默认的 9200 端口运行。打开另一个命令行窗口,请求该端口以验证安装:

$ curl -k --user elastic 'https://localhost:9200'

你应该会看到类似以下的 JSON 响应,包含节点、集群、版本等信息:

{
  "name" : "node-1",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "tf9250XhQ6ee4h7YI11anA",
  "version" : {
    "number" : "8.14.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "19c13d0",
    "build_date" : "2024-01-18T20:44:24.823Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "7.10.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

按下 Ctrl + C 可以停止 Elasticsearch。

如果需要远程访问 Elasticsearch,可以修改安装目录中的 config/elasticsearch.yml 文件,取消 network.host 的注释并将其值改为 0.0.0.0

network.host: 0.0.0.0

重新启动 Elasticsearch 后,即可远程访问。但线上服务不要这样设置,要设成具体的 IP。

二、基本概念

2.1 Node 与 Cluster

Elasticsearch 是一个分布式数据库,允许多台服务器协同工作。每台服务器可以运行多个 Elasticsearch 实例,单个实例称为一个节点(node),一组节点构成一个集群(cluster)。

2.2 Index

Elasticsearch 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。数据管理的顶层单位是 Index(索引),类似于关系型数据库的数据库。每个 Index 的名字必须是小写。

查看当前节点的所有 Index:

$ curl -X GET 'http://localhost:9200/_cat/indices?v'

2.3 Document

Index 中的单条记录称为 Document(文档)。Document 使用 JSON 格式表示,例如:

{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}

同一个 Index 里的 Document 结构不要求完全一致,但最好保持相同,以提高搜索效率。

2.4 Type

Document 可以分组,这种分组称为 Type。Type 是逻辑分组,用来过滤 Document。不同的 Type 应该有相似的结构(schema)。Elasticsearch 6.x 版本后,每个 Index 只允许包含一个 Type,7.x 版本彻底移除 Type。

列出每个 Index 包含的 Type:

$ curl 'localhost:9200/_mapping?pretty=true'

三、新建和删除 Index

新建 Index:

$ curl -X PUT 'localhost:9200/weather'

服务器返回的 JSON 对象中,acknowledged 字段表示操作成功:

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

删除 Index:

$ curl -X DELETE 'localhost:9200/weather'

四、中文分词设置

安装中文分词插件(以 ik 为例):

$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.14.0/elasticsearch-analysis-ik-8.14.0.zip

重新启动 Elasticsearch 后,自动加载这个新安装的插件。

新建 Index 并指定需要分词的字段:

$ curl -X PUT 'localhost:9200/accounts' -H 'Content-Type: application/json' -d '
{
  "mappings": {
    "properties": {
      "user": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
      },
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
      },
      "desc": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
      }
    }
  }
}'

上面代码中,新建一个名为 accounts 的 Index,其中有三个字段 usertitledesc 都需要使用中文分词器 ik_max_word

五、数据操作

5.1 新增记录

向指定的 /Index/_doc 发送 PUT 请求,可以在 Index 中新增一条记录:

$ curl -X PUT 'localhost:9200/accounts/_doc/1' -H 'Content-Type: application/json' -d '
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}'

服务器返回 JSON 对象,给出 Index、Id、Version 等信息:

{
  "_index": "accounts",
  "_id": "1",
  "_version": 1,
  "result": "created"
}

新增记录时,可以不指定 Id,这时要改为 POST 请求:

$ curl -X POST 'localhost:9200/accounts/_doc/' -H 'Content-Type: application/json' -d '
{
  "user": "李四",
  "title": "工程师",
  "desc": "系统管理"
}'

返回的 JSON 对象中,_id 字段为随机字符串:

{
  "_index": "accounts",
  "_id": "AV3qGfrC6jMbsbXb6k1p",
  "_version": 1,
  "result": "created"
}

5.2 查看记录

/Index/_doc/Id 发出 GET 请求查看记录:

$ curl 'localhost:9200/accounts/_doc/1?pretty=true'

返回的数据中,found 字段表示查询成功,_source 字段返回原始记录:

{
  "_index" : "accounts",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "user" : "张三",
    "title" : "工程师",
    "desc" : "数据库管理"
  }
}

5.3 删除记录

删除记录:

$ curl -X DELETE 'localhost:9200/accounts/_doc/1'

5.4 更新记录

更新记录:

$ curl -X PUT 'localhost:9200/accounts/_doc/1' -H 'Content-Type: application/json' -d '
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理,软件开发"
}'

返回结果中,版本(version)和操作类型(result)字段发生变化:

{
  "_index": "accounts",
  "_id": "1",
  "_version": 2,
  "result": "updated"
}

六、数据查询

6.1 返回所有记录

使用 GET 方法,直接请求 /Index/_search 返回所有记录

$ curl 'localhost:9200/accounts/_search'

返回结果中,took 字段表示操作耗时,hits 字段表示命中的记录:

{
  "took": 2,
  "timed_out": false,
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "hits": [
      {
        "_index": "accounts",
        "_id": "1",
        "_source": {
          "user": "张三",
          "title": "工程师",
          "desc": "数据库管理"
        }
      },
      {
        "_index": "accounts",
        "_id": "2",
        "_source": {
          "user": "李四",
          "title": "工程师",
          "desc": "系统管理"
        }
      }
    ]
  }
}

至此,你已经学会了如何使用 Elasticsearch 8.14.0 安装、配置和执行基本的增删查改操作。希望这篇文章能帮助你搭建起自己的全文搜索引擎。

相关推荐

  1. 开始SpringCloud

    2024-06-13 07:56:03       11 阅读
  2. 开始Go语言开发环境

    2024-06-13 07:56:03       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 07:56:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 07:56:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 07:56:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 07:56:03       18 阅读

热门阅读

  1. git - LFS 使用方法

    2024-06-13 07:56:03       6 阅读
  2. LINUX中使用DT_MACHINE_START/MACHINE_START宏

    2024-06-13 07:56:03       5 阅读
  3. 新人学习笔记之(初识C语言)

    2024-06-13 07:56:03       6 阅读
  4. 仓库风格-系统架构师(九)

    2024-06-13 07:56:03       6 阅读
  5. xcode命令行

    2024-06-13 07:56:03       4 阅读
  6. RestTemplate的异常重试机制

    2024-06-13 07:56:03       8 阅读
  7. Python列表和元组的底层实现

    2024-06-13 07:56:03       7 阅读
  8. 轻资产互联网项目:零撸看广告小游戏开发

    2024-06-13 07:56:03       6 阅读
  9. CP AUTOSAR标准之COM(AUTOSAR_CP_SWS_COM)(更新中……)

    2024-06-13 07:56:03       8 阅读
  10. OPenCV中绘制多条多边形曲线函数polylines的使用

    2024-06-13 07:56:03       7 阅读