《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
3、双击elasticsearch.bat启动
4、启动成功
三、数据格式
Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档。为了方便大家理解,我们将Elasticsearch里存储文档数据和关系型数据库Mysql存储数据的概念进行一个类比:
ES里的Index可以看做一个库,而Types相当于表,Documents则相当于表的行。
Elasticsearch7.X中,Type的概念已经被删除了。
四、索引
1、创建索引
在postman中,向ES服务器发送PUT请求:127.0.0.1:9200/work
由于PUT请求具有幂等性,每次PUT请求创建的结果都是一样的,再次请求时,由于ES中已经存在名为work的索引了,所以会创建失败。
POST是不具有幂等性的,所以POST请求后,结果可能不一样,所以添加索引的时候是不允许使用POST请求的。
什么是幂等性?
在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。幂等函数,或幂等方法,是指可以使用相同参数重复执行,并能获得相同结果的函数。这些函数不会影响系统状态,也不用担心重复执行会对系统造成改变。
2、查询索引
(1)通过GET请求可以获取单一索引
(2)获取全部索引信息
127.0.0.1:9200/_cat/indices?v
3、删除索引
五、文档
1、创建文档
ES中的文档相当于MySQL中的表数据,数据格式为JSON格式。
由于文档生成时会自动创建一个唯一性标识,因为POST不是幂等性的,PUT是幂等性的,所以这里只能用POST。
可以指定id
2、查询文档
(1)根据id查询
(2)查询所有文档
127.0.0.1:9200/work/_search
3、更改文档内容
(1)修改文档id=1001的内容,恭喜哪吒大佬被评选为“2021博客之星TOP10”,锣鼓喧天,鞭炮旗鼓。
(2)局部更新
(3)局部更新成功,恭喜哪吒成功晋升TOP5。
六、复杂查询
1、指定条件查询
(1)查询name为哪吒的索引(通过请求路径:127.0.0.1:9200/work/_search?q=name:哪吒)
注意:满篇全是截图也不好看,以下就不截图了,望谅解。
(2)请求体查询
get请求:127.0.0.1:9200/work/_search
请求体:
{
“query”:{
“match”:{
“name”:“哪吒”
}
}
}
(3)分页查询
get请求:127.0.0.1:9200/work/_search
请求体:
{
“query”:{
“match_all”:{
}
} ,
“from”:0,
“size”:2
}
(4)只获取指定字段 and 根据id排序
{
“query”:{
“match_all”:{
}
} ,
“from”:0,
“size”:2,
“_source”:[“title”],
“sort”:{
“_id”:“desc”
}
}
2、多条件查询
must表示and匹配
{
“query”:{
“bool”:{
“must”:[
{
“match”:{
“name”:“哪吒”
}
},{
“match”:{
“title”:“博客专家”
}
}
]
}
}
}
should表示or匹配
{
“query”:{
“bool”:{
“should”:[
{
“match”:{
“name”:“哪吒”
}
},{
“match”:{
“name”:“CSDN”
}
}
]
}
}
}
范围匹配:工资大于10000
{
“query”:{
“bool”:{
“should”:[
{
“match”:{
“name”:“哪吒”
}
},{
“match”:{
“name”:“CSDN”
}
}
],
“filter”:{
“range”:{
“money”:10000
}
}
}
}
}
3、部分词汇匹配查询
将每一个值拆解,组成倒排索引,方便进行全文检索。
{
“query”:{
“match”:{
“name”:“哪”
}
}
}
完全匹配
{
“query”:{
“match_phrase”:{
“name”:“哪”
}
}
}
高亮显示
{
“query”:{
“match”:{
“name”:“哪”
}
},
“highlight”:{
“fields”:{
“name”:{}
}
}
}
4、聚合查询
(1)分组查询
{
“aggs”:{
“money_group”:{
“terms”:{
“field”:“money”
}
}
},
“size”:0
}
(2)平均值查询
{
“aggs”:{
“money_avg”:{
“avg”:{
“field”:“money”
}
}
},
“size”:0
}
七、代码实例
1、引入pom
<?xml version="1.0" encoding="UTF-8"?><project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
com.guor
es
0.0.1-SNAPSHOT
es
Demo project for Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.elasticsearch
elasticsearch
7.8.0
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.8.0
org.elasticsearch.client
elasticsearch-rest-client
org.elasticsearch.client
elasticsearch-rest-client
7.8.0
org.apache.logging.log4j
log4j-api
2.8.2
org.apache.logging.log4j
log4j-core
2.8.2
com.fasterxml.jackson.core
jackson-databind
2.9.9
org.projectlombok
lombok
true
junit
junit
4.12
org.springframework.boot
spring-boot-maven-plugin
2、添加索引
package com.guor.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
public class ESTest {
public static void main(String[] args) throws Exception{
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
//创建索引
CreateIndexRequest request = new CreateIndexRequest(“student”);
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(“创建索引:”+acknowledged);
// 关闭es客户端
esClient.close();
}
}
3、运行出错
解决方法:
将pom中
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.8.0
替换为:
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.8.0
org.elasticsearch.client
elasticsearch-rest-client
org.elasticsearch.client
elasticsearch-rest-client
7.8.0
4、查询索引
package com.guor.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
public class ESTest {
public static void main(String[] args) throws Exception{
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
//创建索引
GetIndexRequest request = new GetIndexRequest(“student”);
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
// 关闭es客户端
esClient.close();
}
}
5、插入文档
package com.guor.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
public class ESTestDoc {
public static void main(String[] args) throws Exception{
addDoc();
}
private static void addDoc() throws Exception{
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
IndexRequest request = new IndexRequest();
request.index(“work”).id(“1001”);
Work work = new Work();
work.setName(“哪吒”);
work.setAge(28);
work.setSex(0);
ObjectMapper mapper = new ObjectMapper();
String workJson = mapper.writeValueAsString(work);
IndexRequest response = request.source(workJson, XContentType.JSON);
System.out.println(response.getShouldStoreResult());
esClient.index(request, RequestOptions.DEFAULT);
// 关闭es客户端
esClient.close();
}
}
6、批量插入文档
private static void addBatchDoc() throws Exception{
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。实际上,作为程序员,丰富自己的知识储备,提升自己的知识深度和广度是很有必要的。
Mybatis源码解析
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
work = new Work();
work.setName(“哪吒”);
work.setAge(28);
work.setSex(0);
ObjectMapper mapper = new ObjectMapper();
String workJson = mapper.writeValueAsString(work);
IndexRequest response = request.source(workJson, XContentType.JSON);
System.out.println(response.getShouldStoreResult());
esClient.index(request, RequestOptions.DEFAULT);
// 关闭es客户端
esClient.close();
}
}
6、批量插入文档
private static void addBatchDoc() throws Exception{
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
总结
我们总是喜欢瞻仰大厂的大神们,但实际上大神也不过凡人,与菜鸟程序员相比,也就多花了几分心思,如果你再不努力,差距也只会越来越大。实际上,作为程序员,丰富自己的知识储备,提升自己的知识深度和广度是很有必要的。
Mybatis源码解析
[外链图片转存中…(img-dZu2Hm5h-1714475639766)]
[外链图片转存中…(img-1LsQ3QfU-1714475639766)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!