首页 > 其他分享 >Springboot整合es

Springboot整合es

时间:2022-10-09 15:05:26浏览次数:73  
标签:Springboot searchRequest boot sourceBuilder httpClientBuilder ES 整合 new es


参考文章链接

​SpringBoot操作ES,实现各种骚操作查询 - 知乎​

二、整合方式

boot整合es三种方式

SpringBoot整合ES的三种方式(API、REST Client、Data-ES)_jacksonary的博客-CSDN博客_springboot集成es三种方式

三、整合

1、引入依赖


//注意版本一致 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>


2、配置类

@Configuration
public class ESConfiguration {
@Bean
public RestHighLevelClient elasticsearchClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));


return new RestHighLevelClient(RestClient.builder(
//使用公网地址 new HttpHost("www.xxx.com", 9200,"https")
new HttpHost("localhost", 9200)
).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}));
}
}

3、使用

//搜索条件
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

//分页
sourceBuilder.from(pageNo);
sourceBuilder.size(pageSize);//聚合时都默认返回10条数据,我们可以通过指定size值来修改这个默认值

//查询条件构造 调用sourceBuilder相关api设置即

//指定字段查询
sourceBuilder.fetchSource(include,exclude);//可以是数组

//超时设置
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

//查询
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

//结果
SearchHit[] searchHits = searchResponse.getHits().getHits();
List<Map<String, Object>> maps = new ArrayList<>();
for (SearchHit hit : searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
maps.add(sourceAsMap);
}

备注

size的大小不能超过index.max_result_window这个参数的设置,默认为10,000可以通过下面语句设置


PUT /indexName/_settings
{
"index": {
"max_result_window": "10000000"
}
}

设置完查看
GET /indexName/_settings


标签:Springboot,searchRequest,boot,sourceBuilder,httpClientBuilder,ES,整合,new,es
From: https://blog.51cto.com/u_11334685/5740468

相关文章

  • es索引、类型(mapping)、文档、ik分词器
    一、概念1、初学可以把es看作数据库可以建立索引(库)文档(库中的数据)2、es是面向文档的,一切都是json3、物理设计es后台把每个索引划分成多个分片,每份分片可以在集群中的不同......
  • node+express搭建服务器环境
    一、概述express是一个基于​​Node.js​​平台,快速、开放、极简的Web开发框架,网址​​Express-基于Node.js平台的web应用开发框架-Express中文文档|Express......
  • 一文搞懂springboot启动原理
    https://cloud.tencent.com/developer/article/1449134温馨提示如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。关注公众号回复关键字领取免费学习资源-电......
  • ES Restful操作
    一、restful风格介绍二、索引增删改查1、增加索引命令:put索引名称/文档类型/文档idPUTtest/user/1{"name":"张三","age":12,"dec":"测试一下"}字段也是可以自己......
  • 「题解」Codeforces 1572B Xor of 3
    我知道你很急,但你先别急。我急了我急了我急了。如果直接上去莽构造的话,很可能就是像我一样大分讨的后果。先特判掉全是1,或者1的个数是奇数的情况。我的做法是考虑所......
  • Python非root用户启动python multiprocessing的semlock,提示没有权限的解决方法
    使用进程间通信的时候Python报错为<spanstyle="font-size:18px;">Traceback(mostrecentcalllast):File"web_game_sign.py",line483,in<module>count=mu......
  • SpringBoot 后台服务端 杂记
    由于公司人手不足,导致桌面应用、微信小程序以及两端对应的服务端都由我自己开发。为了加快开发进度,采用SpringBoot+SpringSecurity+JWT的方式(桌面应用的服务端,微信小......
  • SpringBoot 下载文件
    下载文件  SpringBoot接口输出文件流&Vue下载文件流,获取Header中的文件名@SpringBootTestclassDownloadTests{Loggerlogger=LoggerFactory.getLogger......
  • springboot中对各个层的理解
    springboot中对各个层的理解1、entity层:实体层,数据库在项目中的类。也被称为model层,pojo层。用于定义与数据库对象的属性,提供get/set方法,带参和无参的构造方法。一般数......
  • kubernetes Tcp流量可视化
    kubernetesTcp流量可视化使用k8spacket和grafana的nodegraph插件可以查看kubernetespod的TCP相关信息,如connection、bytes、和duration。下面是接收和响应的字节数信息......