1.安装
(1)必须:在/etc/yum.repos.d/中,创建文件elasticsearch.repo
vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch]
name=Elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md
(2)必须:然后用如下命令来安装(注意记录生成的password)
yum install --enablerepo=elasticsearch elasticsearch
(3)可选:安装后,修改配置文件
vim /etc/elasticsearch/elasticsearch.yml
2. 启动和关闭
- 前台启动:
cd /usr/share/elasticsearch/
./bin/elasticsearch
- 后台启动:
./bin/elasticsearch -d
- 查看是否启动成功
curl --user elastic:some_password 127.0.0.1:9200
- 查看端口是否启动
lsof -i:9200
3. 安装分词插件
- 查看有哪些插件
bin/elasticsearch-plugin list
- 在线安装分词插件analysis-icu
bin/elasticsearch-plugin install analysis-icu
- 安装中文分词插件ik
从https://github.com/medcl/elasticsearch-analysis-ik/releases下载对应版本的ik插件
创建ik插件文件夹cd your-es-root/plugins/ && mkdir ik
解压your-es-root/plugins/ik
可在git查看版本,因为elasticsearch是最新版 8.5.3,但是ik目前最新版是8.5.2,参考教程,找到ik分词器的plugin-descriptor.properties中的elasticsearch.version, 改成自己es版本的版本号
4. python操作ElasticSearch
4.1 创建索引
- 安装:
pip install elasticsearch
from elasticsearch import Elasticsearch
es = Elasticsearch("http://localhost:9200", basic_auth=("elastic", SOME_PASSWORD))
print(es.info())
# 参看现有索引
print(es.cat.indices().raw)
# 为了给索引设置一些属性
# 这里mappings, settings是dict,包含属性字段,详情可见https://blog.csdn.net/YiLiuF/article/details/123310248
mappings = {
'properties': {
"author": {
"type": "text",
"store": True
},
"text": {
"type": "text",
"store": True,
"index": True,
"analyzer": "ik_smart"
},
}
}
settings = {
"index": {
"number_of_shards": "5",
"number_of_replicas": "1"
}
}
es.indices.create(index='test_index', mappings=mappings, settings=settings)
# 参考https://www.elastic.co/guide/en/elasticsearch/client/python-api/8.5/examples.html
doc = {
'author': 'author_name',
'text': 'Interensting content...',
}
resp = es.index(index="test-index", id=1, document=doc)
# 查看索引的文档数量
print(es.indices.stats(index="test-index"))
4.2 查询索引
# 分词效果查看
es.indices.analyze(analyzer='ik_smart',text='我爱你水杯')
附:遇到的报错修改
1) insufficient memory for the Java
修改elastcisearch配置文件夹的jvm.options,详见参考
vim /etc/elasticsearch/jvm.options
-Xms1g
-Xmx1g
2) can not run elasticsearch as root
创建用户,用这个用户来运行:chown -R username:username some_folder
详情见参考
3)received plaintext http traffic on an https channel, closing connection Netty4HttpChannel
elasticsearch.yml 文件中把 xpack.security.http.ssl:enabled 设置成false即可,可见参考
参考
- https://www.elastic.co/guide/en/elasticsearch/reference/8.5/rpm.html#rpm-repo
- https://www.elastic.co/guide/en/elasticsearch/client/python-api/8.5/config.html
- https://baijiahao.baidu.com/s?id=1746831225183250864&wfr=spider&for=pc
- https://blog.csdn.net/YiLiuF/article/details/123310248
- https://blog.csdn.net/xionglangs/article/details/123789875