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

前言

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

一、ES查询集群情况

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

二、ES索引习题

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

所有索引操作

  • 查询ES中所有的index名(简单)
GET /_cat/indices
  • 查询所有index结构(详细),格式化查询
GET /_mapping?pretty=true
  • 查看所有索引文档总数
GET /_all/_count

单个索引操作

  • 增加一个aura_index的index库
PUT /aura_index
  • 创建website的index,要求为该索引有3个分片,2份副本
PUT /website
{
	"settings":{
		"index":{
			"number_of_shards":3,
			"number_of_replicas":2
		}
	}
}
  • 创建website的index,自定义字段price和productID
PUT /website
{
  "mappings": {
    "properties": {
      "price": {
        "type": "long"
      },
      "productID": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

  • 删除一个aura_index的index库
DELETE /aura_index

  • 关闭aura_index索引
POST /aura_index/_close
  • 打开aura_index索引
POST /aura_index/_open
  • 修改分片副本数量
PUT /aura_index/_settings
{
  "index":{
    "number_of_replicas":4
  }
}
  • 新增Mapping映射
POST /aura_index/_mapping
{
  "properties":{
    "phone":{
      "type":"keyword"
    }
  }
}

  • 查询aura_index索引的所有信息
GET /aura_index
  • 查询aura_index索引的mappings结构
GET /aura_index/_mappings
  • 查询aura_index索引的settings结构
GET /aura_index/_settings
  • 查看aura_index索引文档总数
GET /aura_index/_count

三、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" ]
}
  • 查询一条数据
GET /megacorp/_doc/1
  • 修改first字段
POST /megacorp/_update/1
{
  "doc":{
    "first_name" : "John001"
  }
}
  • 删除数据
DELETE /megacorp/_doc/1

批量处理

_bulk命令:
第一个json表示操作类型和操作的id
第二个json是操作所需要的数据。(不允许换行,不然报错)
反复执行,形成一个命令多个操作
  • 批量新增数据
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"]}
  • 批量修改数据
修改的字段前必须带上 ctx._source.这个格式
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'"}}
  • 批量删除数据
POST megacorp/_bulk
{"delete":{"_id":4}}
{"delete":{"_id":5}}
{"delete":{"_id":6}}

四、雇员查询练习题

  • 初始化数据
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" ]}
  • 查看雇员id为1的信息
GET /megacorp/_doc/1
  • 搜索所有员工信息
GET /megacorp/_search
  • 搜索last_name字段值为Smith的员工信息
GET /megacorp/_search
{
	"query": {
        "match": {
        	"last_name" :   "Smith"
        }
    }
}
GET /megacorp/_search
{
 	"query": {
        "multi_match": {
          "query": "Smith",
          "fields": ["last_name"]
        }
    }
}
  • 搜索名字为 Smith 的雇员,但年龄大于 30 岁的
GET /megacorp/_search
{
  "query": {
    "bool": {
      "must": [{"match": { "last_name" : "Smith"}},{"range": { "age": { "gt": 30}}}]
    }
  }
}
  • 搜索下所有喜欢攀岩(rock climbing)的雇员。
GET /megacorp/_search
 {
 	"query": {
        "multi_match": {
          "query": "climbing",
          "fields": ["about"]
        }
    }
}
  • 仅匹配同时包含 “rock” 和 “climbing”,并且二者以短语“rock climbing” 的形式紧挨着的雇员记录
GET /megacorp/_search
{
    "query": {
        "match_phrase":{
          "about":"rock climbing"
        }
    }
}
GET /megacorp/_search
{
    "query": {
        "match":{
          "about":{
            "query": "rock climbing",
            "operator": "and"
          }
        }
    }
}
  • 仅匹配同时包含 “rock” 和 “climbing”,并且二者以短语“rock climbing” 的形式紧挨着的雇员记录,同时需要高亮显示搜索的内容。
GET /megacorp/_search
{
    "query": {
        "match_phrase":{
          "about":"rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}
  • 求出所有雇员年龄相加的值
GET /megacorp/_search
{
  "size": 0,
  "aggs": {
    "total_amount": {
      "sum": {
        "field": "age"
      }
    }
  }
}
  • 求出年龄大于30的雇员,年龄相加的值
GET /megacorp/_search
{
  "query": {
    "range": {
      "age": {
        "gt": 30
      }
    }
  }, 
  "size": 0,
  "aggs": {
    "total_amount": {
      "sum": {
        "field": "age"
      }
    }
  }
}
  • 求所有雇员的平均年龄
GET /megacorp/_search
{
  "size": 0,
  "aggs": {
    "avg_age": {
      "avg": {
        "field": "age"
      }
    }
  }
}
  • 查询所有雇员,并按照年龄降序
GET /megacorp/_search
{
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}
  • 查询所有雇员,从第0个开始,取接下来的2个
GET /megacorp/_search
{
  "from": 0, 
  "size": 2
}

五、学生查询练习题

  • 初始化数据
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"}
  • 同时查询id为1,3,5
GET /stu/_search
{
	"query":{
		"terms":{
			"id":["1","3","5"]
		}
	}
}
  • 名字不叫John
GET /stu/_search
{
	"query":{
		"bool":{
		"must_not":{
			"match":{
				"name":"John"
			}
		}
		}
	}
}
  • 在2016年以前入学的文档
GET /stu/_search
{
	"query":{
		"range":{
			"classOf":{
				"lt":"2016"
			}
		}
	}
}
  • 请把id为4文档添加一个兴趣(字段为“interest”) “poker”(PS:根据查询结果修改字段属性的值)
POST /stu/_update/4
{
  "doc":{
    "interest" : "money,basketball,poker"
  }
}

六、商品信息联系题

  • 初始化数据
PUT /mystore
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "price": {
        "type": "integer"
      },
      "productID": {
        "type": "text"
      }
    }
  }
}
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"}
  • 查找价格为20的商品信息,使用 constant_score 查询以非评分模式来执行 term 查询并以一作为统一评分
GET mystore/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": { "price": 20 }
      }
    }
  }
}
  • 查询具有"XHDK-A-1293-#fJ3"特定商品id的信息
GET mystore/_search
{
  "query":{
    "match_phrase":{
      "productID" : "XHDK-A-1293-#fJ3"
    }
  }
}
  • 查询价格在20-40之间的商品信息
GET mystore/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 20,
        "lte": 40
      }
    }
  }
}
  • 查找商品列表中价格为20或30的商品信息
GET mystore/_search
{
	"query":{
		"terms":{
			"price":[20,30]
		}
	}
}
  • 查询商品价格为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"}}
      ]
    }
  }
}
  • 查询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"}}]}}
      ]
    }
  }
}

其他:一问一答

  • 索引结构确定了,能插入和结构不一致的数据么?
    能,会自动扩大索引拥有的字段

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

  • 数字类型的字段进行字符串搜索匹配么?
    不能,搜索会报错

  • 创建索引会自动生成不支持修改的设置(settings字段),写入创建语句会报错
    provided_name:索引名
    uuid:索引id
    creation_date:创建时间
    version:版本

  • 创建索引会自动生成可修改的设置(settings字段)
    number_of_shards:分片,默认为1
    number_of_replicas:副本,默认为1
    max_result_window:
    mapping.total_fields.limit:

参考文档

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值