1.引入依赖
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.4.2
springboot自动引入的依赖可能版本不同,需要自己指定版本
文章图片
2.配置类 es配置了账号密码
@Configuration
public class GulimallElasticSearchConfig {public static final String username = "elastic";
public static final String password = "xxx624753336";
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient(){
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
//es账号密码
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("xxx.xxx.xxx.xxx", 9200, "http")
).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
})
);
returnclient;
}
}
es没有密码:
@Configuration
public class GuliMallElasticSearchConfig {
@Bean
public RestHighLevelClient esRestClient(){
//String hostname, int port, String scheme
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.155.3",9200,"http")));
//http协议不写也行,默认就是
return client;
}
}
3.简单使用 官方文档:es官方操作文档
1.新增索引
//创建请求 指定索引
IndexRequest indexRequest = new IndexRequest("users");
//指定id 保证不会重复添加
indexRequest.id("1");
//创建对象
User user = new User();
user.setUserName("zhangsanlisi");
user.setAge(18);
user.setGender("男");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss ");
String format = simpleDateFormat.format(new Date());
user.setCreateTime(format);
//将数据转为JSON 利用fastjson
String jsonString = JSON.toJSONString(user);
indexRequest.source(jsonString, XContentType.JSON);
//要保存的内容
//将数据保存到es
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
//查看返回
System.out.println(indexResponse);
get /users/_search
文章图片
2.复杂检索 【全文检索|springboot整合】步骤:
1.创建检索请求,可以指定索引和searchSourceBuilder。
文章图片
2.构建检索条件
文章图片
3.执行检索
文章图片
4.通过查询返回的SearchResponse进行结果分析
文章图片
//1.创建检索请求
SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("bank");
//指定DSL 检索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchRequest.source(searchSourceBuilder);
//searchSourceBuilder.query();
//searchSourceBuilder.from();
//searchSourceBuilder.size();
//searchSourceBuilder.aggregation();
searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
//聚合条件 AggregationBuilders工具类构造聚合条件 聚合的名字 ageagg 根据age字段进行聚合
TermsAggregationBuilder ageagg = AggregationBuilders.terms("ageagg").field("age").size(10);
searchSourceBuilder.aggregation(ageagg);
//计算平均薪资
AvgAggregationBuilder balanceavg = AggregationBuilders.avg("bananceavg").field("balance");
searchSourceBuilder.aggregation(balanceavg);
System.out.println(searchSourceBuilder);
//2.执行检索
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
//3.分析结果
System.out.println(searchResponse);
//获取所有查到的数据
SearchHits searchHits = searchResponse.getHits();
//获取hits中的hits
SearchHit[] hits = searchHits.getHits();
//遍历hits
for (SearchHit hit : hits) {
//获取hits中source的值 转为字符串
String sourceAsString = hit.getSourceAsString();
//json转为对象
root parseObject = JSON.parseObject(sourceAsString, root.class);
//打印对象信息
System.out.println(parseObject);
}
//获取聚合信息
Aggregations aggregations = searchResponse.getAggregations();
//List aggregationList = aggregations.asList();
//for (Aggregation aggregation : aggregationList) {
//System.out.println("当前聚合:"+aggregation.getName());
//}
//根据聚合名字获取
Terms ageagg1 = aggregations.get("ageagg");
//获取到buckets中数据
List extends Terms.Bucket> buckets = ageagg1.getBuckets();
//遍历数据
for (Terms.Bucket bucket : buckets) {
//获取到聚合的信息 比如年龄分布+分布的数量
String keyAsString = bucket.getKeyAsString();
System.out.println("年龄:"+keyAsString+"==>"+bucket.getDocCount());
}
//获取到平均新资的聚合信息
Avg balanceagee = aggregations.get("bananceavg");
System.out.println("平均新资:"+balanceagee.getValue());
kibana查出的信息
文章图片
程序查出的数据
文章图片
推荐阅读
- springboot|SpringBoot集成JWT实现token验证
- 全文检索|elasticsearch&Kibana安装
- 全文检索|初步检索及进阶检索
- 软件测试|详解软件测试中白盒测试基本概念及四种白盒测试方法
- 运维|jenkins构建maven、git项目部署远程服务器完整过程
- Java修炼之旅|jenkins自动部署maven项目
- java|类和对象(上)——JavaSE
- J2EE|jsp标签
- Java|Spring Cloud微服务分布式架构之组件和概念介绍