2024年大数据最全ES数据库学

成功返回

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test"
}

demo1:

#自定义类型 type
PUT /test
{
  "mappings": {
    "properties": {
      "info": {
        "type": "text",
        "analyzer": "ik_smart"  #analyzer分词器选择
      },
      "email": {
        "type": "keyword", #字段类型
        "index": false
      },
      "name": {
        "properties": {
          "firstName": {
            "type": "keyword"
          },
          "lastName": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

demo2

#-----------用户user-----------------
#不自定义类型
PUT /user

#不自定义类型 会默认配置 如字段类型 分片 以及id
PUT /user/_doc/1
{
  "name":"张三",
  "age":10,
  "sex":"男",
  "address":"江苏苏州"
}

GET /user/_search

#批量创建文档数据
POST _bulk
{"create":{"_index":"user", "_type":"_doc", "_id":2}}
{"id":2,"name":"李四","age":"20","sex":"男","address":"苏州园区"}
{"create":{"_index":"user", "_type":"_doc", "_id":3}}
{"id":3,"name":"王芳","age":"30","sex":"女","address":"园区华为"}
{"create":{"_index":"user", "_type":"_doc", "_id":4}}
{"id":4,"name":"赵六","age":"40","sex":"女","address":"华为汽车"}

#批量获取文档数据
docs : 文档数组参数
_index : 指定index
_type : 指定type
_id : 指定id
_source : 指定要查询的字段
--------------------------------------------
GET _mget
{
  "docs": [
    {
      "_index": "user",
      "_type": "_doc",
      "_id": 1
    },
    {
      "_index": "user",
      "_type": "_doc",
      "_id": 2
    }
  ]
}

GET /user/_mget
{
  "docs": [
    {
      "_type": "_doc",
      "_id": 1
    },
    {
      "_type": "_doc",
      "_id": 2
    }
  ]
}

GET /user/_doc/_mget
{
  "docs": [
    {
      "_id": 1
    },
    {
      "_id": 2
    }
  ]
}

GET /user/_mget
{
  "docs": [
    {
      "_id": 1
    },
    {
      "_id": 2
    },
    {
      "_id": 3
    },
    {
      "_id": 4
    }
  ]
}

#批量修改文档数据,不存在则创建,存在则替换
POST _bulk
{"index":{"_index":"user", "_type":"_doc", "_id":2}}
{"id":2,"name":"李四","age":"20","sex":"男","address":"苏州园区"}
{"index":{"_index":"user", "_type":"_doc", "_id":3}}
{"id":3,"name":"王芳","age":"30","sex":"女","address":"园区华为"}
{"create":{"_index":"user", "_type":"_doc", "_id":4}}
{"id":4,"name":"赵六","age":"40","sex":"女","address":"华为汽车"}

#批量修改update
POST _bulk
{"update":{"_index":"user","_type":"_doc","_id":2}}
{"doc":{"address":"苏州园区XX"}}
{"update":{"_index":"user","_type":"_doc","_id":3}}
{"doc":{"address":"园区华为XX"}}

#批量删除
POST _bulk
{"delete":{"_index":"user", "_type":"_doc", "_id":3}}
{"delete":{"_index":"user", "_type":"_doc", "_id":4}}



2.3、删除索引,“acknowledged”:true表示删除成功
DELETE /test

2.4、查看索引的统计信息
GET /_stats?pretty

2.5、修改索引

​ 倒排索引结构,一旦数据结构改变(比如改变了分词器),就需要重新创建倒排索引,这简直是灾难。因此索引库一旦创建,无法修改mapping

​ 然无法修改mapping中已有的字段,但是却允许添加新的字段到mapping中,因为不会对倒排索引产生影响。

方法1:覆盖PUT
PUT first/_doc/1
{
  "name":"林",
  "age":18,
  "from":"gu",
  "desc":"念能力,学生,暗属性",
  "tags":["能力者","男","暗"]
}


方法2:更新 POST

​ 使用 POST 命令,在 id 后面跟 _update ,要修改的内容放到 doc 文档(属性)中即可。

POST first/_doc/3/_update 
{
  "doc": {
    "name":"愚者",
    "desc":"塔罗",
    "tags":["魔法","超能力","塔罗"]
  }
}

2.6、插入数据
PUT first/_doc/1
{
  "name":"林",
  "age":18,
  "from":"gu",
  "desc":"念能力",
  "tags":["能力者","学院","男"]
}

PUT first/_doc/2
{
  
  "name":"宝儿姐",
  "age":22,
  "from":"gu", 
  "desc":"道法",
  "tags":["道", "驱魔","女"]
}

2.7、查看索引
2.7.1、查看指定索引
GET /first?pretty  #查看结构

GET /first/_search #查看表内容 select * from first
or
GET /first/_search
{
  "query": {
    "match_all": {}
  }
}

{
  "took" : 787,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "春生",
          "age" : 18,
          "from" : "gu",
          "desc" : "念能力,学生,暗属性",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "愚者",
          "age" : 22,
          "from" : "gu",
          "desc" : "塔罗",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "宝儿姐",
          "age" : 18,
          "from" : "sheng",
          "desc" : "道法",
          "tags" : [
            "长生",
            "超能力",
            "道法"
          ]
        }
      }
    ]
  }
}


2.7.2、简单查询
GET first/_search?q=from:gu
#使用下面的查询,结果一样 查询条件添加到 match 
GET /first/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.4700036,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "愚者",
          "age" : 22,
          "from" : "gu",
          "desc" : "塔罗",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "春生",
          "age" : 18,
          "from" : "gu",
          "desc" : "念能力,学生,暗属性",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      }
    ]
  }
}

