兰州兼职做网站,贵州建设工程招投标网站,优秀网站 要素,淮安做网站的有多少目录结构#xff1a;
一、es概述
二、es安装/head插件安装/kibana安装
三、es核心概念
四、IK分词器
五、RestFul操作
六、CRUD(增删改查)
七、Springboot集成es
---------------------------------------分割线#xff1a;正文------------------------------------…目录结构
一、es概述
二、es安装/head插件安装/kibana安装
三、es核心概念
四、IK分词器
五、RestFul操作
六、CRUD(增删改查)
七、Springboot集成es
---------------------------------------分割线正文--------------------------------------------------------
一、es概述
1、Doug Cutting
1创建了Lucenees与solr基于Lucene 开源且功能为一套信息检索工具包jar包
2实现了NDFS分布式文件存储系统基于Google的GFS系统
3将NDFS与MapReduce升级改造重新命名为Hadoophadoop之父
4基于Google的BigTable实现了HBase
2、ElasticSearch概述
1概念开源的分布式全文检索引擎
2优点近乎实时存储、检索数据扩展性好可扩展上百台服务器处理PB级别的数据基于Lucene核心通过RestFul API使全文搜索变得简单
3谁在使用维基百科搜狐新闻Github等等
3、ES与solr的区别
1ES是RestFul API而Solr是WebService API
2单纯已有的数据搜索solr更快
3建立索引后的数据Solr会产生io阻塞es有明显的优势
4数据量增加Solr的搜索效率会变低es不受影响 二、es安装/head插件安装/kibana安装 声明安装条件JDK1.8 1、es下载
1es客户端https://www.elastic.co/cn/downloads/elasticsearch
2ik分词器https://github.com/medcl/elasticsearch-analysis-ik/releases
3kibanahttps://www.elastic.co/cn/downloads/kibana
4head插件https://github.com/mobz/elasticsearch-head/archive/master.zip
5chrome-es插件https://github.com/mobz/elasticsearch-head/blob/master/crx/es-head.crx
2、es客户端windows安装1安装步骤
elasticsearch-7.12.1-windows-x86_64.zip 解压即可。
2相关文件
bin启动文件
config配置文件
- - log4j2.properties日志配置
- - elasticsearch.ymles配置文件默认9200端口
- - jvm.optionsjava虚拟机相关配置
lib相关jar包
modules功能模块
plugins插件如ik分词器等
logs日志
3启动es
双击bin目录下elasticsearch.bat 启动成功默认访问9200端口 浏览器访问http://127.0.0.1:9200/ 返回相关配置信息 3、安装可视化界面es head的插件
1解压缩elasticsearch-head-master.zip
2安装node.jshttps://nodejs.org/en/
cmdnode -v验证安装成功 3安装cnpm
cmd安装淘宝镜像npm install -g cnpm -registryhttps://registry.npm.taobao.org cnpm -v验证安装成功 4安装依赖
进入D:\elasticsearch\elasticsearch-head-master目录执行cnpm install 5运行npm run start启动npm 6浏览器访问http://localhost:9100
由于跨域无法访问 7配置可跨域
配置conf下elasticsearch.yml新增配置 http.cors.enabled: true
http.cors.allow-origin: * 重启es服务再次访问集群连接正常 4、Kibana安装
1Kibana安装的版本要与es版本对应
2启动测试运行bin下kibana.bat并测试对应的接口 3开发工具 http://localhost:5601 4配置中文config下kibana.yml新增配置并重启服务 i18n.locale: zh-CN 三、es核心概念 1、elasticsearch是面向文档的(一切都是json)与关系型数据库的对比
RelationDB - Elasticsearch
数据库(database) - 索引(indices)
表(tables) - types(慢慢会被弃用)
行(rows) - documents
字段(columns) - fields
2、es物理设计
elasticsearch在后台把每个索引划分成多个分片每个分片在集群中的不同的服务间迁移。
默认的集群名就是elasticsearch
3、逻辑设计
1文档最小单位即一条条数据
2类型text、date等类型
3索引数据库
4、es字段类型
text会被分词器解析
keyword不会被分词器解析 四、IK分词器
1、分词
把一段中文或别的划分为一个个关键字我们在搜索时候会把自己的信息进行分词默认的中文分词器将每个字看成一个词我们需安装使用中文分词器ik来解决
2、安装ik分词器
1下载https://github.com/medcl/elasticsearch-analysis-ik/releases
2安装elasticsearch-analysis-ik-7.12.1.zip放入elasticsearch-7.12.1\plugins目录下解压缩即可使用 3重启观察ES加载插件成功 [2021-05-12T23:00:05,420][INFO ][o.e.p.PluginsService ] [DESKTOP-GIEVO4Q] loaded plugin [analysis-ik]
[2021-05-12T23:00:05,615][INFO ][o.e.e.NodeEnvironment ] [DESKTOP-GIEVO4Q] using [1] data paths, mounts [[杞欢 (D:)]], net usable_space [179.3gb], net total_space [300.7gb], types [NTFS] 4验证通过elasticsearch-plugin list D:\elasticsearch\elasticsearch-7.12.1\binelasticsearch-plugin list
warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [C:\Program Files\Java\jdk1.8.0_271\jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.
ik 3、使用kibana进行测试
启动kibana进入控制台进行测试
查看不同的分词器效果
1ik_smart GET _analyze
{analyzer: ik_smart,text:软件工程师
} 查看运行结果 {tokens : [{token : 软件,start_offset : 0,end_offset : 2,type : CN_WORD,position : 0},{token : 工程师,start_offset : 2,end_offset : 5,type : CN_WORD,position : 1}]
} 2ik_max_word GET _analyze
{analyzer: ik_max_word,text:软件工程师
} 查看运行结果 {tokens : [{token : 软件工程,start_offset : 0,end_offset : 4,type : CN_WORD,position : 0},{token : 软件,start_offset : 0,end_offset : 2,type : CN_WORD,position : 1},{token : 工程师,start_offset : 2,end_offset : 5,type : CN_WORD,position : 2},{token : 工程,start_offset : 2,end_offset : 4,type : CN_WORD,position : 3},{token : 师,start_offset : 4,end_offset : 5,type : CN_CHAR,position : 4}]
} 4、将加入自己需要的词加到分词器的字典中
打开elasticsearch-7.12.1\plugins\ik\config\IKAnalyzer.cfg.xml配置entry keyext_dict/entry内添加字段映射 1配置前 GET _analyze
{analyzer: ik_max_word,text:我的妈
} 查看运行结果 {tokens : [{token : 我,start_offset : 0,end_offset : 1,type : CN_CHAR,position : 0},{token : 的,start_offset : 1,end_offset : 2,type : CN_CHAR,position : 1},{token : 妈,start_offset : 2,end_offset : 3,type : CN_CHAR,position : 2}]
} 2配置 entry keyext_dictmy.dic/entry 同目录下新增my.dic - 我的妈
3配置后重启es控制台 {tokens : [{token : 我的妈,start_offset : 0,end_offset : 3,type : CN_WORD,position : 0}]
} 查看运行结果 {tokens : [{token : 我的妈,start_offset : 0,end_offset : 3,type : CN_WORD,position : 0}]
} 五、RestFul索引基本操作 1、基本Rest命令:
PUT | localhost:9200/索引名称/类型名称/文档id | 创建文档指定文档id
POST | localhost:9200/索引名称/类型名称 | 创建文档随机文档id
POST | localhost:9200/索引名称/类型名称/文档id/_update | 修改文档
DELETE | localhost:9200/索引名称/类型名称/文档id | 删除文档
GET | localhost:9200/索引名称/类型名称/文档id | 查询文档通过文档id
POST | localhost:9200/索引名称/类型名称/文档id/_search | 查询所有文档
2、基础测试-创建索引
创建一个索引PUT /索引名/~类型名~/文档id PUT /test1/type1/1
{name:小白学es,age:3
} 查看响应完成自动增加了索引类似数据库 #! [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{_index : test1,_type : type1,_id : 1,_version : 1,result : created,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 0,_primary_term : 1
} 3、基本数据类型
1字符串类型text、keyword
2数值类型long、integer、short、byte、double、half float、scaled、float
3日期类型date
4te布尔类型boolean
5二进制类型binary
4、基础测试-创建并指定索引类型 PUT /test2
{mappings:{properties: {name:{type:text},age:{type:long},birthday:{type:date}}}
} 查看运行结果 {acknowledged : true,shards_acknowledged : true,index : test2
} 5、 基础测试-创建/查看默认信息
1创建默认索引即文档 PUT /test3/_doc/1
{name:小白学es,age:18,bithday:1997-07-07
} 查看运行结果 {_index : test3,_type : _doc,_id : 1,_version : 1,result : created,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 0,_primary_term : 1
} 插件查看 2查看索引默认配置字段类型 GET test3 运行结果 {test3 : {aliases : { },mappings : {properties : {age : {type : long},bithday : {type : date},name : {type : text,fields : {keyword : {type : keyword,ignore_above : 256}}}}},settings : {index : {routing : {allocation : {include : {_tier_preference : data_content}}},number_of_shards : 1,provided_name : test3,creation_date : 1621064937104,number_of_replicas : 1,uuid : f_-oPx65RwOk846CysvHRQ,version : {created : 7120199}}}}
} 6、扩展查看集群/索引信息
查看集群状态 GET _cat/health 运行结果 1621065190 07:53:10 elasticsearch yellow 1 1 9 9 0 0 2 0 - 81.8% 查看索引版本信息等 GET _cat/indices?v 运行结果 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_7.12.1_001 aGaAAOZTSQyk_0ZU_lOXsg 1 0 54 23 4.2mb 4.2mb
yellow open test2 3f9X5bcAQXKciY56Il5nfA 1 1 0 0 208b 208b
yellow open test3 f_-oPx65RwOk846CysvHRQ 1 1 1 0 4.2kb 4.2kb
green open .apm-custom-link ZyIwiavFSJSFTbh2cMd8Tg 1 0 0 0 208b 208b
green open .apm-agent-configuration FD1qwkXwTTaCT6orvViPRg 1 0 0 0 208b 208b
green open .kibana_task_manager_7.12.1_001 MIUHf-m7S_e8XrjZMduTpA 1 0 9 272 286.7kb 286.7kb
green open .kibana-event-log-7.12.1-000001 qPBR8ea4Th24CnhfxKa3Sg 1 0 6 0 32.6kb 32.6kb
green open .tasks uwcYsws9QkqK1CXKQiNI0Q 1 0 10 0 50kb 50kb 7、基础测试-修改索引/文档
1直接覆盖PUT PUT /test3/_doc/1
{name:小白学es2,age:18,bithday:1997-07-07
} 运行结果version1result为updated {_index : test3,_type : _doc,_id : 1,_version : 2,result : updated,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 1,_primary_term : 1
} 插件查看结果 2update更新POST POST /test3/_doc/1/_update
{doc:{name:小白学es3}
} 运行结果version1result为updated #! [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
{_index : test3,_type : _doc,_id : 1,_version : 3,result : updated,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 2,_primary_term : 1
} 插件查看结果 8、基础测试-删除索引
1删除索引 DELETE test1 查看运行结果 {acknowledged : true
} 2删除索引中文档 DELETE test3/_doc/1 查看运行结果 {_index : test3,_type : _doc,_id : 1,_version : 4,result : deleted,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 3,_primary_term : 1
} 六、文档CRUD(增删改查)
1、添加数据PUT PUT /xiaobai/user/1
{name:小白,age:30,desc:一顿操作猛如虎一看工资2500,tags:[技术宅,直男]
} 查看运行结果 #! [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{_index : xiaobai,_type : user,_id : 1,_version : 1,result : created,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 0,_primary_term : 1
} 插件查看 同样的方法添加一些数据 2、获取数据GET GET xiaobai/user/1 查看运行结果 #! [types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{_index : xiaobai,_type : user,_id : 1,_version : 1,_seq_no : 0,_primary_term : 1,found : true,_source : {name : 小白,age : 30,desc : 一顿操作猛如虎一看工资2500,tags : [技术宅,直男]}
} 3、更新数据PUT/POST
(1)PUT修改全量字段更新
1号数据name小白 - 白神 PUT /xiaobai/user/1
{name:白神,age:30,desc:一顿操作猛如虎一看工资2500,tags:[技术宅,直男]
} 查看运行结果 #! [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{_index : xiaobai,_type : user,_id : 1,_version : 2,result : updated,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 5,_primary_term : 2
} 2POST _update指定字段更新推荐使用 POST /xiaobai/user/1/_update
{doc:{name:小白java}
} 查看运行结果 #! [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
{_index : xiaobai,_type : user,_id : 1,_version : 5,result : updated,_shards : {total : 2,successful : 1,failed : 0},_seq_no : 8,_primary_term : 2
} 4、简单的搜索GET _searcher
text类型可以根据字段分词搜索keyword关键词不会处理分词器 GET /xiaobai/user/_search?qname:小白 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 32,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 3,relation : eq},max_score : 1.9252907,hits : [{_index : xiaobai,_type : user,_id : 1,_score : 1.9252907,_source : {name : 小白,age : 30,desc : 一顿操作猛如虎一看工资2500,tags : [技术宅,直男]}},{_index : xiaobai,_type : user,_id : 2,_score : 0.53899646,_source : {name : 小黑,age : 32,desc : 腹黑男猪脚,tags : [旅游,渣男,游戏宅]}},{_index : xiaobai,_type : user,_id : 5,_score : 0.53899646,_source : {name : 小王,age : 32,desc : 社会性死亡,tags : [技术宅,暖男,中年危机]}}]}
} 5、复杂操作搜索 _searcher match - 分词模糊匹配 {query:{match: {name: 钻石王老五}}
} 查看运行结果 {took: 2,timed_out: false,_shards: {total: 1,successful: 1,skipped: 0,failed: 0},hits: {total: {value: 4,relation: eq},max_score: 3.0311832,hits: [{_index: xiaobai,_type: user,_id: 6,_score: 3.0311832,_source: {name: 钻石王老五,age: 32,desc: 社会性死亡,tags: [技术宅,暖男,中年危机]}},{_index: xiaobai,_type: user,_id: 4,_score: 1.9277248,_source: {name: 王五,age: 30,desc: 正宗青子,tags: [青年,吃货,旅游,胖子]}},{_index: xiaobai,_type: user,_id: 7,_score: 1.6832076,_source: {name: 钻石李老四,age: 32,desc: 社会性死亡,tags: [技术宅,暖男,中年危机]}},{_index: xiaobai,_type: user,_id: 5,_score: 1.2623059,_source: {name: 钻石123,age: 32,desc: 社会性死亡,tags: [技术宅,暖男,中年危机]}}]}
} 6、复杂操作搜索 _searcher match_phrase - 完整模糊匹配 GET /xiaobai/user/_search
{query:{match_phrase: {name: 钻石王老五}}
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 0,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 1,relation : eq},max_score : 3.0311837,hits : [{_index : xiaobai,_type : user,_id : 6,_score : 3.0311837,_source : {name : 钻石王老五,age : 32,desc : 社会性死亡,tags : [技术宅,暖男,中年危机]}}]}
} 7、复杂操作搜索 _searcher term - 完成精确匹配 GET xiaobai/user/_search
{query:{term: {name.keyword: 钻石王老五}}
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 0,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 1,relation : eq},max_score : 1.2039728,hits : [{_index : xiaobai,_type : user,_id : 3,_score : 1.2039728,_source : {name : 钻石王老五,age : 19,desc : 社会性死亡,tags : [技术宅,暖男]}}]}
} 8、复杂操作搜索 _searcher _source - 过滤展示结果 GET /xiaobai/user/_search
{query:{match_phrase: {name: 钻石王老五}},_source:[name,desc]
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 11,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 1,relation : eq},max_score : 3.0311837,hits : [{_index : xiaobai,_type : user,_id : 6,_score : 3.0311837,_source : {name : 钻石王老五,desc : 社会性死亡}}]}
} 9、复杂操作搜索 _searcher sort - 排序desc/asc GET xiaobai/user/_search
{query:{match_phrase: {name: 钻石}},sort:[{age:{order:desc}}]
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{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 : xiaobai,_type : user,_id : 2,_score : null,_source : {name : 钻石李老四,age : 32,desc : 社会性死亡,tags : [技术宅,暖男,中年危机]},sort : [32]},{_index : xiaobai,_type : user,_id : 4,_score : null,_source : {name : 钻石123,age : 28,desc : 废物一个,tags : [技术宅]},sort : [28]},{_index : xiaobai,_type : user,_id : 3,_score : null,_source : {name : 钻石王老五,age : 19,desc : 社会性死亡,tags : [技术宅,暖男]},sort : [19]}]}
} 10、复杂操作搜索 _searcher from size - 分页查询 GET xiaobai/user/_search
{query:{match_phrase: {name: 钻石}},from:0,size:2
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 0,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 3,relation : eq},max_score : 0.77691567,hits : [{_index : xiaobai,_type : user,_id : 4,_score : 0.77691567,_source : {name : 钻石123,age : 28,desc : 废物一个,tags : [技术宅]}},{_index : xiaobai,_type : user,_id : 2,_score : 0.62774795,_source : {name : 钻石李老四,age : 32,desc : 社会性死亡,tags : [技术宅,暖男,中年危机]}}]}
} 11、复杂操作搜索 _searcher bool - 联合查询 -must - 所有条件均符合 GET xiaobai/user/_search
{query:{bool: {must:[{match:{name:钻石王老五}},{match:{age:28}}]}}
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 1,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 1,relation : eq},max_score : 1.7769157,hits : [{_index : xiaobai,_type : user,_id : 4,_score : 1.7769157,_source : {name : 钻石123,age : 28,desc : 废物一个,tags : [技术宅]}}]}
} 12、复杂操作搜索_searcher bool - 联合查询 - should - 满足任意一个条件 GET xiaobai/user/_search
{query:{bool: {should:[{match:{name:小白}},{match:{age:28}}]}}
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 1,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 2,relation : eq},max_score : 2.9761126,hits : [{_index : xiaobai,_type : user,_id : 1,_score : 2.9761126,_source : {name : 小白,age : 30,desc : 一顿操作猛如虎一看工资2500,tags : [技术宅,直男]}},{_index : xiaobai,_type : user,_id : 4,_score : 1.0,_source : {name : 钻石123,age : 28,desc : 废物一个,tags : [技术宅]}}]}
} 13、复杂操作搜索_searcher bool - 联合查询 - must_not - 不满足 GET xiaobai/user/_search
{query:{bool: {must_not:[{match:{name:小白}},{match:{age:28}}]}}
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 1,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 2,relation : eq},max_score : 0.0,hits : [{_index : xiaobai,_type : user,_id : 2,_score : 0.0,_source : {name : 钻石李老四,age : 32,desc : 社会性死亡,tags : [技术宅,暖男,中年危机]}},{_index : xiaobai,_type : user,_id : 3,_score : 0.0,_source : {name : 钻石王老五,age : 19,desc : 社会性死亡,tags : [技术宅,暖男]}}]}
} 14、复杂操作搜索_searcher bool - 联合查询 - filter - 过滤
gt() gte() lt() lte() GET xiaobai/user/_search
{query:{bool: {must:[{match:{name:钻石}}],filter: {range: {age: {gte: 10,lte: 30}}}
}} }
查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 1,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 2,relation : eq},max_score : 0.77691567,hits : [{_index : xiaobai,_type : user,_id : 4,_score : 0.77691567,_source : {name : 钻石123,age : 28,desc : 废物一个,tags : [技术宅]}},{_index : xiaobai,_type : user,_id : 3,_score : 0.62774795,_source : {name : 钻石王老五,age : 19,desc : 社会性死亡,tags : [技术宅,暖男]}}]}
} 15、复杂操作搜索_searcher match - 匹配多个标签分词空格隔开 GET xiaobai/user/_search
{query:{match: {tags:男 技术宅}}
} 查看运行结果 #! [types removal] Specifying types in search requests is deprecated.
{took : 1,timed_out : false,_shards : {total : 1,successful : 1,skipped : 0,failed : 0},hits : {total : {value : 4,relation : eq},max_score : 0.6987428,hits : [{_index : xiaobai,_type : user,_id : 1,_score : 0.6987428,_source : {name : 小白,age : 30,desc : 一顿操作猛如虎一看工资2500,tags : [技术宅,直男]}},{_index : xiaobai,_type : user,_id : 3,_score : 0.6987428,_source : {name : 钻石王老五,age : 19,desc : 社会性死亡,tags : [技术宅,暖男]}},{_index : xiaobai,_type : user,_id : 2,_score : 0.5337937,_source : {name : 钻石李老四,age : 32,desc : 社会性死亡,tags : [技术宅,暖男,中年危机]}},{_index : xiaobai,_type : user,_id : 4,_score : 0.38828292,