ElasticSearch入门
Elasticsearch简介
- 一个分布式的、Restful风格的搜索引擎。
- 支持对各种类型的数据的检索(非结构化的也可以)。
- 搜索速度快,可以提供实时的搜索服务。
- 便于水平扩展(集群式部署),每秒可以处理PB级海量数据。
Elasticsearch术语
- 索引(数据库,6.0后对应表)、类型(表)、文档(行)、字段(列)。
- 集群、节点、分片、副本。
安装es服务器
docker部署见https://git.lug.ustc.edu.cn/Iris666/elastic-kg/-/tree/main?ref_type=heads
先用docker部署,不行再直接安装
为了简单,还是直接安装了ES,就是解压压缩包,
打开config/elasticsearch.yml文件改配置:
cluster.name: nowcoder
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /Users/iris/items/elasticsearch-8.13.2/data
#
# Path to log files:
#
path.logs: /Users/iris/items/elasticsearch-8.13.2/logs
然后把二进制程序添加到环境变量
vim ~/.bash_profile
export PATH=$PATH:/path/to/elasticsearch/bin
source ~/.bash_profile
再mac上直接运行es会报错,说jdk来路不明,方法是暂时关闭检查,用下面的命令:
sudo spctl --master-disable
为了安全用完后再打开:
sudo spctl --master-enable
安装中文分词插件
bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/8.13.2
(docker版本exec进container里面装插件)
版本要和es的版本严格对应。不然报错,之后会将插件存储在es/plugins路径下
使用postman发送HTTP请求
注册账号模拟发送HTTP请求
通过命令行访问es
在命令行中键入:
curl -X GET "http://localhost:9200/_cluster/settings?pretty"
查看状态,但是报错empty,原因是es默认SSL开的,所以http过不去,解决方法是在config中将:
xpack.security.enabled: false
运行结果如下:
{
"error" : {
"root_cause" : [
{
"type" : "security_exception",
"reason" : "missing authentication credentials for REST request [/_cluster/settings?pretty]",
"header" : {
"WWW-Authenticate" : [
"Basic realm=\"security\" charset=\"UTF-8\"",
"ApiKey"
]
}
}
],
"type" : "security_exception",
"reason" : "missing authentication credentials for REST request [/_cluster/settings?pretty]",
"header" : {
"WWW-Authenticate" : [
"Basic realm=\"security\" charset=\"UTF-8\"",
"ApiKey"
]
}
},
"status" : 401
}
接着报错,原因是curl的时候要-u传入用户名和密码,但是之前的已经忘了,重新创建个用户:
./elasticsearch-users useradd your_username -p your_password -r superuser
curl -u ***:password -X GET "http://localhost:9200/_cluster/settings?pretty"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open average_annual_wage kqix1Pp7SiaUDtwWYNnELQ 1 1 785 0 56.8kb 56.8kb
green open .monitoring-es-7-2024.03.26 JtNGXpQYSvqBsClZtT8jdw 1 0 61371 0 24.7mb 24.7mb
green open .monitoring-es-7-2024.03.25 bHqBijFKQPy-S8aIXz_NQw 1 0 39185 0 16.2mb 16.2mb
green open .monitoring-kibana-7-2024.03.26 3vMGC8z5TJibwdTi1s0yOA 1 0 8178 0 1.7mb 1.7mb
yellow open jobsearch 9ekhjB0bQ4m3KKai8WmpFw 2 1 10661 0 161.4mb 161.4mb
green open .monitoring-kibana-7-2024.03.25 uY_wWKlGR1KgWdbUgSjHfw 1 0 6698 0 1.5mb 1.5mb
green open .monitoring-logstash-7-2024.03.25 VUJRgqRlSx-pDUr84z-QkA 1 0 39399 0 2mb 2mb
green open .monitoring-kibana-7-2024.03.27 sbMuIju9STCrPVuYLiQHzQ 1 0 230 0 126.4kb 126.4kb
green open .monitoring-kibana-7<