2.7.3、控制返回结果
 _source 来控制仅返回

GET /first/_search
{
  "query": {
    "match_all": {}
  },
      "_source": ["tags","name"]
}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "宝儿姐",
          "tags" : [
            "长生",
            "超能力",
            "道法"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "愚者",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "春生",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      }
    ]
  }
}

2.7.4、排序 sort

desc[倒序] or asc[正序]

GET /first/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["age","name"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "宝儿姐",
          "age" : 18
        },
        "sort" : [
          18
        ]
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "春生",
          "age" : 18
        },
        "sort" : [
          18
        ]
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "愚者",
          "age" : 22
        },
        "sort" : [
          22
        ]
      }
    ]
  }
}

2.7.5、分页查询 from size
GET /first/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["age","name"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ],
    "from":0, #第n条开始
    "size":1 #返回多少条数据
}

2.7.6、布尔查询
MUST

“select age,name where first where from=gu and age=18”

GET /first/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "from": "gu"
        }
        },
        {"match": {
          "age": "18"}
        }
      ]
    }
  },
  "_source": ["age","name"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "春生",
          "age" : 18
        },
        "sort" : [
          18
        ]
      }
    ]
  }
}

shoud

“select age,name where first where from=gu or age=18”

GET /first/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {
          "from": "gu"
        }
        },
        {"match": {
          "age": "18"}
        }
      ]
    }
  },
  "_source": ["age","name","from"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

most_not

“select age,name where first where from!=gu and age!=18”

GET /first/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {
          "from": "gu"
        }
        },
        {"match": {
          "age": "22"}
        }
      ]
    }
  },
  "_source": ["age","name","from"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

filter 过滤查询

过滤条件的范围用 range 表示

  • gt 表示大于
  • gte 表示大于等于
  • lt 表示小于
  • lte 表示小于等于

“select age,name where first where from=gu and age>=18 and age<=20”

GET /first/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "from": "gu"
        }
        }
      ],
      "filter": [
        {"range": {
          "age": {
            "gte": 18,
            "lte": 20
          }
        }}
      ]
    }
  },
  "_source": ["age","name","from"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

2.7.7、短语检索【可用数组中检索关键字】
模糊查找
GET /first/_search
{
  "query": {
    "match": {
      "tags": "暗 魔"  #空格分开
    }
    } 
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0732633,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "1",
        "_score" : 1.0732633,
        "_source" : {
          "name" : "春生",
          "age" : 18,
          "from" : "gu",
          "desc" : "念能力,学生,暗属性",
          "tags" : [
            "能力者",
            "男",
            "暗"
          ]
        }
      },
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 0.9403362,
        "_source" : {
          "name" : "愚者",
          "age" : 22,
          "from" : "gu",
          "desc" : "塔罗",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        }
      }
    ]
  }
}


