美文网首页
elasticsearch安装

elasticsearch安装

作者: brightranger | 来源:发表于2021-07-27 11:07 被阅读0次

    下载安装包

    https://www.elastic.co/cn/downloads/past-releases/elasticsearch-6-3-1

    解压

    tar -zxvf elasticsearch-7.13.0-linux-x86_64.tar.gz
    

    修改配置文件(config/elasticsearch.yml)

    • 配置集群名
    image.png
    • 当前节点名: 每个节点的名字不能相同, 当分发到其他节点的时候, 需要改成不同的名字
    image.png
    • 给当前节点绑定 ip 地址, 端口号保持默认 9200 就行


      image.png
    • 关掉 bootstrap 自检程序


      image.png
    • 启动elasticSearch
    ./elasticsearch
    
    • 报错
    java.lang.RuntimeException: can not run elasticsearch as root
    

    elasticSearch无法使用root用户运行

    • 创建新的用户 es
    1. 创建组
    groupadd es
    
    1. 添加用户到组中
    useradd -m -g es elastic
    -g :指定组
    
    1. 修改用户名
    passwd elastic
    
    1. 移动文件并修改权限
    cd /home/elastic
    mv /usr/local/soft/elasticsearch/elasticsearch-7.13.0 ./
    chown -R elastic:es elasticsearch-7.13.0
    

    5、重新启动

    直接启动
    ./elasticsearch
    以后台方式启动
    ./elasticsearch -d
    

    6、本机访问

    [jht@localhost ~]$ curl localhost:9200
    {
      "name" : "node-one",
      "cluster_name" : "my-zhanghf",
      "cluster_uuid" : "j7siWKq1S5OwGKDTmxVLfg",
      "version" : {
        "number" : "7.13.0",
        "build_flavor" : "default",
        "build_type" : "tar",
        "build_hash" : "5ca8591c6fcdb1260ce95b08a8e023559635c6f3",
        "build_date" : "2021-05-19T22:22:26.081971330Z",
        "build_snapshot" : false,
        "lucene_version" : "8.8.2",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    

    7、配置外网可以访问

    elasticsearch.yml文件
    network.host: 0.0.0.0
    
    • 报错
    at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
    

    放开:

    cluster.initial_master_nodes: ["node-1", "node-2"]
    
    • 报错
    [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
    

    使用sudo用户修改文件:sudo vim /etc/sysctl.conf

    添加一行配置:

    vm.max_map_count=655360
    

    启动

    ./elasticsearch
    
    1. 访问
    浏览器访问: http://10.10.205.114:9200
    {
      "name" : "node-one",
      "cluster_name" : "my-zhanghf",
      "cluster_uuid" : "j7siWKq1S5OwGKDTmxVLfg",
      "version" : {
        "number" : "7.13.0",
        "build_flavor" : "default",
        "build_type" : "tar",
        "build_hash" : "5ca8591c6fcdb1260ce95b08a8e023559635c6f3",
        "build_date" : "2021-05-19T22:22:26.081971330Z",
        "build_snapshot" : false,
        "lucene_version" : "8.8.2",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    

    基本概念

    Node 与 Cluster

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

    单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

    Index

    Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。

    所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。

    下面的命令可以查看当前节点的所有 Index。
    相当于关系数据库的 数据库实例

    $ curl -X GET 'http://localhost:9200/_cat/indices?v'
    

    Document

    Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。
    Document 使用 JSON 格式表示,下面是一个例子。

    {
      "user": "张三",
      "title": "工程师",
      "desc": "数据库管理"
    }
    

    同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。
    相当于关系数据库的表

    Type

    Document 可以分组,比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。

    不同的 Type 应该有相似的结构(schema),举例来说,id字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。性质完全不同的数据(比如productslogs)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。

    下面的命令可以列出每个 Index 所包含的 Type。

    $ curl 'localhost:9200/_mapping?pretty=true'
    

    根据规划,Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移除 Type。

    Field

    简单操作使用

    新建和删除 Index

    新建 Index,可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫weather的 Index。

    $ curl -X PUT 'localhost:9200/weather'
    

    服务器返回一个 JSON 对象,里面的acknowledged字段表示操作成功。

    {
      "acknowledged":true,
      "shards_acknowledged":true
    }
    

    然后,我们发出 DELETE 请求,删除这个 Index。

    $ curl -X DELETE 'localhost:9200/weather'
    
    中文分词设置
    1. 下载中文分词器
    https://github.com/medcl/elasticsearch-analysis-ik/releases
    
    2.上传到插件目录
    /home/elastic/elasticsearch-7.13.0/plugins
    
    3.重启
    
    

    或者直接在线安装

    ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.14.0/elasticsearch-analysis-ik-7.14.0.zip
    

    直接写入数据(自动创建索引)

    curl -X PUT "localhost:9200/megacorp/employee/1?pretty" -H 'Content-Type: application/json' -d'
    {
        "first_name" : "John",
        "last_name" :  "Smith",
        "age" :        25,
        "about" :      "I love to go rock climbing",
        "interests": [ "sports", "music" ]
    }
    '
    
    查询
    [elastic@localhost bin]$ curl -X GET "localhost:9200/megacorp/employee/1?pretty"
    {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "first_name" : "John",
        "last_name" : "Smith",
        "age" : 25,
        "about" : "I love to go rock climbing",
        "interests" : [
          "sports",
          "music"
        ]
      }
    }
    
    查询所有雇员
    [elastic@localhost bin]$ curl -X GET "localhost:9200/megacorp/employee/_search?pretty"
    {
      "took" : 74,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 3,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            }
          },
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          },
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "3",
            "_score" : 1.0,
            "_source" : {
              "first_name" : "Douglas",
              "last_name" : "Fir",
              "age" : 35,
              "about" : "I like to build cabinets",
              "interests" : [
                "forestry"
              ]
            }
          }
        ]
      }
    }
    
    

    条件搜索

    [elastic@localhost bin]$ curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"
    
    {
      "took" : 31,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 0.4700036,
        "hits" : [
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 0.4700036,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            }
          },
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.4700036,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          }
        ]
      }
    }
    
    

    Elasticsearch 提供一个丰富灵活的查询语言叫做 查询表达式 , 它支持构建更加复杂和健壮的查询。
    领域特定语言 (DSL), 使用 JSON 构造了一个请求。我们可以像这样重写之前的查询所有名为 Smith 的搜索 :

    curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'
    {
        "query" : {
            "match" : {
                "last_name" : "Smith"
            }
        }
    }
    '
    
    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 0.4700036,
        "hits" : [
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 0.4700036,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            }
          },
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.4700036,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          }
        ]
      }
    }
    

    现在尝试下更复杂的搜索。 同样搜索姓氏为 Smith 的员工,但这次我们只需要年龄大于 30 的。查询需要稍作调整,使用过滤器 filter ,它支持高效地执行一个结构化查询。

    curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'
    {
        "query" : {
            "bool": {
                "must": {
                    "match" : {
                        "last_name" : "smith" 
                    }
                },
                "filter": {
                    "range" : {
                        "age" : { "gt" : 30 } 
                    }
                }
            }
        }
    }
    '
    
    {
      "took" : 28,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.4700036,
        "hits" : [
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.4700036,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          }
        ]
      }
    }
    
    

    全文搜索

    截止目前的搜索相对都很简单:单个姓名,通过年龄过滤。现在尝试下稍微高级点儿的全文搜索——一项 传统数据库确实很难搞定的任务。

    搜索下所有喜欢攀岩(rock climbing)的员工:

    curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'
    {
        "query" : {
            "match" : {
                "about" : "rock climbing"
            }
        }
    }
    '
    
    {
      "took" : 15,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 1.4167401,
        "hits" : [
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 1.4167401,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            }
          },
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "2",
            "_score" : 0.4589591,
            "_source" : {
              "first_name" : "Jane",
              "last_name" : "Smith",
              "age" : 32,
              "about" : "I like to collect rock albums",
              "interests" : [
                "music"
              ]
            }
          }
        ]
      }
    }
    
    

    Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。第一个最高得分的结果很明显:John Smith 的 about 属性清楚地写着 “rock climbing” 。

    但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about 属性里提到了 “rock” 。因为只有 “rock” 而没有 “climbing” ,所以她的相关性得分低于 John 的。

    这是一个很好的案例,阐明了 Elasticsearch 如何 在 全文属性上搜索并返回相关性最强的结果。Elasticsearch中的 相关性 概念非常重要,也是完全区别于传统关系型数据库的一个概念,数据库中的一条记录要么匹配要么不匹配。

    短语搜索

    找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 。 比如, 我们想执行这样一个查询,仅匹配同时包含 “rock” 和 “climbing” ,并且 二者以短语 “rock climbing” 的形式紧挨着的雇员记录。

    为此对 match 查询稍作调整,使用一个叫做 match_phrase 的查询:

    curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'
    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        }
    }
    '
    
    {
      "took" : 14,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.4167401,
        "hits" : [
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 1.4167401,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            }
          }
        ]
      }
    }
    

    高度搜索

    许多应用都倾向于在每个搜索结果中 高亮 部分文本片段,以便让用户知道为何该文档符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。

    再次执行前面的查询,并增加一个新的 highlight 参数:

    curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'
    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        },
        "highlight": {
            "fields" : {
                "about" : {}
            }
        }
    }
    '
    
    {
      "took" : 67,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.4167401,
        "hits" : [
          {
            "_index" : "megacorp",
            "_type" : "employee",
            "_id" : "1",
            "_score" : 1.4167401,
            "_source" : {
              "first_name" : "John",
              "last_name" : "Smith",
              "age" : 25,
              "about" : "I love to go rock climbing",
              "interests" : [
                "sports",
                "music"
              ]
            },
            "highlight" : {
              "about" : [
                "I love to go <em>rock</em> <em>climbing</em>"
              ]
            }
          }
        ]
      }
    }
    
    

    当执行该查询时,返回结果与之前一样,与此同时结果中还多了一个叫做 highlight 的部分。这个部分包含了 about 属性匹配的文本片段,并以 HTML 标签 <em></em> 封装:

    分析统计查询

    终于到了最后一个业务需求:支持管理者对员工目录做分析。 Elasticsearch 有一个功能叫聚合(aggregations),允许我们基于数据生成一些精细的分析结果。聚合与 SQL 中的 GROUP BY 类似但更强大。

    举个例子,挖掘出员工中最受欢迎的兴趣爱好:

    相关文章

      网友评论

          本文标题:elasticsearch安装

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