美文网首页
01-docker安装ElasticSearch7

01-docker安装ElasticSearch7

作者: wshsdm | 来源:发表于2023-03-16 09:29 被阅读0次

    1 安装ES7

    1.1 设置jvm线程数限制

    #修改sysctl.conf
    vi /etc/sysctl.conf
    #修改max_map_count调大,如果没有这个设置,则新增一行
    vm.max_map_count=262144
    #改完保存后, 执行下面命令让sysctl.conf文件生效
    sysctl -p
    

    1.2 创建挂载目录

    • 为了防止容器删掉数据丢失,需要进行数据文件挂载
    #创建es配置目录
    mkdir /home/es/config -p
    #创建es数据目录
    mkdir /home/es/data 
    #创建es插件目录
    mkdir /home/es/plugins
    #授权目录
    chmod -R 777 /home/es
    
    • /home/es/config目录下创建配置文件 elasticsearch.yml
      注意:配置冒号后面都有一个空格
    • 单机配置文件内容:
    network.host: 0.0.0.0
    network.publish_host: 192.168.101.157
    http.port: 9200
    transport.tcp.port: 9300
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    
    # ======================== Elasticsearch Configuration =========================
    # 配置es的集群名称,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群
    cluster.name: elasticsearch
    # 节点名称
    node.name: node-1
    # 指定该节点是否有资格被选举成为node
    node.master: true
    # 指定初始主节点
    cluster.initial_master_nodes: ["172.16.2.84:9300"]
    # 指定该节点是否存储索引数据,默认为true
    node.data: true
    # 设置绑定的ip地址还有其他节点和该节点交换的ip地址,本机ip
    network.host: 0.0.0.0
    network.publish_host: 172.16.2.84
    # 指定http端口
    http.port: 9200
    # 设置节点间交互的tcp端口,默认是9300
    transport.tcp.port: 9300
    # 设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点
    discovery.zen.ping.unicast.hosts: ["172.16.2.84:9300","172.16.2.84:9300","172.16.2.85:9300"]
    # 如果要使用head,那么需要解决跨域问题,使head插件可以访问es
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    
    • 集群方式其他节点/home/es/config目录下创建配置文件 elasticsearch.yml
    # ======================== Elasticsearch Configuration =========================
    # 配置es的集群名称,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群
    cluster.name: elasticsearch
    # 节点名称
    node.name: node-2
    # 指定该节点是否有资格被选举成为node
    node.master: true
    # 指定初始主节点
    cluster.initial_master_nodes: ["172.16.2.84:9300"]
    # 指定该节点是否存储索引数据,默认为true
    node.data: true
    # 设置绑定的ip地址还有其他节点和该节点交换的ip地址,本机ip
    network.host: 0.0.0.0
    network.publish_host: 172.16.2.85
    # 指定http端口
    http.port: 9200
    # 设置节点间交互的tcp端口,默认是9300
    transport.tcp.port: 9300
    # 设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点
    discovery.zen.ping.unicast.hosts: ["172.16.2.84:9300","172.16.2.85:9300","172.16.2.85:9300"]
    # 如果要使用head,那么需要解决跨域问题,使head插件可以访问es
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    

    1.3 创建ES容器

    1. 拉取镜像
    docker pull elasticsearch:7.16.3
    
    1. 启动容器
    • 单机启动
    docker run --restart=always -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -d --name es7.16.3 -p 9200:9200 -p 9300:9300 -v /home/es/data:/usr/share/elasticsearch/data -v /home/es/plugins:/usr/share/elasticsearch/plugins -v /home/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch:7.16.3
    # 参数: 
    -e ES_JAVA_OPTS="-Xms512m -Xmx512m"  因为测试机器,把内存设为512m
    -e "discovery.type=single-node"  单机启动
    -d 后台运行
    --name 给启动的容器起名称
    -p 9200:9200 -p 9300:9300 映射docker的端口,9200是http的端口,9300是内部通讯的端口
    -v 目录挂载
    elasticsearch:7.16.3  es镜像名称,用这个镜像启动为容器
    

    2 安装kibana

    2.1 拉取镜像

    docker pull kibana:7.16.3
    

    2.2 创建并配置文件

    mkdir -p /home/kibana/config
    vi kibana.yml
    
    • kibana.yml内容
    server.name: kibana
    server.host: "0"
    elasticsearch.hosts: ["http://IP:9200"]
    xpack.monitoring.ui.container.elasticsearch.enabled: true
    monitoring.ui.container.elasticsearch.enabled: true
    

    2.3 启动kibana容器

    docker run --restart=always --name kibana716 -v /home/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5601:5601 -d kibana:7.16.3
    

    3 使用kibana的Dev tools简单操作

    3.1 创建索引

    建立搜索数据集的对象,即建立索引

    PUT /hotol
    {
      "mappings":{
        "properties":{
          "title":{
            "type":"text"
          },
          "city":{
            "type":"keyword"
          },
          "price":{
            "type":"double"
          }
        }
      }
    }
    

    3.2 查询索引

    GET /_cat/indices
    

    3.3 删除指定索引

    DELETE 索引名
    

    3.4 写入文档

    在索引中填充一些数据,创建了一条ID为001的文档

    POST /hotol/_doc/001
    {
      "title":"大富豪酒店",
      "city":"长春",
      "price":680.0
    }
    

    3.5 根据_id搜索文档

    GET /hotol/_doc/001
    

    3.6 根据一般字段搜索文档

    query子句可以按照需求填充查询项。假设按照城市进行搜索,把酒店文档搜索出来。因为只需要进行文本是否相等的判断,所以需要用到term搜索

    GET /hotol/_search
    {
      "query": {
        "term": {
          "city": {
            "value": "长春"
          }
        }
      }
    }
    
    • 查询结果
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {   // 命中文档总数
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.2876821, //命中文档最高得分
        "hits" : [
          {
            "_index" : "hotol",     //命中文档所在索引
            "_type" : "_doc",
            "_id" : "001",              //命中文档ID
            "_score" : 0.2876821, //命中文档分值
            "_source" : {     //命中文档内容
              "title" : "大富豪酒店",
              "city" : "长春",
              "price" : 680.0
            }
          }
        ]
      }
    }
    

    3.7 对某个字段进行模糊匹配

    GET /hotol/_search
    {
      "query": {
        "match": {
          "title": "富"
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:01-docker安装ElasticSearch7

          本文链接:https://www.haomeiwen.com/subject/iwbrrdtx.html