• ElasticSearch综合练习题,ES为8版本,使用Kibana运行语句


    前言

    • ES8版本没有type概念,所以语法可能会与其他版本有差异

    一、ES查询集群情况

    • 查看集群的健康状况
    GET _cat/health
    
    • 1
    • 查询ES的所有配置
    GET _all
    
    • 1

    二、ES索引习题

    • es的索引mapper结构不支持修改已经存在的字段类型,但是能添加新的字段

    查询所有索引

    • 查询ES中所有的index
    GET _cat/indices
    
    • 1
    • 查询所有index列表,格式化查询
    GET _mapping?pretty=true
    
    • 1
    • 查看所有索引文档总数
    GET _all/_count
    
    • 1

    查询单个索引

    • 增加一个aura_index的index库
    PUT /aura_index
    
    • 1
    • 查询aura_index索引的所有信息
    GET /aura_index
    
    • 1
    • 删除一个aura_index的index库
    DELETE /aura_index
    
    • 1
    • 查看aura_index索引文档总数
    GET /aura_index/_count
    
    • 1
    • 修改分片副本数量
    PUT /aura_index/_settings
    {
      "index":{
        "number_of_replicas":4
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 新增Mapping映射
    POST /aura_index/_mapping
    {
      "properties":{
        "phone":{
          "type":"keyword"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 创建website的index,要求为该索引有3个分片,2份副本
    PUT /website
    {
    	"settings":{
    		"index":{
    			"number_of_shards":3,
    			"number_of_replicas":2
    		}
    	}
    }
    {
        "website": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "creation_date": "1561987828517",
                    "number_of_shards": "3",
                    "number_of_replicas": "2",
                    "uuid": "3aiQiakoQeGcnrfdBVqBjA",
                    "version": {
                        "created": "6020299"
                    },
                    "provided_name": "website"
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    三、ES增删改查数据

    • 无论这个id是否存在,只要对这个id操作增删改,都会使这个id的version加1。
    • 如果该id已经存在,增加一条数据,旧数据完全被新数据完全覆盖。(不会报错)
    • 更新一条数据,如果id或者字段不存在(报错)

    单条处理

    • 增加一条数据
    PUT /megacorp/_doc/1
    {
    "first_name" : "John",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I love to go rock climbing",
    "interests": [ "sports", "music" ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 查询一条数据
    GET /megacorp/_doc/1
    
    • 1
    • 修改first字段
    POST /megacorp/_update/1
    {
      "doc":{
        "first_name" : "John001"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 删除数据
    DELETE /megacorp/_doc/1
    
    • 1

    批量处理

    _bulk命令:
    第一个json表示操作类型和操作的id
    第二个json是操作所需要的数据。(不允许换行,不然报错)
    反复执行,形成一个命令多个操作
    
    • 1
    • 2
    • 3
    • 4
    • 批量新增数据
    POST megacorp/_bulk
    {"index":{"_id":4}}
    {"id":4,"name":"admin","counter":"10","tags":["red","black"]}
    {"index":{"_id":5}}
    {"id":5,"name":"zcy"}
    {"index":{"_id":6}}
    {"id":6,"name":"admin006","counter":"10","tags":["red","black"]}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 批量修改数据
    修改的字段前必须带上 ctx._source.这个格式
    
    • 1
    POST megacorp/_bulk
    {"update":{"_id":4}}
    {"script":{"source":"ctx._source.name='admin_update'"}}
    {"update":{"_id":5}}
    {"script":{"source":"ctx._source.name='zcy_update'"}}
    {"update":{"_id":6}}
    {"script":{"source":"ctx._source.name='admin006_update'"}}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 批量删除数据
    POST megacorp/_bulk
    {"delete":{"_id":4}}
    {"delete":{"_id":5}}
    {"delete":{"_id":6}}
    
    • 1
    • 2
    • 3
    • 4

    四、雇员查询练习题

    • 初始化数据
    POST megacorp/_bulk
    {"index":{"_id":1}}
    {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests": [ "sports", "music" ]}
    {"index":{"_id":2}}
    {"first_name" : "Jane","last_name" : "Smith","age" : 32,"about" : "I like to collect rock albums","interests": [ "music" ]}
    {"index":{"_id":3}}
    {"first_name" : "Douglas","last_name" : "Fir","age" : 35,"about": "I like to build cabinets","interests": [ "forestry" ]}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 查看雇员id为1的信息
    GET /megacorp/_doc/1
    
    • 1
    • 搜索所有员工信息
    GET /megacorp/_search
    
    • 1
    • 搜索last_name字段值为Smith的员工信息
    GET /megacorp/_search
    {
    	"query": {
            "match": {
            	"last_name" :   "Smith"
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    GET /megacorp/_search
    {
     	"query": {
            "multi_match": {
              "query": "Smith",
              "fields": ["last_name"]
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 搜索名字为 Smith 的雇员,但年龄大于 30 岁的
    GET /megacorp/_search
    {
      "query": {
        "bool": {
          "must": [{"match": { "last_name" : "Smith"}},{"range": { "age": { "gt": 30}}}]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 搜索下所有喜欢攀岩(rock climbing)的雇员。
    GET /megacorp/_search
     {
     	"query": {
            "multi_match": {
              "query": "climbing",
              "fields": ["about"]
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 仅匹配同时包含 “rock” 和 “climbing”,并且二者以短语“rock climbing” 的形式紧挨着的雇员记录
    GET /megacorp/_search
    {
        "query": {
            "match_phrase":{
              "about":"rock climbing"
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 仅匹配同时包含 “rock” 和 “climbing”,并且二者以短语“rock climbing” 的形式紧挨着的雇员记录,同时需要高亮显示搜索的内容。
    GET /megacorp/_search
    {
        "query": {
            "match_phrase":{
              "about":"rock climbing"
            }
        },
        "highlight": {
            "fields" : {
                "about" : {}
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 求出所有雇员年龄相加的值
    GET /megacorp/_search
    {
      "size": 0,
      "aggs": {
        "total_amount": {
          "sum": {
            "field": "age"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 求出年龄大于30的雇员,年龄相加的值
    GET /megacorp/_search
    {
      "query": {
        "range": {
          "age": {
            "gt": 30
          }
        }
      }, 
      "size": 0,
      "aggs": {
        "total_amount": {
          "sum": {
            "field": "age"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 求所有雇员的平均年龄
    GET /megacorp/_search
    {
      "size": 0,
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    五、学生查询练习题

    • 初始化数据
    PUT /_bulk
    {"create":{"_index":"stu","_id":"1"}}
    {"id": 1, "studentNo": "TH-CHEM-2016-C001", "name": "Jonh Smith", "major":"Chemistry", "gpa": 4.8, "yearOfBorn": 2000, "classOf": 2016,  "interest": "soccer, basketball, badminton, chess"}
    {"create":{"_index":"stu","_id":"2"}}
    {"id": 2, "studentNo": "TH-PHY-2018-C001", "name": "Isaac Newton", "major":"Physics", "gpa": 3.6, "yearOfBorn": 2001, "classOf": 2018,  "interest": "novel, soccer, cooking"}
    {"create":{"_index":"stu","_id":"3"}}
    {"id": 3, "studentNo": "BU-POLI-2016-C001", "name": "John Kennedy", "major":"Politics", "gpa": 4.2, "yearOfBorn": 2000, "classOf": 2016,  "interest": "talking, dating, boxing, shooting, chess"}
    {"create":{"_index":"stu","_id":"4"}}
    {"id": 4, "studentNo": "BU-POLI-2015-C001", "name": "John Kerry",  "major":"Politics", "gpa": 4.1, "yearOfBorn": 1999, "classOf": 2015,  "interest": "money, basketball"}
    {"create":{"_index":"stu","_id":"5"}}
    {"id": 5, "studentNo": "BU-ARTS-2016-C002", "name": "Da Vinci",  "major":"Arts", "gpa": 4.8, "yearOfBorn": 1995, "classOf": 2016,  "interest": "drawing, music, wine"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 同时查询id为1,3,5
    GET /stu/_search
    {
    	"query":{
    		"terms":{
    			"id":["1","3","5"]
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 名字不叫John
    GET /stu/_search
    {
    	"query":{
    		"bool":{
    		"must_not":{
    			"match":{
    				"name":"John"
    			}
    		}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 在2016年以前入学的文档
    GET /stu/_search
    {
    	"query":{
    		"range":{
    			"classOf":{
    				"lt":"2016"
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 请把id为4文档添加一个兴趣(字段为“interest”) “poker”(PS:根据查询结果修改字段属性的值)
    POST /stu/_update/4
    {
      "doc":{
        "interest" : "money,basketball,poker"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    六、商品信息联系题

    • 初始化数据
    PUT /mystore
    {
      "settings": {
        "number_of_shards": 1
      },
      "mappings": {
        "properties": {
          "price": {
            "type": "integer"
          },
          "productID": {
            "type": "text"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    POST mystore/_bulk
    {"index":{"_id":1}}
    {"price":10,"productID":"XHDK-A-1293-#fJ3"}
    {"index":{"_id":2}}
    {"price":20,"productID":"XHDK-A-1293-#f20"}
    {"index":{"_id":3}}
    {"price":30,"productID":"JODL-X-1937-#pV7"}
    {"index":{"_id":4}}
    {"price":30,"productID":"QQPX-R-3956-#aD8"}
    {"index":{"_id":5}}
    {"price":50,"productID":"KDKE-B-9947-#kL5"}
    {"index":{"_id":6}}
    {"price":30,"productID":"KDKE-B-9947-#kL5"}
    {"index":{"_id":7}}
    {"price":70,"productID":"JODL-X-1937-#pV7"}
    {"index":{"_id":8}}
    {"price":40,"productID":"JODL-X-1937-#pV7"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 查找价格为20的商品信息,使用 constant_score 查询以非评分模式来执行 term 查询并以一作为统一评分
    GET mystore/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "term": { "price": 20 }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 查询具有"XHDK-A-1293-#fJ3"特定商品id的信息
    GET mystore/_search
    {
      "query":{
        "match_phrase":{
          "productID" : "XHDK-A-1293-#fJ3"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 查询价格在20-40之间的商品信息
    GET mystore/_search
    {
      "query": {
        "range": {
          "price": {
            "gte": 20,
            "lte": 40
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 查找商品列表中价格为20或30的商品信息
    GET mystore/_search
    {
    	"query":{
    		"terms":{
    			"price":[20,30]
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 查询商品价格为30或者"productID"为"XHDK-A-1293-#fJ3"的商品信息,但是商品的"productID"不能为"QQPX-R-3956-#aD8"
    GET mystore/_search
    {
      "query": {
        "bool": {
          "should": [
            {"match": {"price": "30"}},
            {"match_phrase": {"productID": "XHDK-A-1293-#fJ3"}}
          ],
          "must_not": [
            {"match_phrase": {"productID": "QQPX-R-3956-#aD8"}}
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 查询productID 为"KDKE-B-9947-#kL5"的商品信息或者 productID为"JODL-X-1937-#pV7" 并且同时 price为 30的商品信息
    GET mystore/_search
    {
      "query":{
        "bool":{
          "must":[
            {"match":{"price":30}},
            {"bool": {"should":[{"match_phrase":{"productID":"KDKE-B-9947-#kL5"}},{"match_phrase":{"productID":"JODL-X-1937-#pV7"}}]}}
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    其他:一问一答

    • 索引结构确定了,能插入和结构不一致的数据么?
      能,会自动扩大索引拥有的字段
    • 索引结构确定了,同一字段能插入不同类型的数据么?
      如果某字段是数字类型,不能插入字符串类型的数据,es会进行强转,强转失败则报错。但是能存入纯数字字符串,插入的数据还会以字符串的方式存储(长度有一定限制)。
    • 数字类型的字段进行字符串搜索匹配么?
      不能,搜索会报错

    参考文档

  • 相关阅读:
    机器会思考吗?浅析ai智能体框架metagpt的思考机制
    SmartCode ViewerX VNC 3.11 Crack
    golang工程——gRpc 拦截器及原理
    C语言字符串比较详解:strcmp和strncmp函数的使用方法和规则
    博俊转债上市价格预测
    【SpringCloud】微服务技术栈入门6 - RestClient深入
    线性代数2:梯队矩阵形式
    23面向对象
    Qt+ECharts开发笔记(四):ECharts的饼图介绍、基础使用和Qt封装百分比图Demo
    07.webpack5搭建Vue环境
  • 原文地址:https://blog.csdn.net/m0_46085118/article/details/134371040