最后
关于面试刷题也是有方法可言的,建议最好是按照专题来进行,然后由基础到高级,由浅入深来,效果会更好。当然,这些内容我也全部整理在一份pdf文档内,分成了以下几大专题:
- Java基础部分
- 算法与编程
- 数据库部分
- 流行的框架与新技术(Spring+SpringCloud+SpringCloudAlibaba)
这份面试文档当然不止这些内容,实际上像JVM、设计模式、ZK、MQ、数据结构等其他部分的面试内容均有涉及,因为文章篇幅,就不全部在这里阐述了。
作为一名程序员,阶段性的学习是必不可少的,而且需要保持一定的持续性,这次在这个阶段内,我对一些重点的知识点进行了系统的复习,一方面巩固了自己的基础,另一方面也提升了自己的知识广度和深度。
穷尽词库的可能, 字典
!
- ik分词器增加自己的配置与字典
plugins\ik\config
下
- 重启ES 和 Kibana
-
基础测试
-
创建一个索引
PUT /索引名/类型名/文档id
{请求体}
指令
put /test/achang/1
{
“name”:“achang”,
“age”:18,
“address”:“中国”
}
结果
{
“_index” : “test”,# 索引
“_type” : “achang”,# 类型(已经废弃)
“_id” : “1”,# id
“_version” : 1,# 版本
“result” : “created”,# 操作类型
“_shards” : {# 分片信息
“total” : 2,
“successful” : 1,
“failed” : 0
},
“_seq_no” : 0,
“_primary_term” : 1
}
- es字段类型
- 设置索引库的参数类型,创建规则
- 获取具体的索引规则
GET test2
{
“test2” : {
“aliases” : { },
“mappings” : {
“properties” : {
“age” : {
“type” : “integer”
},
“birthday” : {
“type” : “date”
},
“name” : {
“type” : “text”
}
}
},
“settings” : {
“index” : {
“creation_date” : “1599708623941”,
“number_of_shards” : “1”,
“number_of_replicas” : “1”,
“uuid” : “ANWnhwArSMSl8k8iipgH1Q”,
“version” : {
“created” : “7080099”
},
“provided_name” : “test2”
}
}
}
}
查看默认的规则
PUT /test3/_doc/1
{
“name”: “狂神说Java”,
“age”: 28,
“birthday”: “1997-01-05”
}
GET test3
{
“test3” : {
“aliases” : { },
“mappings” : {
“properties” : {
“age” : {
“type” : “long”
},
“birthday” : {
“type” : “date”
},
“name” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
}
}
},
“settings” : {
“index” : {
“creation_date” : “1599708906181”,
“number_of_shards” : “1”,
“number_of_replicas” : “1”,
“uuid” : “LzPLCDgeQn6tdKo3xBBpbw”,
“version” : {
“created” : “7080099”
},
“provided_name” : “test3”
}
}
}
}
- 修改索引 POST
只会修改指定项,其他内容保证不变
#post /test/achang/1
{
“name”:“achang!!!”,
“age”:20,
“address”:“中国”
}
#get /test/_doc/1
{
“_index” : “test”,
“_type” : “achang”,
“_id” : “1”,
“_version” : 2,#版本号加1,乐观锁
“_seq_no” : 1,
“_primary_term” : 1,
“found” : true,
“_source” : {
“name” : “achang!!!”,
“age” : 20,
“address” : “中国”
}
}
- 删除索引
- 基本操作(简单的查询)
put /achang/user/1
{
“name”: “阿昌”,
“age”: 22,
“desc”: “一顿操作猛如虎,一看工资2500”,
“tags”: [“码农”, “技术宅”, “直男”]
}
put /achang/user/2
{
“name”: “张三”,
“age”: 28,
“desc”: “法外狂徒”,
“tags”: [“旅游”, “渣男”, “交友”]
}
put /achang/user/3
{
“name”: “李四1”,
“age”: 30,
“desc”: “不知道怎么描述”,
“tags”: [“旅游”, “靓女”, “唱歌”]
}
GET achang/user/1
GET achang/user/_search?q=name:阿昌
- 复杂操作(排序、分页、高亮、模糊查询、标准查询!)
模糊查询
GET achang/user/_search
{
“query”: {
“match”: {#模糊匹配
“name”: “阿”
}
}
}
对查询结果进行字段过滤
GET achang/user/_search
{
“query”: {
“match”: {
“name”: “阿昌”
}
},
“_source”: [“name”, “desc”]#代表你只要name、desc的值
}
排序
GET achang/user/_search
{
“query”: {
“match”: {
“name”: “阿昌”
}
},
“sort”:[{
“age”: “asc”# 指定匹配规则desc、asc
}]
}
分页
GET achang/user/_search
{
“query”: {
“match”: {
“name”: “阿昌” #模糊匹配条件
}
},
“sort”:[{
“age”: “asc” #排序规则
}],
“from”: 0,#从第几个开始
“size”: 2 #显示数量
}
- 布尔值条件查询
多条件查询 must 相当于and
GET achang/user/_search
{
“query”: {
“bool”: {
“must”: [#条件数组,模糊匹配
{“match”: {
“name”: “阿昌” #条件1
}},
{“match”: {
“age”: 22 #条件2
}}
]
}
}
}
多条件查询 should 相当于or
GET achang/user/_search
{
“query”: {
“bool”: {
“should”: [# or关系
{“match”: {
“name”: “阿昌” #条件1
}},
{“match”: {
“age”: 25 #条件2
}}
]
}
}
}
多条件查询 must_not 相当于 not
GET achang/user/_search
{
“query”: {
“bool”: {
“must_not”: [ # not关系
{“match”: {
“age”: 25 #条件,age不等于25的数据
}}
]
}
}
}
过滤查询1 age > 24
GET achang/user/_search
{
“query”: {
“bool”: {
“must”: [ # 模糊匹配
{“match”: {
“name”: “1” #name含有1的
}}
],
“filter”: [ # 过滤条件
{“range”: { #范围
“age”: { #age大于24的
“gt”: 24
}
}}
]
}
}
}
过滤器2 22<age<30
GET achang/user/_search
{
“query”: {
“bool”: {
“must”: [
{“match”: {
“name”: “阿昌”
}}
],
“filter”: [ #过滤规则
{“range”: { #范围
“age”: { #年龄
“lt”: 30, #小于30
“gt”: 21 #大于21
}
}}
]
}
}
}
- 多条件查询
多条件用空格分开,数据只要满足一个条件就会被查出
GET achang/user/_search
{
“query”: { #模糊匹配
“match”: { #匹配规则
“tags”: “技术 男” #tags含有技术、男
}
}
}
- 精准查询
keyword
类型不会被分词器解析
term
: 精确匹配 倒排索引
定义类型
PUT xiaoachang_test_db
{
“mappings”: { #映射
“properties”: { #字段属性
“name”: {
“type”: “keyword”
},
“desc”: {
“type”: “text”
}
}
}
}
PUT /xiaoachang_test_db/_doc/1
{
“name”: “阿昌Java Name”,
“desc”: “阿昌 Desc”
}
PUT /xiaoachang_test_db/_doc/2
{
“name”: “阿昌Java Name”,
“desc”: “阿昌Java Desc 2”
}
按照keyword类型精准匹配
GET xiaoachang_test_db/_search
{
“query”: {
“term”: { #倒排索引查询
“name”: “阿昌Java Name”
}
}
}
结果:
{
“took” : 0,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 0.6931471,
“hits” : [
{
“_index” : “test_db”,
“_type” : “_doc”,
“_id” : “1”,
“_score” : 0.6931471,
“_source” : {
“name” : “阿昌Java Name”,
“desc” : “阿昌Java Desc”
}
}
]
}
}
按照text类型匹配
GET xiaoachang_test_db/_search
{
“query”: {
“term”: { #倒排索引查询
“name”: “小”
}
}
}
结果:
{
“took” : 0,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 2,
“relation” : “eq”
},
“max_score” : 0.18232156,
“hits” : [
{
“_index” : “test_db”,
“_type” : “_doc”,
“_id” : “1”,
“_score” : 0.18232156,
“_source” : {
“name” : “阿昌Java Name”,
“desc” : “阿昌Java Desc”
}
},
{
“_index” : “test_db”,
“_type” : “_doc”,
“_id” : “2”,
“_score” : 0.18232156,
“_source” : {
“name” : “阿昌Java Name”,
“desc” : “阿昌Java Desc 2”
}
}
]
}
}
- 多个值匹配精确查询
PUT /test_db/_doc/3
{
“t1”: “22”,
“t2”: “2020-09-10”
}
PUT /test_db/_doc/4
{
“t1”: “33”,
“t2”: “2020-09-11”
}
GET test_db/_search
{
“query”: {
“bool”: { #布尔判断
“should”: [ #or关系
{
“term”: {
“t1”: “22” #精确判断,t1等于22
}
},
{
“term”: {
“t1”: “33” #或t1等于33
}
}
]
}
}
}
- 高亮查询
GET achang/user/_search
{
“query”: {
“match”: { #模糊查询
“name”: “阿昌”
}
},
“highlight”: { #高亮查询
“pre_tags”: “
”, #前缀,左侧的key为css样式
“post_tags”: “
”, #后缀“fields”: { #设置需要高亮的字段
“name”: {}
}
}
}
结果显示:
{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 2.0834165,
“hits” : [
{
“_index” : “achang”,
“_type” : “user”,
“_id” : “1”,
“_score” : 2.0834165,
“_source” : {
“name” : “阿昌”,
“age” : 22,
“desc” : “一顿操作猛如虎,一看工资2500”,
“tags” : [
“码农”,
“技术宅”,
“直男”
]
},
“highlight” : {
“name” : [
“
阿
昌
” #需要高亮的内容被加上了em标签,这里加上了我们上面自定义的内容标签]
}
}
]
}
}
-
添加依赖
org.springframework.boot
spring-boot-starter-data-elasticsearch
springboot2.5.4版本默认引入的es版本是7.12.1
我们需要自定义es的版本,让他跟我们使用的版本是一致的
- 自定义es版本
<java.version>1.8</java.version>
<repackage.classifier/>
<spring-native.version>0.10.3</spring-native.version>
<elasticsearch.version>7.14.1</elasticsearch.version> #自定义版本依赖
ElasticsearchRestClientAutoConfiguration自动配置类中导入了三个类
RestClientBuilderConfiguration、RestHighLevelClientConfiguration、RestClientSnifferConfiguration,
他们都是ElasticsearchRestClientConfigurations中的内部类
- 自定义配置
//es配置类
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
//如果是集群就构建多个
new HttpHost(“localhost”, 9200, “http”)//ip,端口,协议
)
);
return client;
}
}
-
编写测试类
-
创建索引
//创建索引
@Test
void contextLoads() throws IOException {
//1、创建索引请求
CreateIndexRequest request = new CreateIndexRequest(“achang_index”);
//2、通过客户端执行请求,获得响应
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);//org.elasticsearch.client.indices.CreateIndexResponse@7e130706
}
- 索引存在
//索引是否存在
@Test
void test() throws IOException {
//获取索引请求
GetIndexRequest request = new GetIndexRequest(“achang_index”);
boolean isExist = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(isExist);//true|false
}
- 删除索引
//删除索引
@Test
void test1() throws IOException {
//删除索引请求
DeleteIndexRequest request = new DeleteIndexRequest(“achang_index”);
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response);
}
- 添加文档
*//添加文档
@Test
void test2() throws IOException {
User user = new User(“阿昌”,18);
IndexRequest request = new IndexRequest(“achang_index”);
// 规则 PUT /index/_doc/1
request.id(“1”);
request.timeout(TimeValue.timeValueSeconds(1));//超时时间1秒
//将数据序列化
request.source(JSON.toJSON(user), XContentType.JSON);//放入资源,设置请求格式为JSON
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.status());
System.out.println(response.toString());
}
- 判断文档是否存在
//判断文档是否存在
@Test
void test3() throws IOException {
GetRequest request = new GetRequest(“achang_index”, “1”);
request.fetchSourceContext(new FetchSourceContext(false));//不获取返回的 _source的上下文
request.storedFields(“none”);
boolean isExist = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(isExist);
}
- 获取文档
//获取文档
@Test
void test4() throws IOException {
我的面试宝典:一线互联网大厂Java核心面试题库
以下是我个人的一些做法,希望可以给各位提供一些帮助:
整理了很长一段时间,拿来复习面试刷题非常合适,其中包括了Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等,且还会持续的更新…可star一下!
283页的Java进阶核心pdf文档
Java部分:Java基础,集合,并发,多线程,JVM,设计模式
数据结构算法:Java算法,数据结构
开源框架部分:Spring,MyBatis,MVC,netty,tomcat
分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等
微服务部分:SpringBoot,SpringCloud,Dubbo,Docker
还有源码相关的阅读学习
request.timeout(TimeValue.timeValueSeconds(1));//超时时间1秒
//将数据序列化
request.source(JSON.toJSON(user), XContentType.JSON);//放入资源,设置请求格式为JSON
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.status());
System.out.println(response.toString());
}
- 判断文档是否存在
//判断文档是否存在
@Test
void test3() throws IOException {
GetRequest request = new GetRequest(“achang_index”, “1”);
request.fetchSourceContext(new FetchSourceContext(false));//不获取返回的 _source的上下文
request.storedFields(“none”);
boolean isExist = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(isExist);
}
- 获取文档
//获取文档
@Test
void test4() throws IOException {
我的面试宝典:一线互联网大厂Java核心面试题库
以下是我个人的一些做法,希望可以给各位提供一些帮助:
整理了很长一段时间,拿来复习面试刷题非常合适,其中包括了Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等,且还会持续的更新…可star一下!
[外链图片转存中…(img-lUYMtkvT-1715625467640)]
283页的Java进阶核心pdf文档
Java部分:Java基础,集合,并发,多线程,JVM,设计模式
数据结构算法:Java算法,数据结构
开源框架部分:Spring,MyBatis,MVC,netty,tomcat
分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等
微服务部分:SpringBoot,SpringCloud,Dubbo,Docker
[外链图片转存中…(img-REQ6OBnt-1715625467641)]
还有源码相关的阅读学习
[外链图片转存中…(img-WAWsmBmc-1715625467641)]