elastic search8安装及使用

一、elasticsearch 安装

  1. 下载地址 :https://www.elastic.co/cn/start
  2. 下载 elasticsearch-8.0.0-linux-x86_64.tar.gz 完成后复制到虚拟机
  3. 安装java8环境
yum install -y java-1.8.0-openjdk
  1. 虚拟机配置
vim /etc/security/limits.conf
# 添加
* soft nofile 65536
* hard nofile 65536
# 保存,并重新登陆
vim /etc/sysctl.conf
# 最后一行添加
vm.max_map_count=262144
# 保存
sysctl -p
  1. 安装:注意不能使用root用户
tar -xvzf elasticsearch-8.0.0-linux-x86_64.tar.gz
# 修改配置 
cd elasticsearch-8.0.0
vim config/elasticsearch.yml
# 设置network.host的IP为0.0.0.0
# 设置node.name: node-1
# 设置cluster.initial_master_nodes: ["node-1"]
# 添加
xpack.reporting.encryptionKey: "chenqionghe"
xpack.security.encryptionKey: "122333444455555666666777777788888888"
xpack.encryptedSavedObjects.encryptionKey: "122333444455555666666777777788888888"
# 保存
# 启动
cd bin
./elasticsearch
  1. 查看
    访问 https://192.168.122.8:9200/ (使用htpps协议)
    用户名密码在启动后的提示信息中
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.

ℹ️  Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  密码
  1. 出现以下内容说明访问正常
{
  "name" : "node-1",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "err8U3ufRS-EiKUqWTh4wQ",
  "version" : {
    "number" : "8.0.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "1b6a7ece17463df5ff54a3e1302d825889aa1161",
    "build_date" : "2022-02-03T16:47:57.507843096Z",
    "build_snapshot" : false,
    "lucene_version" : "9.0.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

二、kibana 安装

  1. 下载地址 :https://www.elastic.co/cn/start
  2. 下载 kibana-8.0.0-linux-x86_64.tar.gz完成后复制到虚拟机
  3. 配置
  4. 安装:注意不能使用root用户
tar -xvzf kibana-8.0.0-linux-x86_64.tar.gz 
vim kibana-8.0.0/config/kibana.yml 
# 修改server.host: "0.0.0.0"
# 修改中文 i18n.locale: "zh-CN"
# 修改 server.publicBaseUrl: "http://localhost:5601/"
# 保存
cd kibana-8.0.0/bin/
# 启动
./kibana
  1. 复制日志中的链接,修改为虚拟机地址并访问
  2. 在elasticsearch的日志中复制token,填到页面中,并点击Configure manually
  3. 点击check address
  4. 生成用户密码
cd elasticsearch-8.0.0/bin/
./elasticsearch-reset-password --username kibana_system
y
  1. 将用户名密码输入,并勾选node-1 点击配置
  2. 使用elastic 的账号密码进行登陆

三、elastic search 配置中文分词器

  1. 分词器下载地址 https://github.com/medcl/elasticsearch-analysis-ik/releases
  2. 将压缩包复制到 elasticsearch-8.0.0/plugins文件夹下,并在该目录下解压缩
unzip -d ik-analyzer elasticsearch-analysis-ik-8.0.0.zip
  1. 删除压缩包,并重启elasticsearch

四、elastic使用

1.索引

  1. 创建索引
# 注 number_of_shards:分片数
# number_of_replicas:备份数
PUT 索引名
{
  "settings": {
    "index": {
      "number_of_shards": "5",
      "number_of_replicas": "1"
    }
  }
}
  1. 创建索引,并设置mapping
PUT blog1
{
  "mappings":{
      "properties":{
        "id":{
          "type":"long",
          "store":true
        },
        "title":{
          "type":"text",
          "store":true,
          "index":true,
          "analyzer":"standard"
        },
        "content":{
          "type":"text",
          "store":true,
          "index":true,
          "analyzer":"standard"
        }
      }
  }
}

  1. 查询指定索引信息
GET blog1
  1. 查询所有索引
# ?v 可以不加
GET _cat/indices?v
  1. 修改索引 - 设置mapping(只能新增)
POST blog/_mapping
{
  "properties":{
    "id":{
      "type":"long",
      "store": true
    },
    "title":{
      "type":"text",
      "store":true,
      "index": true,
      "analyzer":"standard"
    },
    "content":{
      "type":"text",
      "store":true,
      "index": true,
      "analyzer":"standard"
    }
  }
}
  1. 删除索引
DELETE index-hello

2.文档

  1. 创建文档
POST blog/_doc/1
{
  "id": 1,
  "title": "“开启,这些要点提前看",
  "content":"讨论哪些问题?一起了解"
}

或者

POST blog/_doc/
{
  "id": 100,
  "title": "你好",
  "content":"我是程序员间3月1日晚空。"
}

  1. 删除文档
DELETE blog/_doc/1
  1. 修改文档
    在es中修改文档就是将原文档删除,然后新增,这里只要使用添加方法将原文档覆盖即可
POST blog/_doc/1
{
  "id":1,
  "title":"你好来了",
  "content":"我在这条路上遇到你"
}
  1. 查询文档
    根据id查询
GET blog/_doc/1

查询所有

GET blog/_search

term查询

GET blog/_search
{
  "query": {
    "term": {
      "content": "在"
    }
  }
}

querystring查询
注:在查询时会对字符串进行分词,并进行分别查询

GET blog/_search
{
  "query": {
    "query_string": {
      "default_field": "title", 
      "query": "可惜"
    }
  }
}

3.分词效果查看

GET _analyze?pretty
{
  "analyzer": "standard",
  "text": ["我是程序员"] 
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值