0


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

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

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

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

一、安装

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

  1. JAVA_HOME

1. 下载和安装 Elasticsearch

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

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

2. 启动 Elasticsearch

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

  1. $ ./bin/elasticsearch

如果遇到错误

  1. "max virtual memory areas vm.max_map_count [65530] is too low"

,请运行以下命令解决:

  1. $ sudosysctl-wvm.max_map_count=262144

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

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

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

  1. {"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"}

按下

  1. Ctrl + C

可以停止 Elasticsearch。

如果需要远程访问 Elasticsearch,可以修改安装目录中的

  1. config/elasticsearch.yml

文件,取消

  1. network.host

的注释并将其值改为

  1. 0.0.0.0

  1. 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:

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

2.3 Document

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

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

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

2.4 Type

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

列出每个 Index 包含的 Type:

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

三、新建和删除 Index

新建 Index:

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

服务器返回的 JSON 对象中,

  1. acknowledged

字段表示操作成功:

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

删除 Index:

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

四、中文分词设置

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

  1. $ ./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 并指定需要分词的字段:

  1. $ curl-X PUT 'localhost:9200/accounts'-H'Content-Type: application/json'-d'
  2. {
  3. "mappings": {
  4. "properties": {
  5. "user": {
  6. "type": "text",
  7. "analyzer": "ik_max_word",
  8. "search_analyzer": "ik_max_word"
  9. },
  10. "title": {
  11. "type": "text",
  12. "analyzer": "ik_max_word",
  13. "search_analyzer": "ik_max_word"
  14. },
  15. "desc": {
  16. "type": "text",
  17. "analyzer": "ik_max_word",
  18. "search_analyzer": "ik_max_word"
  19. }
  20. }
  21. }
  22. }'

上面代码中,新建一个名为

  1. accounts

的 Index,其中有三个字段

  1. user

  1. title

  1. desc

都需要使用中文分词器

  1. ik_max_word

五、数据操作

5.1 新增记录

向指定的

  1. /Index/_doc

发送 PUT 请求,可以在 Index 中新增一条记录:

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

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

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

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

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

返回的 JSON 对象中,

  1. _id

字段为随机字符串:

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

5.2 查看记录

  1. /Index/_doc/Id

发出 GET 请求查看记录:

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

返回的数据中,

  1. found

字段表示查询成功,

  1. _source

字段返回原始记录:

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

5.3 删除记录

删除记录:

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

5.4 更新记录

更新记录:

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

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

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

六、数据查询

6.1 返回所有记录

使用 GET 方法,直接请求

  1. /Index/_search

返回所有记录

  1. $ curl'localhost:9200/accounts/_search'

返回结果中,

  1. took

字段表示操作耗时,

  1. hits

字段表示命中的记录:

  1. {"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 安装、配置和执行基本的增删查改操作。希望这篇文章能帮助你搭建起自己的全文搜索引擎。


本文转载自: https://blog.csdn.net/wenbingy/article/details/139617671
版权归原作者 技术蜜糖罐 所有, 如有侵权,请联系我们删除。

“从零开始使用 Elasticsearch(8.14.0)搭建全文搜索引擎”的评论:

还没有评论