# Elasticsearch

# 一、安装

# 1.1 Docker 安装

# 下载镜像

docker pull kibana:7.12.1
docker pull elasticsearch:7.12.1
1
2

# 配置网络

【es 搜索数据库】和kibana 【可视化工具】容器互联!

docker network create es-net
1

# 单点运行kiba

docker run -d \
  --name es \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -e "discovery.type=single-node" \
    -v es-data:/usr/share/elasticsearch/data \
    -v es-plugins:/usr/share/elasticsearch/plugins \
    --privileged \
    --network es-net \
    -p 9200:9200 \
    -p 9300:9300 \
elasticsearch:7.12.1
1
2
3
4
5
6
7
8
9
10
11

在浏览器中输入:http://ip地址:9200 即可看到elasticsearch的响应结果!

# 单点运行kibana! 提供数据可视化!

docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network=es-net \
-p 5601:5601  \
kibana:7.12.1
1
2
3
4
5
6

此时,在浏览器输入地址访问:http://ip地址:5601,即可看到结果! 打开DevTools,进行es库操作!

# 配置ik中文分词器

GitHub地址:elasticsearch-analysis-ik (opens new window)

[root@docker ~]# docker volume inspect es-plugins
[
    {
        "CreatedAt": "2022-12-07T16:03:43+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/es-plugins/_data",
        "Name": "es-plugins",
        "Options": null,
        "Scope": "local"
    }
]
# 将ik解压,导入到_data下
[root@docker ~]# cd /var/lib/docker/volumes/es-plugins/_data
[root@docker _data]# mkdir ik
[root@docker _data]# cd ik
[root@docker ik]# unzip ~/elasticsearch-analysis-ik-7.12.1.zip

docker restart es 即可!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

分词测试

POST /_analyze
{
  "analyzer": "standard",
  "text": "今天天气太好了!very good!"
}

//使用ik分词器,进行中文分词!

POST /_analyze
{
  "analyzer": "ik_smart",
  "text": "今天天气太好了!very good!"
}

POST /_analyze
{
  "analyzer": "ik_max_word",
  "text": "今天天气太好了!very good!"
}

//执行下下面这句话!创建下结构
# 创建商品索引!
# 根据多列搜索性能较差,组成成一列搜索提高性能
PUT /product
{
  "mappings": {
    "properties": {
      "productId":{
        "type": "integer"
      },
      "productName":{
        "type": "text",
        "analyzer": "ik_smart",
        "copy_to": "all"
      },
      "categoryId":{
        "type": "integer"
      },
      "productTitle":{
        "type": "text",
        "analyzer": "ik_smart",
        "copy_to": "all"
      },
      "productIntro":{
        "type":"text",
        "analyzer": "ik_smart",
        "copy_to": "all"
      },
      "productPicture":{
        "type": "keyword",
        "index": false
      },
      "productPrice":{
        "type": "double",
        "index": true
      },
      "productSellingPrice":{
        "type": "double"
      },
      "productNum":{
        "type": "integer"
      },
      "productSales":{
        "type": "integer"
      },
      "all":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

# 1.2 单机安装

Elasticsearch 官方文档 (opens new window)

# 下载安装包
wget https://s3-gzpu.didistatic.com/pub/elasticsearch.tar.gz
[root@node01 opt]# tar -xzf elasticsearch-7.6.tar.gz

# 创建ES所属用户
useradd arius

# 配置用户的打开文件数
echo "arius soft nofile 655350" >> /etc/security/limits.conf
echo "arius hard nofile 655350" >> /etc/security/limits.conf
echo "vm.max_map_count = 655360" >> /etc/sysctl.conf
sysctl -p

# 更改目录所属组
[root@node01 opt]# chown arius:arius elasticsearch

# 修改配置文件(参考以下配置)
vim /opt/elasticsearch/config/elasticsearch.yml
    cluster.name: km_es
    node.name: es-node1
    node.master: true
    node.data: true
    path.data: /data/es_data
    http.port: 8060
    discovery.seed_hosts: ["127.0.0.1:9300"]

# 修改内存配置
vim /opt/elasticsearch/config/jvm.options
    -Xms2g
    -Xmx2g

# 启动服务
su - arius
export JAVA_HOME=/usr/local/java11
sh /opt/elasticsearch/control.sh start

# 确认状态
sh /opt/elasticsearch/control.sh status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
更新时间: 7/7/2023, 11:32:03 AM