做网站 淘宝,烟台网站建设seo,企业查询app排行榜,广告流量投放Elasticsearch 是目前最常用的全文搜索引擎。它可以快速地存储、搜索和分析海量数据#xff0c;广泛应用于维基百科、Stack Overflow、Github 等网站。
Elasticsearch 的底层是开源库 Lucene。直接使用 Lucene 需要写大量代码#xff0c;而 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_count262144正常启动后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?v2.3 Document
Index 中的单条记录称为 Document文档。Document 使用 JSON 格式表示例如
{user: 张三,title: 工程师,desc: 数据库管理
}同一个 Index 里的 Document 结构不要求完全一致但最好保持相同以提高搜索效率。
2.4 Type
Document 可以分组这种分组称为 Type。Type 是逻辑分组用来过滤 Document。不同的 Type 应该有相似的结构schema。Elasticsearch 6.x 版本后每个 Index 只允许包含一个 Type7.x 版本彻底移除 Type。
列出每个 Index 包含的 Type
$ curl localhost:9200/_mapping?prettytrue三、新建和删除 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其中有三个字段 user、title 和 desc 都需要使用中文分词器 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?prettytrue返回的数据中found 字段表示查询成功_source 字段返回原始记录
{_index : accounts,_id : 1,_version : 1,found : true,_source : {user : 张三,title : 工程师,desc : 数据库管理}
}5.3 删除记录
删除记录
$ curl -X DELETE localhost:9200/accounts/_doc/15.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 安装、配置和执行基本的增删查改操作。希望这篇文章能帮助你搭建起自己的全文搜索引擎。