注意:满篇全是截图也不好看,以下就不截图了,望谅解。
(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”))
);
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index(“work”).id(“1001”).source(XContentType.JSON, “name”, “csdn哪吒”));
request.add(new IndexRequest().index(“work”).id(“1002”).source(XContentType.JSON, “name”, “csdn哪吒1”));
request.add(new IndexRequest().index(“work”).id(“1003”).source(XContentType.JSON, “name”, “csdn哪吒2”));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
// 关闭es客户端
esClient.close();
}
7、根据条件查询文档中全部数据
/**
- 查询索引中全部数据
*/
private static void getDoc() throws Exception{
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
//查询索引中全部数据
SearchRequest request = new SearchRequest();
request.indices(“work”);
//构造查询条件,匹配所有
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
request.source(query);
//查询文档
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//查询的结果
SearchHits hits = response.getHits();
//查询条数
System.out.println(hits.getTotalHits());
//查询时间
System.out.println(response.getTook());
//查询的具体数据
for (SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
// 关闭es客户端
esClient.close();
}
控制台输出
8、按条件查询
SearchSourceBuilder query = new SearchSourceBuilder().
query(QueryBuilders.termQuery(“name”,“csdn哪吒”));
9、分页查询
//构造查询条件,分页查询
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//起始页
builder.from(0);
//每页数据量
builder.size(2);
request.source(builder);
10、按年龄排序
//按照年龄排序
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
builder.sort(“age”, SortOrder.DESC);
request.source(builder);
控制台输出
11、按条件查询
12、组合条件查询
13、模糊查询
//差一个字符也能匹配
SearchSourceBuilder builder = new SearchSourceBuilder();
FuzzyQueryBuilder fuzziness = QueryBuilders.
fuzzyQuery(“name”, “哪吒”).fuzziness(Fuzziness.ONE);
builder.query(fuzziness);
request.source(builder);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)
笔者福利
以下是小编自己针对马上即将到来的金九银十准备的一套“面试宝典”,不管是技术还是HR的问题都有针对性的回答。
有了这个,面试踩雷?不存在的!
回馈粉丝,诚意满满!!!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
FuzzyQueryBuilder fuzziness = QueryBuilders.
fuzzyQuery(“name”, “哪吒”).fuzziness(Fuzziness.ONE);
builder.query(fuzziness);
request.source(builder);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-anNRWS48-1713839053737)]
[外链图片转存中…(img-J1ivgSYN-1713839053738)]
[外链图片转存中…(img-UBY987dA-1713839053738)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)
[外链图片转存中…(img-KTkDXp7A-1713839053738)]
笔者福利
以下是小编自己针对马上即将到来的金九银十准备的一套“面试宝典”,不管是技术还是HR的问题都有针对性的回答。
有了这个,面试踩雷?不存在的!
回馈粉丝,诚意满满!!!
[外链图片转存中…(img-sfJnEtum-1713839053739)]
[外链图片转存中…(img-PD7CJyRi-1713839053739)]
[外链图片转存中…(img-ViW0PtSp-1713839053739)]
[外链图片转存中…(img-9odTYo86-1713839053739)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!