默认端口号5601

高级条件查询之间的区别参考:六、ES的HTTP操作

1. 软件下载

https://artifacts.elastic.co/downloads/kibana/kibana-7.8.0-linux-x86_64.tar.gz

2. 软件安装

  • 上传至 /opt/software ,解压至 /opt/module/

    1. tar -zxvf kibana-7.8.0-linux-x86_64.tar.gz -C /opt/module/
  • 改名

    1. mv kibana-7.8.0-linux-x86_64 kibana-7.8.0
  • 修改 /kibana-7.8.0/config/kibana.yml 文件

    1. # 修改如下几项:
    2. # 对外服务监听端口
    3. server.port: 5601
    4. #绑定可以访问5601端口服务的IP地址,0.0.0.0表示任何地址在没有防火墙限制的情况下都可以访问,生产环境别这样设置,不安全。
    5. server.host: "0.0.0.0"
    6. # 默认值为主机名称,表示kibana实例绑定的主机,可以是IP地址或者主机名称
    7. server.name: "hadoop101"
    8. # ES实例地址,集群用,分隔
    9. elasticsearch.hosts: ["http://hadoop101:9200","http://hadoop102:9200","http://hadoop103:9200"]
    10. # 用来控制证书的认证,可选的值为full,none,certificate。此处由于没有证书,所以设置为null,否则启动会提示错误
    11. elasticsearch.ssl.verificationMode: none
    12. # kibana搜索数据请求超时时间
    13. elasticsearch.requestTimeout: 30000
    14. # kibana在es中存储自身数据的使用索引名
    15. kibana.index: ".kibana"
    16. # 让kibana支持中文
    17. i18n.locale: "zh-CN"

    3. 启动软件

    1. # 由于kibana默认不允许使用root启动,如果直接使用root启动需要增加--allow-root,否则汇报如下错误:
    2. nohup kibana --allow-root

    image.png

    4. Kibana启动脚本

    ```shell

    !/bin/bash

    启动Kibana—-配置的是es集群

    kibana_home=/opt/module/kibana-7.8.0 case $1 in “start”){

    1. echo "************ 启动Kibana ************"
    2. su - es -c "nohup $kibana_home/bin/kibana > $kibana_home/nohup.out 2>&1"

    };;

“stop”){
echo “** 停止Kibana **“ kibana_pid=ps -ef | awk '/kibana/ && !/awk/ {print $2}' ssh hadoop101 “su - es -c ‘kill -15 $kibana_pid’” };;
esac

  1. <a name="NZH8J"></a>
  2. # 5. 查询语句
  3. > 访问:IP:5601
  4. > ![image.png](https://cdn.nlark.com/yuque/0/2021/png/668367/1628351550756-1e37ba9b-f2dc-496c-847f-0ad593161c44.png#crop=0&crop=0&crop=1&crop=1&height=463&id=MmUMl&margin=%5Bobject%20Object%5D&name=image.png&originHeight=925&originWidth=889&originalType=binary&ratio=1&rotation=0&showTitle=false&size=78473&status=done&style=none&title=&width=444.5)
  5. <a name="T2PTz"></a>
  6. ## 5.1 查看ES各种信息
  7. <a name="VdqOn"></a>
  8. ### 5.1.1 查看所有节点
  9. ```http
  10. GET /_cat/nodes

5.1.2 查看集群健康状态

  1. GET /_cluster/health?pretty

5.1.3 查看所有索引及索引状态

  1. GET /_cat/indices?v

5.1.4 使用分词器分析词条

  1. GET /_analyze
  2. {
  3. "analyzer": "standard",
  4. "text": "hello world"
  5. }
  6. # "analyzer": "standard":
  7. # 是指定使用哪个分词器,standard是自带分词器
  8. # 如果装了ik分词器
  9. # 1. 可以使用ik_max_word --- 会将文本做最细粒度的拆分
  10. # 2. 还可以使用ik_smart --- 会将文本做最粗粒度的拆分
  11. # "text": "hello world"是指定文本

5.2 索引

5.2.1 创建

  1. PUT /person

5.2.2 创建索引同时创建映射、设置分片和副本

  1. PUT person
  2. {
  3. "settings" : {
  4. "number_of_shards" : 3, // 3个主分片
  5. "number_of_replicas" : 1 // 每个分片1个副本
  6. },
  7. "mappings": {
  8. "properties":{
  9. "name":{
  10. "type":"text",
  11. "index":true
  12. },
  13. "sex":{
  14. "type":"keyword",
  15. "index":true
  16. },
  17. "tel":{
  18. "type":"text",
  19. "store":true
  20. }
  21. }
  22. }
  23. }

5.2.3 修改副本分片数

  1. PUT person/_settings
  2. {
  3. "number_of_replicas" : 2
  4. }

5.2.4 查看所有索引

  1. GET /_cat/indices?v

5.2.5 查询单个

  1. GET person

5.2.6 删除索引

  1. DELETE person

5.3 映射

5.3.1 创建

