已更新整合springboot ,这里说的是答题方向,详细实践可以参考代码:https://gitee.com/laobinggan/img-blog/blob/master/blog/es-boot-nonBoot.zip
ES是一个开源的高扩展的分布式全栈全文搜索引擎。
下载链接直达:https://www.elastic.co/downloads/past-releases#elasticsearch,解压后点击elasticserch。bat,启动,访问http://localhost:9200/
数据格式
倒排索引
2.快速入门中午文档https://www.elastic.co/guide/cn/elasticsearch/guide/current/_document_oriented.html
2.1index操作 2.1.1使用postman发送put请求创建 indexhttp://127.0.0.1:9200/shoppong2.1.2查询index相关信息,使用postman发送get请求
http://127.0.0.1:9200/shoppong
结果:
2.1.3查询所有index,发送get请求http://127.0.0.1:9200/_cat/indices?v2.1.4删除index,发送delete请求
http://127.0.0.1:9200/shoppong2.2数据操作 2.2.1添加数据,发送post请求(非幂等)
http://127.0.0.1:9200/shoppong/_doc
需要添加请求体json数据
每次产生的id不一样
2.2.2添加数据,可以指定id,就是幂等操作,可以使用put操作http://127.0.0.1:9200/shoppong/_doc/1
添加数据也可以使用restful-create
http://127.0.0.1:9200/shoppong/_create/22.2.3查询doc中数据,get请求
http://127.0.0.1:9200/shoppong/_doc/12.2.4查询所有数据
http://127.0.0.1:9200/shoppong/_search2.2.5查询指定id数据
http://127.0.0.1:9200/shoppong/_doc/22.2.6修改数据
1.全部修改就是覆盖,PUT请求,传入json数据
http://127.0.0.1:9200/shoppong/_doc/1
2.局部修改非幂等,post请求,传入修改数据
http://127.0.0.1:9200/shoppong/_update/12.2.7删除操作,delete请求
http://127.0.0.1:9200/shoppong/_doc/12.2.8按条件查询重点**,get请求
http://127.0.0.1:9200/shoppong/_search
其他细节
fom的公式 (想查询的页面-1)*每页条数2.2.9复合条件查新,title=mytest并且age=13
{
"query":{
"bool":{
"must":[
{
"match":{
"title":"mytest"
}
},{
"match":{
"age":"13"
}
}]
}
}
}
title=mytest或者age=13,语句的must改为should
{
"query":{
"bool":{
"should":[
{
"match":{
"title":"mytest"
}
},{
"match":{
"age":"13"
}
}]
}
}
}
2.2.10范围查询大于某个部分
{
"query":{
"bool":{
"must":[
{
"match":{
"title":"mytest"
}
},{
"match":{
"age":"13"
}
}],
"filter":{
"range":{
"age":{
"gt":12
}
}
}
}
}
}
2.2.11固定检索,检索内容高亮
{
"query":{
"match_phrase":{
"age":12
}
},
"highlight":{
"fields":{
"title":{}
}
}
}
2.2.12 聚合操作分组
{
"aggs":{
"age-group":{//分组名任意
"terms":{//分组 改为avg就是求平均值
"field":"age"//根据什么分组
}
}
},
"size":0 //取除原始数据
}
2.2.13 映射关系
设置内容是否可被查找
{
"properties":{
"name":{
"type":"text",
"index":true
},
"sex":{
"type":"keyword",
"index":true
},
"tel":{
"type":"keyword"
"index":false
}
}
}
创建数据后查询,分别对应,可以分块检索,不可分块检索,无法查询
{
"query":{
"match":{
"tel":"2222"
}
}
}
3.JavaAPI操作
导入需要的依赖
org.elasticsearch elasticsearch7.8.0 org.elasticsearch.client elasticsearch-rest-high-level-client7.8.0 org.apache.logging.log4j log4j-api2.8.2 org.apache.logging.log4j log4j-core2.8.2 com.fasterxml.jackson.core jackson-databind2.9.9 cn.hutool hutool-all5.7.21 junit junit4.12
开始测试,详见代码,内容过多
4集群集群Cluster
一个集群就是由一个或多个服务器节点组织在一起,共同持有整个的数据,并起一提供
索引和搜索功能。一个Elasticsearch集群有一个唯一.的名字标识,这个名字默认就
是”elasticsearch”。这个名字是重要的,因为一 个节点只能通过指定某个集群的名字,来加入
这个集群。
节点Node
集群中包含很多服务器,一个节点就是其中的一个服务器。作为集群的一部分,它存储
数据,参与集群的索引和搜索功能。
复制读个es,删除data和log,修改config
//集群名称需一致 cluster.name: my-application // 本节点的name,masster,data设置 node.name: node-3 node.master: true node.data: true // 网络地址 network.host: localhost # # Set a custom port for HTTP: #端口号 http.port: 1003 //集群内部通信端口 transport.tcp.port: 9303 // es内部配置 discovery.seed_hosts: ["localhost:9301","localhost:9302"] discovery.zen.fd.ping_timeout: 1m discovery.zen.fd.ping_retries: 5 // 跨域 http.cors.enabled: true http.cors.allow-origin: "*"4.2es进阶 4.2.1概念
index 索引
doc 数据
filed 字段
mapping 映射,表结构信息
shards 分片就是分表
replicas 副本,多分数据
allocaton 分配 master决定分片如何分配
4.2.2架构 4.2.3单机集群创建put请求
http://127.0.0.1:1001/users
json配置
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
智能故障转移,水平扩容,应对故障,调整副本个数
put请求
http://127.0.0.1:1001/users/_settings
{
"number_of_replicas" : 2
}
4.2.4路由计算和分片控制
数据存入前,先决定放入那个主分片,路由到那个主分片由hash计算。这就是路由极端
分片控制,就是取数据由那个数据点去取。一般为轮询
4.2.5 集群数据读/写/更新流程读流程
可选参数改变流程
写流程
更新
剩余的原理分析,自己听的混乱,太多专有名词,使用过一段时间后再更新。
4.2.6 kibana下载直达:https://www.elastic.co/elastic-stack/
修改配置文件,加入
5.整合springboot1.这里说一下基本步骤,详细可以看代码
2.这里创建的是空项目再整合成boot,如果直接创建boot项目可以省下一些环节。
pom文件
4.0.0 com.yt.es es-boot1.0.0 org.springframework.boot spring-boot-starter-parent2.3.6.RELEASE 11 11 org.projectlombok lombokorg.springframework.boot spring-boot-starter-data-elasticsearchorg.springframework.boot spring-boot-devtoolsruntime true org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-testjunit junitorg.springframework spring-test
3.application.properties加入配置
# es 服务地址 elasticsearch.host=127.0.0.1 # es 服务端口 elasticsearch.port=9200 # 配置日志级别,开启 debug 日志 logging.level.com.atguigu.es=debug
4.创建实体类,加入注解配置
5.创建接口继承ElasticsearchRepository
6.创建测试类,测试使用



