Elasticsearch(一)安装与简单使用


安装OpenJDK15

下载

https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/15/jdk/x64/windows/OpenJDK15U-jdk_x64_windows_hotspot_15_36.zip

解压

D:\dev\jdk15

环境变量

新增系统环境变量JAVA_HOME值是D:\dev\jdk15

Path系统环境变量增加一项 %JAVA_HOME%\bin

安装elasticsearch

elasticsearch7.9.2自带了OpenJDK15,我们自己安装有OpenJDK15,所有,可以把elasticsearch7.9.2目录里面的jdk删除了

下载

https://mirrors.huaweicloud.com/elasticsearch/7.9.2/elasticsearch-7.9.2-windows-x86_64.zip

解压

D:\dev\elasticsearch7.9.2\node-1
D:\dev\elasticsearch7.9.2\node-2
D:\dev\elasticsearch7.9.2\node-3

配置

node-1

cluster.name: timo
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
transport.port: 9300

# 集群发现,此设置通常应包含群集中所有可以成为master节点的地址
discovery.seed_hosts: ["127.0.0.1:9300", "127.0.0.1:9301", "127.0.0.1:9302"]

# 集群初始化的提供的master候选地址,第一次启动时将从该列表中获取master
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]

node-2

cluster.name: timo
node.name: node-1
network.host: 0.0.0.0
http.port: 9201
transport.port: 9301
discovery.seed_hosts: ["127.0.0.1:9300", "127.0.0.1:9301", "127.0.0.1:9302"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]

node-3

cluster.name: timo
node.name: node-1
network.host: 0.0.0.0
http.port: 9202
transport.port: 9302
discovery.seed_hosts: ["127.0.0.1:9300", "127.0.0.1:9301", "127.0.0.1:9302"]
cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]

防火墙增加入站规则

端口9200-9400

启动

运行

打开3个cmder窗口,分别运行

D:\dev\elasticsearch7.9.2\node-1\bin\elasticsearch
D:\dev\elasticsearch7.9.2\node-2\bin\elasticsearch
D:\dev\elasticsearch7.9.2\node-3\bin\elasticsearch

访问

http://localhost:9200/
# 查看集群健康状态
http://localhost:9200/_cluster/health

安装cerebro

下载

https://github.com/lmenezes/cerebro/releases/download/v0.9.2/cerebro-0.9.2.zip

解压

D:\dev\elasticsearch7.9.2\cerebro-0.9.2

启动

D:\dev\elasticsearch7.9.2\cerebro-0.9.2\bin\cerebro

配置

application.conf

auth={
    type: basic
    settings: {
        username="admin"
        password="123456"
    }
}
hosts = [
  {
    host = "http://localhost:9200"
    name = "timo"
    headers-whitelist = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ]
  }
]

访问

http://127.0.0.1:9000

创建一个索引

PUT /employee
{
    "settings": {
        "index": {
            "number_of_shards": 3,
            "number_of_replicas": 1
        }
    },
    "mappings": {
        "properties": {
            "age": {
                "type": "integer"
            },
            "first_name": {
                "type": "keyword"
            },
            "last_name": {
                "type": "keyword"
            },
            "about": {
                "type": "text"
            },
            "interests": {
                "type": "keyword"
            }
        }
    }
}

添加文档

点击cerebro顶部菜单rest创建创建

POST /employee/_doc/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

获取文档

GET employee/_doc/1

搜索文档

简单搜索

GET /employee/_search?q=last_name:Smith

使用DSL语句查询

GET /employee/_search
{
    "query": {
        "match": {
            "last_name": "Smith"
        }
    }
}

排序

GET /employee/_search
{
    "query":{
        "match":{
            "about":"rock"
        }
    },
    "sort":[{
        "age":"asc"
    }]
}

更复杂的搜索

GET /employee/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}

全文检索+高亮显示

GET /employee/_search
{
    "query":{
        "match":{
            "about":"rock climbing"
        }
    },
    "highlight":{
        "fields":{
            "about":{}
        }
    }
}

安装Kibana

https://www.elastic.co/guide/cn/kibana/current/install.html

安装ik分词插件

安装好后重启elasticsearch

D:\dev\elasticsearch7.9.2\node-1\bin\elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/ releases/download/v7.9.2/elasticsearch-analysis-ik-7.9.2.zip

文档

  1. Elasticsearch英文文档
  2. Elasticsearch: 权威指南
  3. Elasticsearch 权威指南(中文版)
  4. Elasticsearch中文文档
  5. ElasticSerach 7 教程
  6. elasticsearch7常见查询(term、match、bool、filter)

视频教程

  1. 【狂神说Java】ElasticSearch7.6.x最新完整教程通俗易懂
  2. Elasticsearch顶尖高手系列-高手进阶篇
  3. 无半句废话、全干货的Elasticsearch沥血之作