elasticsearch-kibana环境搭建

一. elasticsearch环境搭建

  1. 复制elasticsearch-7.7.0-amd64.deb到服务器~/soft目录下
  2. 使用命令sudo dpkg -i elasticsearch-7.7.0-amd64.deb安装
  3. *可以查看安装目录在/usr/share/elasticsearch
  4. *可以查看配置文件在目录/etc/elasticsearch/elasticsearch.yml
  5. 修改配置文件sudo vi /etc/elasticsearch/elasticsearch.yml
network.host: 0.0.0.0
node.name: node-1
cluster.initial_master_nodes: ["node-1"]
  1. 启动elasticsearch服务service elasticsearch start
  2. 浏览器输入http://localhost:9200即可查看

二. kibana环境搭建

  1. 复制kibana-7.7.0-amd64.deb到服务器~/soft目录下
  2. 使用命令sudo dpkg -i kibana-7.7.0-amd64.deb安装
  3. *可以查看安装目录在/usr/share/kibana
  4. *可以查看配置文件在目录/etc/elasticsearch/kibana.yml
  5. 修改配置文件sudo vi /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
il8n.locale: "zh-CN"

三. 使用kibana操作elasticsearch

# 1.创建索引
PUT test1
# 2.查看索引
GET _cat/indices
# 3.删除索引
DELETE test1
# 4.添加数据
PUT test1/_doc/1
{
  "name":"大壮",
  "age":18,
  "location":"北京"
}
# 5.查看数据
GET test1/_doc/1
# 6.查看索引内所有数据
GET test1/_search
# 7.更新数据
POST test1/_update/1
{
  "doc": {
    "location": "北京市朝阳区"
  }
}
# 8.删除数据
DELETE test1/_doc/1
# 9.简单查询
GET test1/_search?q=location="广州"
# 10.使用DSL语句进行查询
# location text 分词,以or的形式匹配
# location.keyword,不分词
GET test1/_search
{
  "query": {
    "match": {
      "location.keyword": "广州"
    }
  }
}
# 11.select * from tables
GET test1/_search
{
  "query": {
    "match_all": {}
  }
}
# 12.按age排序
GET test1/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}
# 13.分页,从索引为1开始,查询两条
GET test1/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1,
  "size": 2
}  
# 14.bool查询 not and or
GET test1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "location.keyword": "广州"
          }
        }
      ], 
      "should": [
        {
          "match": {
            "age": 20
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "name": "李四"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gt": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}
# 15.结果过滤
GET test1/_search
{
  "_source": "name"
}
# 16.高亮显示
GET test1/_search
{
  "query": {
    "match": {
      "location": "广州"
    }
  },
  "highlight": {
    "pre_tags": "",
    "post_tags": "", 
    "fields": {
      "location": {}
    }
  }
}
# 17. 聚合函数
GET test1/_search
{
  "query": {
    "match": {
      "location.keyword": "广州"
    }
  },
  "aggs": {
    "agg_avg": {
      "avg": {
        "field": "age"
      }
    }
  },
 "_source": ["name", "age"]
}
# 18.分组查询
GET test1/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "age_group": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          }
        ]
      }
      , "aggs": {
        "age_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}
  • 安装ik分词器sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.0/elasticsearch-analysis-ik-7.7.0.zip

你可能感兴趣的:(elasticsearch-kibana环境搭建)