精准查找
GET /first/_search
{
  "query": {
    "match_phrase": {
      "tags": "魔法"
    }
    } 
}

2.7.8 、term查询

term查询是直接通过倒排索引指定的 词条,也就是精确查找。

term和match的区别:

  • match是经过分析(analyer)的,也就是说,文档是先被分析器处理了,根据不同的分析器,分析出的结果也会不同,在会根据分词 结果进行匹配。
  • term是不经过分词的,直接去倒排索引查找精确的值。
2.7.8.1、字段是否存在:exist
GET /first/_search
{
  "query": {
    "exists": {
      "field": "from_"
    }
  }
  
}



2.7.8.2、id查询:ids

ids 即对id查找

GET /first/_search
{
  "query": {
    "ids": {
      "values": [3, 1]
    }
  }
}

2.7.8.3、前缀:prefix

通过前缀查找某个字段

GET /first/_search
{
  "query": {
    "prefix": {
      "desc": {
        "value": "道"
      }
    }
  }
}

select * from first where match(desc,"^道")

2.7.8.4、分词匹配:term

前文最常见的根据分词查询

GET /first/_search
{
  "query": {
    "terms": {
      "tags": "长生"
    }
  }
}

select * from first where "长生" in tags

2.7.8.5、多个分词匹配:terms

按照读个分词term匹配,它们是or的关系

GET /test-dsl-term-level/_search
{
  "query": {
    "terms": {
      "programming\_languages": ["php","c++"]
    }
  }
}

2.7.8.6、通配符:wildcard
GET /first/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "儿*",
        "boost": 1.0,
        "rewrite": "constant_score"
      }
    }
  }
}

SELECT  * from accesslog a WHERE match(host,'儿');

模糊匹配:fuzzy

官方文档对模糊匹配:编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括:

  • 更改字符(box→ fox)
  • 删除字符(black→ lack)
  • 插入字符(sic→ sick)
  • 转置两个相邻字符(act→ cat)
GET /first/_search
{
  "query": {
   "fuzzy": {
      "name": {
        "value": "shong"
      }
    }
  }
}
#可以匹配sheng

2.8、高亮显示
GET /first/_search
{
  "query": {
    "match_phrase": {
      "tags": "魔法"
    }
    },
    "highlight": {
      "fields": {
        "tags": {}
      }
    }
}

结果

{
  "took" : 108,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.390936,
    "hits" : [
      {
        "_index" : "first",
        "_type" : "chunsheng",
        "_id" : "3",
        "_score" : 1.390936,
        "_source" : {
          "name" : "愚者",
          "age" : 22,
          "from" : "gu",
          "desc" : "塔罗",
          "tags" : [
            "魔法",
            "超能力",
            "塔罗"
          ]
        },
        "highlight" : {
          "tags" : [
            "<em>魔</em><em>法</em>"  #this 
          ]
        }
      }
    ]
  }
}

2.9 深度分页

es有10000条限制,因此要使用分页

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  }
}

}


结果



{
“took” : 108,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 1.390936,
“hits” : [
{
“_index” : “first”,
“_type” : “chunsheng”,
“_id” : “3”,
“_score” : 1.390936,
“_source” : {
“name” : “愚者”,
“age” : 22,
“from” : “gu”,
“desc” : “塔罗”,
“tags” : [
“魔法”,
“超能力”,
“塔罗”
]
},
“highlight” : {
“tags” : [
” #this
]
}
}
]
}
}


#### 2.9 深度分页


es有10000条限制,因此要使用分页


[外链图片转存中...(img-ox4C15BW-1714640630659)]
[外链图片转存中...(img-i8mWoZj8-1714640630659)]
[外链图片转存中...(img-8NLzevdF-1714640630660)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值