ElasticSearch04_elasticsearch-Rest-Client整合SpringBoot中使用保存数据利用JAVA代码操作es
Posted 所得皆惊喜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearch04_elasticsearch-Rest-Client整合SpringBoot中使用保存数据利用JAVA代码操作es相关的知识,希望对你有一定的参考价值。
文章目录
- ①. elasticsearch-Rest-Client
- ②. SpringBoot整合ElasticSearch
- ③. 利用ElasticSearch保存数据
- ④. 使用JAVA - API操作ES进行测试
①. elasticsearch-Rest-Client
-
①. java操作es有两种方式
-
②. 9300: TCP(spring-data-elasticsearch:transport-api.jar)
- springboot版本不同,ransport-api.jar不同,不能适配es版本
- 7.x已经不建议使用,8以后就要废弃
- ③. 9200: HTTP
- JstClient:非官方,更新慢
- RestTemplate:模拟HTTP请求,ES很多操作需要自己封装,麻烦
- HttpClient:同上
- Elasticsearch-Rest-Client:官方RestClient,封装了ES操作,API层次分明,上手简单
最终选择Elasticsearch-Rest-Client(elasticsearch-rest-high-level-client)
- ③. 如何找到Elasticsearch-Rest-Client官网常用的操作
②. SpringBoot整合ElasticSearch
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.4.2</elasticsearch.version>
</properties>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
- ②. 官方建议把requestOptions创建成单实例
@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();
//https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html
@Bean
public RestHighLevelClient esRestClient()
RestClientBuilder builder = null;
// 可以指定多个es
builder = RestClient.builder(new HttpHost("192.168.56.10", 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
③. 利用ElasticSearch保存数据
- ①. 在官方文档中找到保存数据的位置
- ②. 保存数据测试
@SuppressWarnings("all")
@RunWith(SpringRunner.class)
@SpringBootTest
public class SearchApplicationTests
@Autowired
private RestHighLevelClient levelClient;
@Test
public void contextLoads()
System.out.println(levelClient);
@Data
class User
private String userName;
private String gender;
private Integer age;
/**
*es新增数据(更新也可以)
*/
@Test
public void indexData() throws IOException
IndexRequest request = new IndexRequest("users");
request.id("1");
User user = new User();
user.setUserName("TANGZHI");
user.setAge(25);
user.setGender("男");
/**
* <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>$fastjson.version</version>
</dependency>
*/
String jsonString = JSON.toJSONString(user);
request.source(jsonString, XContentType.JSON);
//执行操作
IndexResponse indexResponse = levelClient.index(request, RequestOptions.DEFAULT);
//提取有用的数据
System.out.println(indexResponse);
GET users/_search
"took" : 0,
"timed_out" : false,
"_shards" :
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
,
"hits" :
"total" :
"value" : 1,
"relation" : "eq"
,
"max_score" : 1.0,
"hits" : [
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" :
"age" : 25,
"gender" : "男",
"userName" : "TANGZHI"
]
④. 使用JAVA - API操作ES进行测试
- ①. 利用官方文档写出对应的Java代码
需求:搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人的详情
@Test
public void find() throws IOException
//1、创建检索请求
SearchRequest searchRequest=new SearchRequest();
//指定索引
searchRequest.indices("bank");
//指定DSL,检索条件
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//1.1 构造检索条件
sourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
//AggregationBuilders工具类构建AggregationBuilder
// 构建第一个聚合条件:按照年龄的值分布
TermsAggregationBuilder agg1 = AggregationBuilders.terms("ageAgg").field("age").size(10);// 聚合名称
// 参数为AggregationBuilder
sourceBuilder.aggregation(agg1);
AvgAggregationBuilder avgAgg = AggregationBuilders.avg("ageAvg").field("age");
sourceBuilder.aggregation(avgAgg);
System.out.println("检索条件"+sourceBuilder.toString());
searchRequest.source(sourceBuilder);
//2、执行检索
SearchResponse searchResponse = levelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("执行检索:"+searchResponse);
- ②. 转换成一个BEAN
//3、分析结果,全部的结果在searchResponse
// 3.1 获取java bean
SearchHits hits = response.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit hit : hits1)
hit.getId();
hit.getIndex();
String sourceAsString = hit.getSourceAsString();
Account account = JSON.parseObject(sourceAsString, Account.class);
System.out.println(account);
@Data
@ToString
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;
Account(accountNumber=970, balance=19648, firstname=Forbes, lastname=Wallace, age=28, gender=M, address=990 Mill Road, employer=Pheast, email=forbeswallace@pheast.com, city=Lopezo, state=AK)
Account(accountNumber=136, balance=45801, firstname=Winnie, lastname=Holland, age=38, gender=M, address=198 Mill Lane, employer=Neteria, email=winnieholland@neteria.com, city=Urie, state=IL)
Account(accountNumber=345, balance=9812, firstname=Parker, lastname=Hines, age=38, gender=M, address=715 Mill Avenue, employer=Baluba, email=parkerhines@baluba.com, city=Blackgum, state=KY)
Account(accountNumber=472, balance=25571, firstname=Lee, lastname=Long, age=32, gender=F, address=288 Mill Street, employer=Comverges, email=leelong@comverges.com, city=Movico, state=MT)
- ③. Buckets分析信息
// 3.2 获取检索到的分析信息
Aggregations aggregations = searchResponse.getAggregations();
Terms ageAvg = aggregations.get("ageAgg");
for (Terms.Bucket bucket : ageAvg.getBuckets())
System.out.println(bucket.getKey()+"----"+bucket.getDocCount());
Avg avgAgg2 = aggregations.get("ageAvg");
System.out.println("平均年龄的值为:"+avgAgg2.getValue());
- ④. 完整代码展示
//搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人的详情
@Test
public void find() throws IOException
//1、创建检索请求
SearchRequest searchRequest=new SearchRequest();
//指定索引
searchRequest.indices("bank");
//指定DSL,检索条件
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//1.1 构造检索条件
sourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
//AggregationBuilders工具类构建AggregationBuilder
// 构建第一个聚合条件:按照年龄的值分布
TermsAggregationBuilder agg1 = AggregationBuilders.terms("ageAgg").field("age").size(10);// 聚合名称
// 参数为AggregationBuilder
sourceBuilder.aggregation(agg1);
AvgAggregationBuilder avgAgg = AggregationBuilders.avg("ageAvg").field("age");
sourceBuilder.aggregation(avgAgg);
System.out.println("检索条件"+sourceBuilder.toString());
searchRequest.source(sourceBuilder);
//2、执行检索
SearchResponse searchResponse = levelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("执行检索:"+searchResponse);
//3、分析结果,全部的结果在searchResponse
// SearchHits hits = searchResponse.getHits();
// SearchHit[] hits1 = hits.getHits();
// for (SearchHit hit : hits1)
// hit.getId();
// hit.getIndex();
// String sourceAsString = hit.getSourceAsString();
// Account account = JSON.parseObject(sourceAsString, Account.class);
// System.out.println(account);
//
Aggregations aggregations = searchResponse.getAggregations();
Terms ageAvg = aggregations.get("ageAgg");
for (Terms.Bucket bucket : ageAvg.getBuckets())
System.out.println(bucket.getKey()+"----"+bucket.getDocCount());
Avg avgAgg2 = aggregations.get("ageAvg");
System.out.println("平均年龄的值为:"+avgAgg2.getValue());
// List<Aggregation> aggregations1 = aggregations.asList();
// for (Aggregation aggregation : aggregations1)
// System.out.println(aggregation.getName());
//
// 结果输出
检索条件"query":"match":"address":"query":"mill","operator":"OR","prefix_length":0,"max_expansions":50,"fuzzy_transpositions":true,"lenient":false,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"boost":1.0,"aggregations":"ageAgg":"terms":"field":"age","size":10,"min_doc_count":1,"shard_min_doc_count":0,"show_term_doc_count_error":false,"order":["_count":"desc","_key":"asc"],"ageAvg":"avg":"field":"age"
执行检索:"took":1,"timed_out":false,"_shards":"total":1,"successful":1,"skipped":0,"failed":0,"hits":"total":"value":4,"relation":"eq","max_score":5.4032025,"hits":["_index":"bank","_type":"account","_id":"970","_score":5.4032025,"_source":"account_number":970,"balance":19648,"firstname":"Forbes","lastname":"Wallace","age":28,"gender":"M","address":"990 Mill Road","employer":"Pheast","email":"forbeswallace@pheast.com","city":"Lopezo","state":"AK","_index":"bank","_type":"account","_id":"136","_score":5.4032025,"_source":"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"winnieholland@neteria.com","city":"Urie","state":"IL","_index":"bank","_type"docker安装elasticsearch