文章目录
前言 一、ES查询集群情况 二、ES索引习题
三、ES增删改查数据
四、雇员查询练习题 五、学生查询练习题 六、商品信息联系题 其他:一问一答 参考文档
前言
ES8版本没有type概念,所以语法可能会与其他版本有差异
一、ES查询集群情况
GET _cat/health
GET _all
二、ES索引习题
es的索引mapper结构不支持修改已经存在的字段类型,但是能添加新的字段
查询所有索引
GET _cat/indices
GET _mapping?pretty=true
GET _all/_count
查询单个索引
PUT /aura_index
GET /aura_index
DELETE /aura_index
GET /aura_index/_count
PUT /aura_index/_settings
{
"index":{
"number_of_replicas":4
}
}
POST /aura_index/_mapping
{
"properties" :{
"phone" :{
"type" : "keyword"
}
}
}
创建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"
}
}
}
}
三、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
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" ]}
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"
}
}
}
仅匹配同时包含 “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"
}
}
}
}
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"
}
}
}
}
五、学生查询练习题
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"}
GET /stu/_search
{
"query":{
"terms":{
"id":["1","3","5"]
}
}
}
GET /stu/_search
{
"query":{
"bool":{
"must_not":{
"match":{
"name":"John"
}
}
}
}
}
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"
}
}
}
GET mystore/_search
{
"query": {
"range": {
"price": {
"gte": 20,
"lte": 40
}
}
}
}
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会进行强转,强转失败则报错。但是能存入纯数字字符串,插入的数据还会以字符串的方式存储(长度有一定限制)。 数字类型的字段进行字符串搜索匹配么? 不能,搜索会报错
参考文档
ElasticSearch综合练习题 ElasticSearch从入门到精通,史上最全(持续更新,未完待续,每天一点点) ES: 数据增,删,改,批量操作