第二十八节 Kibana查询ElasticSearch的操作命令简介

亮子 2024-10-25 00:37:55 918 0 0 0

1、查看索引列表

GET /_cat/indices

2、创建索引

PUT /es_user

3、查看索引

GET /es_user

4、删除索引

DELETE /es_user

5、向索引中存储数据

POST /es_user/_doc/1
{
  "userId": 1,
  "userName": "guguoyang",
  "nickName": "顾国阳",
  "age": 30,
  "hobbit": "学习,打篮球,游泳"
}

POST /es_user/_doc/2
{
  "userId": 2,
  "userName": "liyamin",
  "nickName": "李亚敏",
  "age": 36,
  "hobbit": "学习,打羽毛球,游泳"
}


POST /es_user/_doc
{
  "userId": 5,
  "userName": "lihanyu",
  "nickName": "李翰雨",
  "age": 26,
  "hobbit": "学习,打羽毛球,游泳"
}

6、查看索引数据(根据ID查看)

GET /es_user/_doc/1

7、查看所有数据

GET /es_user/_search

8、批量添加文档

POST /es_user/_bulk
{"index":{"_id":3}}
{"userId": 3,  "userName": "youzhijie",  "nickName": "尤志杰",  "age": 26,  "hobbit": "学习,足球,游泳"}
{"index":{"_id":4}}
{"userId": 4,  "userName": "guolincong",  "nickName": "郭林聪",  "age": 16,  "hobbit": "足球,游泳"}

9、修改文档(全量修改)

POST /es_user/_doc/1
{
  "userId": 1,
  "userName": "guguoyang",
  "nickName": "顾国阳",
  "age": 16,
  "hobbit": "学习,打篮球,游泳"
}

10、修改文档(局部修改)

POST /es_user/_update/1
{
  "doc": {
    "age": 99
  }
}

11、根据ID删除文档

DELETE /es_user/_doc/1

12、批量删除

POST /es_user/_bulk
{"delete":{"_id":"2"}}
{"delete":{"_id":"3"}}

13、查询所有的记录

GET /es_user/_search
{
  "query": {
    "match_all": {}
  }
}

14、根据条件查询

GET /es_user/_search
{
  "query": {
    "match": {
      "nickName": "郭林聪"
    }
  }
}

15、返回特定的字段

GET /es_user/_search
{
  "query": {
    "match": {
      "nickName": "郭林聪"
    }
  },
  "_source": ["nickName","age"]
}

16、分页查询(from不是页码,是偏移量的记录数)

# from - 跳过多少条数据 【计算】
# size - 每页展示多少条数据

GET /es_user/_search
{
  "query": {
    "match_all": {}
  },
  "from": 2,
  "size": 2
}

17、排序查询(根据年龄降序排列)

# 降序排列
GET /es_user/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

# 升序排列
GET /es_user/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

18、多条件查询

# should == OR

GET /es_user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "hobbit": "学习"
          }
        },
        {
          "match": {
            "nickName": "李"
          }
        }
      ]
    }
  }
}

# must == AND
GET /es_user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "hobbit": "学习"
          }
        },
        {
          "match": {
            "nickName": "李"
          }
        }
      ]
    }
  }
}

19、区间查询

GET /es_user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}

20、精确查询

GET /es_user/_search
{
  "query": {
    "term": {
      "age": {
        "value": "36"
      }
    }
  }
}

21、高亮搜索

GET /es_user/_search
{
  "query": {
    "match": {
      "hobbit": "游泳"
    }
  },
  "highlight": {
    "pre_tags": "<font color='red'>",
    "post_tags": "</font>",
    "fields": {
      "hobbit": {},
      "nickName": {}
    }
  }
}

22、聚合查询

# "size":0, // 如果加上这段会去除查询出来的原始数据

GET /es_user/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "count_age": {
      "terms": {
        "field": "age",
        "size": 10
      }
    }
  }
}

GET /es_user/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "age_age": {
      "avg": {
        "field": "age"
      }
    }
  }
}

23、测试默认分词器

POST /_analyze
{
 "text":"学习 Elasticsearch 数据库"
}

24、使用IK分词器

# ik_smart:最少切分(最粗粒度的切分模式)
POST /_analyze
{
  "analyzer": "ik_smart",
  "text":"学习 Elasticsearch 数据库"
}

# ik_max_word:最多切分(最细粒度的切分模式)

POST /_analyze
{
  "analyzer": "ik_max_word",
  "text":"学习 Elasticsearch 数据库"
}

25、查询ES安装了哪些插件

GET /_cat/plugins

26、查询索引的映射

GET /es_user/_mapping

GET /es_user

27、创建索引和设置动态映射

PUT /es_user1
{
    "mappings" : {
      "dynamic": true,
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "hobbit" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "nickName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "userId" : {
          "type" : "long"
        },
        "userName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
}

GET /es_user1

28、创建索引和设置静态映射

# 静态映射:不允许修改映射

PUT /es_user2
{
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "hobbit" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "nickName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "userId" : {
          "type" : "long"
        },
        "userName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
}

GET es_user2

POST /es_user2/_doc/6
{
  "userId": 6,
  "userName": "guoyongshuo",
  "nickName": "郭永硕",
  "age": 26,
  "hobbit": "学习,打羽毛球,游泳",
  "sex": "男"
}

POST /es_user2/_doc/6
{
  "userId": 6,
  "userName": "guoyongshuo",
  "nickName": "郭永硕",
  "age": "二十六",
  "hobbit": "学习,打羽毛球,游泳",
  "sex": "男"
}