如果没有索引会自动创建

  1. PUT person
  2. {
  3. "mappings": {
  4. "properties":{
  5. "name":{
  6. "type":"text",
  7. "index":true
  8. },
  9. "sex":{
  10. "type":"keyword",
  11. "index":true
  12. },
  13. "tel":{
  14. "type":"text",
  15. "store":true
  16. }
  17. }
  18. }
  19. }

5.3.2 查看

  1. GET person/_mapping

5.4 文档

5.4.1 新增

5.4.1.1 随机_id

  1. POST person/_doc
  2. {
  3. "name":"张三",
  4. "sex":"男的",
  5. "tel":"1111"
  6. }

5.4.1.2 新增—-自定义_id

  1. POST person/_doc/1002
  2. {
  3. "name":"李小四",
  4. "sex":"女的",
  5. "tel":"1111"
  6. }

5.4.2 查询

5.4.2.1 主键查询

  1. GET person/_doc/1001

5.4.2.2 查询文档—-全部查询

  1. GET person/_doc/_search

5.4.2.3 高级查询

5.5 高级查询

5.4.3 修改

5.4.3.1 某条记录某个字段

  1. POST person/_update/1001
  2. {
  3. "doc": {
  4. "tel":"2222"
  5. }
  6. }

5.4.3.2 某条记录全字段

  1. POST person/_doc/1001
  2. {
  3. "name":"李老四",
  4. "sex":"中性的",
  5. "tel":"3333"
  6. }

5.4.4 删除

5.4.4.1 主键删除

  1. DELETE person/_doc/1001

5.4.4.2 条件删除

  1. POST person/_delete_by_query
  2. {
  3. "query":{
  4. "match": {
  5. "tel": "1111"
  6. }
  7. }
  8. }

5.5 高级查询

5.5.1 查询所有记录

  1. GET person/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

5.5.2 单字段分词词条匹配查询

张三会分词成张、三,两个词条去匹配词条字段

  1. GET person/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "张三"
  6. }
  7. }
  8. }

5.5.3 多字段分词词条匹配查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "四",
  6. "fields": ["name","sex"]
  7. }
  8. }
  9. }

5.5.4 短语匹配查询

把李四当做一个词条去匹配

  1. GET person/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "name": "李四"
  6. }
  7. }
  8. }

5.5.5 单关键字精确查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "term": {
  5. "name": {
  6. "value": "李四"
  7. }
  8. }
  9. }
  10. }

5.5.6 多关键字精确查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "name": [
  6. "李",
  7. "女"
  8. ]
  9. }
  10. }
  11. }

5.5.7 (不)返回指定字段查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "李"
  6. }
  7. },
  8. "_source": {
  9. "includes": "tel",
  10. "excludes": "sex"
  11. }
  12. }

5.5.8 组合查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "sex": "女的"
  9. }
  10. },
  11. {
  12. "match": {
  13. "name": "小"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

5.5.9 范围查询

5.5.9.1 直接范围查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "range": {
  5. "tel": {
  6. "gte": 10,
  7. "lte": 20000
  8. }
  9. }
  10. }
  11. }

5.5.9.2 过滤查询范围

  1. GET person/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "sex": "男的"
  9. }
  10. }
  11. ],
  12. "filter": [
  13. {
  14. "range": {
  15. "tel": {
  16. "gte": 1000,
  17. "lte": 2000
  18. }
  19. }
  20. }
  21. ]
  22. }
  23. }
  24. }

5.5.10 通配符查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "wildcard": {
  5. "sex": {
  6. "value": "*的"
  7. }
  8. }
  9. }
  10. }

5.5.11 纠错查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "fuzzy": {
  5. "tel": {
  6. "value": "111122",
  7. "fuzziness": 3
  8. }
  9. }
  10. }
  11. }

5.5.12 排序查询

5.5.12.1 单字段排序查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "_id": {
  9. "order": "desc"
  10. }
  11. }
  12. ]
  13. }

5.5.12.2 多字段排序查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "_id": {
  9. "order": "desc"
  10. },
  11. "sex": {
  12. "order": "desc"
  13. }
  14. }
  15. ]
  16. }

5.5.13 高亮查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "match": {
  5. "name": "四"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "name":{}
  11. },
  12. "pre_tags": "<font color='red'>",
  13. "post_tags": "</font>"
  14. }
  15. }

5.5.14 分页查询

  1. GET person/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "from": 0,
  7. "size": 2
  8. }

5.5.15 统计查询

  1. # 1. 平均值查询
  2. GET person/_search
  3. {
  4. "aggs":{
  5. "tel_avg":{
  6. "sum":{
  7. "field":"sex"
  8. }
  9. }
  10. },
  11. "size":0
  12. }
  13. # 2. state聚合查询,对某个字段一次性返回 count,max,min,avg 和 sum 五个指标
  14. GET person/_search
  15. {
  16. "aggs":{
  17. "stats_age":{
  18. "stats":{
  19. "field":"age"
  20. }
  21. }
  22. },
  23. "size":0
  24. }

5.5.16 分组聚合查询(桶聚合查询)

  1. GET person/_search
  2. {
  3. "aggs":{
  4. "price_group":{
  5. "terms":{
  6. "field":"sex"
  7. }
  8. }
  9. },
  10. "size": 0
  11. }