Elastic学习(一)

ElasticSearch介绍

ElasticSearch是目前较流行的全文搜索引擎,(ElasticSearch以下简称为ES)ES底层是Lucene。ElasticSearch是Lucene的封装;

ES提供了Rest API,开箱即用;

安装ES

  • 下载压缩包,解压压缩包

  • es压缩包版本为7.6,需要升级jdk为jdk11

  • es启动报错:内存不足;解决:虚拟机增加内存,或者修改配置文件conf下的某个配置文件中配置的最大、最小内存;

    cd /usr/local/soft/elasticsearch-7.6.1/config/
    vim jvm.options 
    
    # Xmx represents the maximum size of total heap space
    #默认是512m
    -Xms128m
    -Xmx128m
    
    #编辑
    vim /etc/sysctl.conf
    #添加一行
    vm.max_map_count=655360
    #保存后,
    sysctl -p
    #之后输出下边信息,表示修改生效
    vm.max_map_count = 655360
    
  • es不能用root权限启动,创建新用户,例如es,修改es文件夹的权限为es:es;

    以上操作的命令请自行百度;

  • 启动ES,进入bin目录,./elasticsearch 即可;

  • 关闭,如果是后台启动,kill 进程号关闭,如果不是后台启动,直接ctrl + c关闭;

启动

启动后,默认端口为9200,默认只允许本机访问es;

修改conf/elasticsearch.yml,可以更改端口号,设置network.host: 0.0.0.0,并重启,可以让任意机子访问es;

ES启动报错解决方式

  • 错误1:“max virtual memory areas vm.maxmapcount [65530] is too low”

    解决:切换到root 用户,输入sysctl -w vm.max_map_count=262144

  • 错误2:max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]

    解决:

    修改/etc/security/limits.d/def.conf文件,

    如果不存在文件,则自动创建文件,vim命令不存在指定文件,就会创建;

    修改如下:

    * soft nofile 204800
    * hard nofile 204800
    

    修改完毕后,重新登录用户;输入ulimit -Hn 和ulimit -Sn查看是否生效

  • 错误3:the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

    解决:

    错误说这三个至少要配置一个;[discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes]

    所以咱就去配置一下,切换到es目录,

    cd /usr/local/soft/elasticsearch-7.6.1/config/
    #修改配置文件
    vim elasticsearch.yml
    #找到如下配置,加入配置cluster.initial_master_nodes: ["node-1"]
    #cluster.initial_master_nodes: ["node-1", "node-2"]
    cluster.initial_master_nodes: ["node-1"]
    

    我这是单节点ES,加一个节点名字就好,默认名字就是node-1;

Elastic Search相关概念

node和cluster

ES本质上是一个分布式的数据库,允许多台服务器协同工作,每台服务器可以运行多个ES实例;

每个ES实例,成为一个node(节点) 。一组节点构成一个cluster(集群)

index

index名必须是小写的;

index相当于一张mysql中的表;

  • index相关命令:
#查看当前节点中的所有索引
[es@localhost ~]$ curl -X GET 'http://localhost:9200/_cat/indices?v'
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   weather XOz_dEPdSraZh5dSMHPnbQ   1   1          0            0       283b           283b
#新建索引
curl -X PUT 'http://localhost:9200/weather'
#删除
curl -X DELETE 'http://localhost:9200/weather'

设置分词器,安装ik中文分词器

安装方式1:

#通过命令方式下载;我没用这种 因为老卡在downling...
[es@localhost ik]$ ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.1/elasticsearch-analysis-ik-7.6.1.zip

安装方式2:

去网站下载,再解压

我es对应的ik版本是:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.6.1

#切换目录
cd /phoenix/elastic/elasticsearch-2.4.0/plugins/
#创建ik文件夹
mkdir ik
#切换到ik文件夹下进行文件上传
cd ik
#没有unzip命令的同学输入如下命令安装unzip命令
yum install -y unzip
#对zip进行解压
unzip elasticsearch-analysis-ik-1.10.0.zip

重启es,即可生效;

测试:

创建索引、设置type、并使用到分词器

创建一个索引,给这个索引创建一个type

curl -X PUT "localhost:9200/person?pretty" -H 'Content-Type: application/json' -d'
{  
   "mappings": {
    "person": {
      "properties": {
        "user": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "title": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "desc": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
  }
     
}
'

给user、title、desc这三个字段,分别指定了数据类型、字段文本的分词器、搜索词的分词器;

"title": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }

对文档的增删改查

es 7.x 版本;

操作在这了:https://www.jianshu.com/p/d262d68c44a2

ES的可视化工具

我只知道es-head,但是界面太丑了,还有一个是kibana,比head好看多了;

kibana安装:

https://www.elastic.co/cn/downloads/past-releases#kibana找和es对应的kibana的版本,下载下来;

解压后,修改config/kibana.yml 修改

vi kibana.yml

server.host: "192.168.157.181"
elasticsearch.hosts: "http://192.168.157.181:5601:9200"

启动:

进入bin,

cd bin
./kibana

如果一直警告:

 [17:22:09.563] [warning][savedobjects-service] Another Kibana instance appears to be migrating the index. Waiting for that migration to complete. If no other Kibana instance is attempting migrations, you can get past this message by deleting index .kibana_task_manager_1 and restarting Kibana.

解决:删除索引

curl -XDELETE http://localhost:9200/.kibana_task_manager_1
#localhost是你部署es的服务器ip

汉化,改配置文件语言设置en改为zh-CN,再重启;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值