Elasticsearch-Rest-Client
整合Elasticsearch
pom.xml配置
Maven Repository
<properties>
<elasticsearch.version>7.4.2</elasticsearch.version>
</properties>
<!-- 导入elasticsearch的rest-high-level-client-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
ElasticSearch 配置文件
GulimallElasticSearchConfig.java:
package com.atguigu.gulimall.search.config;
/**
* 1、导入依赖 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-getting-started-maven.html
* 2、编写配置,容器中注入一个RestHighLevelClient https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-getting-started-initialization.html
* 3、参照API https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high.html
*/
@Configuration
public class GulimallElasticSearchConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer " + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient() {
//官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-getting-started-initialization.html
// RestHighLevelClient client = new RestHighLevelClient(
// RestClient.builder(
// new HttpHost("192.168.188.128", 9200, "http")));
RestClientBuilder builder = null;
//public HttpHost(String hostname, int port, String scheme)
builder = RestClient.builder(new HttpHost("192.168.188.128", 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
Elasticsearch 测试
GulimallSearchApplicationTests.java:
package com.atguigu.gulimall.search;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallSearchApplicationTests {
@Resource
private RestHighLevelClient client;
/**
* https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-search.html
* @throws IOException
*/
@Test
public void searchData() throws IOException {
//1、创建检索请求
SearchRequest searchRequest = new SearchRequest();
// 指定索引
searchRequest.indices("bank");
// 检索条件 DSL
// public SearchRequest source(SearchSourceBuilder sourceBuilder)
// SearchSourceBuilder sourceBuilder 封装条件
SearchSourceBuilder sourceBuilder= new SearchSourceBuilder();
searchRequest.source(sourceBuilder);
//(1). 构建检索条件
// sourceBuilder.query();
// sourceBuilder.from();
// sourceBuilder.size();
// sourceBuilder.aggregation();
//public SearchSourceBuilder query(QueryBuilder query)
/**
* # 搜索 address 中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人的详情
* GET bank/_search
* {
* "query": {
* "match": {
* "address": "mill"
* }
* },
* "aggs": {
* "ageAgg": {
* "terms": {
* "field": "age",
* "size": 10
* }
* },
* "ageAvg":{
* "avg":{
* "field":"age"
* }
* },
* "balanceAvg":{
* "avg": {
* "field": "balance"
* }
* }
* },
* "size": 0
* }
*/
sourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
// (2).构建聚合条件 按照年龄的值分布进行聚合
// public SearchSourceBuilder aggregation(AggregationBuilder aggregation)
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
sourceBuilder.aggregation(ageAgg);
// (3).计算平均薪资
TermsAggregationBuilder balanceAvg = AggregationBuilders.terms("balanceAvg").field("balance");
sourceBuilder.aggregation(balanceAvg);
System.out.println("检索条件: "+sourceBuilder.toString());
searchRequest.source(sourceBuilder);
//2、执行检索
SearchResponse searchResponse = client.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
//3、分析结果 SearchResponse
System.out.println(searchResponse.toString());
// Map map = JSON.parseObject(searchResponse.toString(), Map.class);
//(1).获取所有查到的数据
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for(SearchHit hit:searchHits){
/**
* "_index" : "bank",
* "_type" : "account",
* "_id" : "472",
* "_score" : 5.4032025,
* "_source" :
*/
// hit.getIndex();
// hit.getType();
// hit.getIndex();
String sourceAsString = hit.getSourceAsString();
// public static <T> T parseObject(String text, Class<T> clazz) 将String转为Java对象
Account account = JSON.parseObject(sourceAsString, Account.class);
System.out.println("账号信息account: "+account);
}
//(2).获取检索到的分析信息
Aggregations aggregtion = searchResponse.getAggregations();
// for (Aggregation aggregation : aggregtion.asList()) {
// System.out.println("当前聚合: "+aggregation.getName());
// /**
// * Aggregations aggregations = searchResponse.getAggregations();
// * Terms byCompanyAggregation = aggregations.get("by_company");
// * Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic");
// * Avg averageAge = elasticBucket.getAggregations().get("average_age");
// * double avg = averageAge.getValue();
// */
// }
Terms ageAgg1 = aggregtion.get("ageAgg");
for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
String keyAsString = bucket.getKeyAsString();
System.out.println("年龄: "+keyAsString+" ==> "+bucket.getDocCount());
}
// org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms
// cannot be cast to
// org.elasticsearch.search.aggregations.metrics.Avg
// TODO 待解决
Avg balanceAvg1 = aggregtion.get("balanceAvg");
System.out.println("平均薪资: "+balanceAvg1.getValue());
}
/**
* 测试存储数据到es
* https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.5/java-rest-high-document-index.html
*/
@Test
public void indexData() throws IOException {
IndexRequest indexRequest = new IndexRequest("users");
indexRequest.id("1");
// indexRequest.source("userName","zhangsan","age",18,"gender","男");
// 方式二:
//推荐使用该方式
// 将对象转成json
User user = new User();
user.setUserName("zhangsan");
user.setAge(18);
user.setGender("男");
String jsonString = JSON.toJSONString(user);
// 要保存到es的内容
//java.lang.IllegalArgumentException: The number of object passed must be even but was [1] 需要添加XContentType.JSON
indexRequest.source(jsonString, XContentType.JSON);
// 执行操作
IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
//提取有效的效应数据
System.out.println(index);
}
@Data
class User{
private String userName;
private String gender;
private Integer age;
}
@Data
@ToString
//com.alibaba.fastjson.JSONException: can't create non-static inner class instance. 添加static
static class Account{
private int account_number;
private int balance;
private String firstname;
private String lastname;
private int age;
private String gender;
private String address;
private String employer;
private String email;
private String city;
private String state;
}
}