ElasticSearch 实现分词全文检索 - ES、Kibana、IK分词器安装

目录

ElasticSearch 实现分词全文检索 - 概述
ElasticSearch 实现分词全文检索 - ES、Kibana、IK安装
ElasticSearch 实现分词全文检索 - Restful基本操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - 测试数据准备
ElasticSearch 实现分词全文检索 - term、terms查询
ElasticSearch 实现分词全文检索 - match、match_all、multimatch查询
ElasticSearch 实现分词全文检索 - id、ids、prefix、fuzzy、wildcard、range、regexp 查询
ElasticSearch 实现分词全文检索 - Scroll 深分页
ElasticSearch 实现分词全文检索 - delete-by-query
ElasticSearch 实现分词全文检索 - 复合查询
ElasticSearch 实现分词全文检索 - filter查询
ElasticSearch 实现分词全文检索 - 高亮查询
ElasticSearch 实现分词全文检索 - 聚合查询 cardinality
ElasticSearch 实现分词全文检索 - 经纬度查询
ElasticSearch 实现分词全文检索 - 搜素关键字自动补全(suggest)
ElasticSearch 实现分词全文检索 - SpringBoot 完整实现 Demo 附源码

安装 ElasticSearch

Docker ElasticSearch

详见 Docker 安装 Elasticsearch、Kibana

Docker Compose 安装

或者 使用 docker compose 安装

[root@localhost ~]# docker-compose version
bash: docker-compose: 未找到命令...

docker compose 下载地址:https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64

将 docker-compose-Linux-x86_64 传到 /usr/local/bin 目录下,并改名为 docker-compose
image

[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose version
Docker Compose version v2.16.0

Docker Compose ElasticSearch

在目录 /opt/elasticsearch (目录自定义)下创建 docker-compose.yml 文件内容如下

version: "7.9.3"
services:
  elasticsearch:
    image: elasticsearch:7.9.3
    restart: always
    container_name: elasticsearch
    ports:
      - 9200:9200
    volumes:
      - /opt/elasticsearch/logs:/usr/share/elasticsearch/logs
  kibana:
    image: kibana:7.9.3
    restart: always
    container_name: kibana
    ports:
      - 5601:5601
    environment:
      - elasticsearch url=http://172.16.3.88:9200
      - I18N_LOCALE=zh-CN
    depends_on:
      - elasticsearch

执行命令

[root@localhost ~]# cd /opt/elasticsearch
[root@localhost elasticsearch]# pwd
/opt/elasticsearch
#安装
[root@localhost elasticsearch]# docker-compose up -d
[+] Running 0/24
 ⠇ elasticsearch Pulling                                                                                    42.8s
   ⠇ 3c72a8ed6814 Downloading [=================================>                 ]   5...                  26.9s
   ⠇ e0812c7c6d71 Download complete                                                                         26.9s
   ⠇ ae1d3d20a65c Download complete                                                                         26.9s
   ⠇ f38df76a58b2 Downloading [====>                                              ]  28...                  26.9s
   ⠇ 2eca3bea9f3c Download complete                                                                         26.9s
   ⠇ 8d0e26c84dc9 Download complete                                                                         26.9s
   ⠇ 739cff10e6d4 Download complete                                                                         26.9s
   ⠇ ebb4b2f0826b Download complete                                                                         26.9s
   ⠇ a93ad4ceb967 Download complete                                                                         26.9s
 ⠧ kibana Pulling                                                                                           42.8s
   ⠧ 55b131d31acb Downloading [======================================>            ]  17...                  26.7s
   ⠧ b8d3f5b80820 Waiting                                                                                   26.7s
   ⠧ ce01809aac54 Waiting                                                                                   26.7s
   ⠧ cd62ae8b2560 Waiting                                                                                   26.7s
   ⠧ 3f1b4e913944 Waiting                                                                                   26.7s
   ⠧ a14f0f39a444 Waiting                                                                                   26.7s
   ⠧ 6b9eac14d916 Waiting                                                                                   26.7s
   ⠧ 7d309d052637 Waiting                                                                                   26.7s
   ⠧ b35a5bce98d2 Waiting                                                                                   26.7s
   ⠧ d57619633b37 Waiting                                                                                   26.7s
   ⠧ 97c6a30a6480 Waiting                                                                                   26.7s
   ⠧ 1bff97a8e672 Waiting                                                                                   26.7s
   ⠧ 5ffe1970589a Waiting                                                                                   26.7
#查看日志
[root@localhost elasticsearch]# docker-compose logs -f

安装 IK Analysis

ES 默认分词器,对汉字的分词不太友好

下载 ik 分词器插件

找到对应的 Elasticsearch 版本,这里用的:7.9.3,将地址复制到讯雷里下,直接下的话下不动
https://elasticsearch.cn/download/
image
image

方式一:在容器中下载安装

先把zip下载下来。放到任意一台服务器(直接github上下载多数会失败)

# 查看 ES 的容器ID
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                                                                  NAMES
ad684997e228   elasticsearch:7.9.3   "/tini -- /usr/local…"   7 minutes ago   Up 7 minutes   0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch
# 进入 ES 容器
[root@localhost ~]# docker exec -it ad68 /bin/bash
# 通过命令进行 ElasticSearch 插件安装,不需要重启
[root@ad684997e228 elasticsearch]# elasticsearch-plugin install http://172.16.0.183:8899/Java/elasticsearch-analysis-ik-7.9.3.zip
-> Downloading http://172.16.0.183:8899/Java/elasticsearch-analysis-ik-7.9.3.zip
[=================================================] 100%?? 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik
[root@ad684997e228 elasticsearch]#

方式二:将文件上传至容器,进行安装

将 ik 文件传至服务器

image

将 ik 复制到容器,并安装
# 查看 ES 的容器ID
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                                                                  NAMES
ad684997e228   elasticsearch:7.9.3   "/tini -- /usr/local…"   7 minutes ago   Up 7 minutes   0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   elasticsearch
# 将it 插件,复制到容器中
[root@localhost ~]# docker cp /opt/elasticsearch-analysis-ik-7.9.3.zip ad684997e228:/usr/share/elasticsearch
# 进入 ES 容器
[root@localhost ~]# docker exec -it ad68 /bin/bash
# 通过命令进行 ElasticSearch 插件安装,不需要重启
[root@ad684997e228 elasticsearch]# elasticsearch-plugin install file:\/usr/share/elasticsearch/elasticsearch-analysis-ik-7.9.3.zip
-> Installing file:/usr/share/elasticsearch/elasticsearch-analysis-ik-7.9.3.zip
-> Downloading file:/usr/share/elasticsearch/elasticsearch-analysis-ik-7.9.3.zip
[=================================================] 100%?? 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik
[root@ad684997e228 elasticsearch]#

重启容器

docker restart xxx

如果不重启会报:failed to find global analyzer [ik_smart]

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "failed to find global analyzer [ik_smart]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "failed to find global analyzer [ik_smart]"
    },
    "status": 400
}

接口测试(Postman)

http://172.16.3.88:9200/_analyze

{
    "analyzer":"ik_smart",
    "text":"心脏搭桥手术"
}

image

Kibana 测试

image

POST _analyze
{
    "analyzer":"ik_smart",
    "text":"心脏搭桥手术"
}

image

posted @ 2023-03-03 16:47  VipSoft  阅读(998)  评论(0编辑  收藏  举报