Centos8下ElasticSearch及ElasticSearch-head的安装与配置

与关系型数据库对应关系

Relational DBElasticsearch
数据库(database)索引 index
表(tables)索引 index(原为 types)
行(rows)文档 documents
字段(columns)fields
约束(schema)映射mapping

RPM方式安装Elasticsearch

下载地址
在这里插入图片描述

安装

rpm -Uvh elasticsearch-7.12.0-x86_64.rpm

在这里插入图片描述

启动服务

systemctl enable elasticsearch

systemctl start elasticsearch
#查看es状态
systemctl status elasticsearch

在这里插入图片描述

测试服务

如果是elasticsearch8.+

vim /etc/elasticsearch/elasticsearch.yml

xpack.security.enabled默认为true,此时改成false

xpack.security.enabled: false

在这里插入图片描述
保存后重启

curl "http://localhost:9200"

在这里插入图片描述

跨域访问设置

使用图形工具head连接时需要添加增加跨域访问参数

vim /etc/elasticsearch/elasticsearch.yml

添加及修改以下配置

http.host: 0.0.0.0

network.host: 0.0.0.0

discovery.seed_hosts: ["0.0.0.0", "[::1]"]

#启用跨域支持
http.cors.enabled: true
http.cors.allow-origin: "*"

在这里插入图片描述
重启服务

systemctl restart elasticsearch

现在可以实现远程连接了
在这里插入图片描述

安装nodejs环境

下载地址
在这里插入图片描述

下载并安装

wget https://nodejs.org/dist/v14.16.1/node-v14.16.1-linux-x64.tar.xz

在这里插入图片描述
解压到指定目录

tar -xvf node-v14.16.1-linux-x64.tar.xz

mv ./node-v14.16.1-linux-x64 /usr/local/nodejs

配置npm和node命令全局生效

ln -s /usr/local/nodejs/bin/npm /usr/local/bin/
ln -s /usr/local/nodejs/bin/node /usr/local/bin/

查看nodejs是否安装成功

node -v
npm -v

在这里插入图片描述

安装图形客户端elasticsearch-head

GitHub托管地址

克隆项目到本地

git clone https://github.com/mobz/elasticsearch-head.git

在这里插入图片描述

修改license参数

进入根目录

cd elasticsearch-head/
vim package.json
"license": "Apache-2.0"

在这里插入图片描述

安装

npm install

警告信息可以忽略
在这里插入图片描述
前台运行

npm run start

在这里插入图片描述
后台运行(推荐)

nohup npm run start &

在这里插入图片描述

访问

默认端口为9100

http://ip地址:9100

修改服务地址点击连接,如下状态代表连接成功
在这里插入图片描述

安装分词器

GitHub地址

在这里插入图片描述

Releases地址

在这里插入图片描述

下载对应的版本

wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip

在这里插入图片描述
解压分词器到创建的目录中

mkdir /usr/share/elasticsearch/plugins/ik

unzip -o elasticsearch-analysis-ik-7.12.0.zip -d /usr/share/elasticsearch/plugins/ik

在这里插入图片描述
查看文件

ll /usr/share/elasticsearch/plugins/ik

在这里插入图片描述
重启服务

systemctl restart elasticsearch

搜索测试

创建索引

index是索引名,可自定义

curl -XPUT http://localhost:9200/index

创建mapping

index为指定的索引名

curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            }
        }

}'

官方对于ik_max_word 和 ik_smart 的解释
在这里插入图片描述

插入测试数据

curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d'{"content":"美国留给伊拉克的是个烂摊子吗"}'
curl -XPOST http://localhost:9200/index/_create/2 -H 'Content-Type:application/json' -d'{"content":"公安部:各地校车将享最高路权"}'
curl -XPOST http://localhost:9200/index/_create/3 -H 'Content-Type:application/json' -d'{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'
curl -XPOST http://localhost:9200/index/_create/4 -H 'Content-Type:application/json' -d'{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'

验证分词查询

以“中国领事馆”为关键字

curl -XPOST http://localhost:9200/index/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国领事馆" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}'

在这里插入图片描述

使用图形客户端搜索

{
  "query": {
    "match": {
      "content": "中国领事馆"
    }
  },
  "highlight": {
    "pre_tags": [
      "<tag1>",
      "<tag2>"
    ],
    "post_tags": [
      "</tag1>",
      "</tag2>"
    ],
    "fields": {
      "content": {}
    }
  }
}

红框中为分词器匹配到的数据
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值