欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 从零开始使用 Elasticsearch(8.14.0)搭建全文搜索引擎

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

2025/2/25 12:33:01 来源:https://blog.csdn.net/wenbingy/article/details/139617671  浏览:    关键词:从零开始使用 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 安装、配置和执行基本的增删查改操作。希望这篇文章能帮助你搭建起自己的全文搜索引擎。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词