以下步骤均参考Elasticsearch操作文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
1、导入依赖@Configurationpublic class ElasticsearchConfig { 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(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.8.110", 9200, "http"))); return client; }}
3、测试@Datapublic class User{ private String name; private Integer age; private String gender;}
3.1、数据操作 - - 增改添加或修改
//添加或修改@Autowiredprivate RestHighLevelClient client;@Testpublic void indexData() throws IOException { IndexRequest request = new IndexRequest("users"); //等价下面注释的两行代码 request.id("1"); User user = new User(); user.setAge(20); user.setName("李四"); user.setGender("男"); String jsonString = JSON.toJSONString(user); //要保存的内容 request.source(jsonString, XContentType.JSON); //执行操作 IndexResponse response = client.index(request, ElasticsearchConfig.COMMON_OPTIONS); //响应数据 System.out.println(response);}
批量添加
//批量添加@Testpublic void bulkData() throws IOException { List
文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html
3.2.1、简单检索 SDL语法:
结果:
@Testpublic void searchData() throws IOException { // 1、创建检索请求 SearchRequest request = new SearchRequest(); // 指定检索索引 request.indices("bank"); // DSL,检索条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// searchSourceBuilder.query();// searchSourceBuilder.from();// searchSourceBuilder.size();// searchSourceBuilder.aggregation(); searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill")); System.out.println(searchSourceBuilder.toString()); request.source(searchSourceBuilder); // 2、执行检索 SearchResponse response = client.search(request, ElasticsearchConfig.COMMON_OPTIONS); // 3、分析结果 System.out.println(response.toString());}
3.2.2、复杂检索
// 此类根据查询结果,在线生成(例如json在线格式化)@Datapublic class Accout { 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;}
@Testpublic void searchData() throws IOException { // 1、创建检索请求 SearchRequest request = new SearchRequest(); // 指定检索索引 request.indices("bank"); // DSL,检索条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // 1.1)构造检索条件 searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill")); // 1.2)按照年龄的值分布进行聚合操作 TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10); searchSourceBuilder.aggregation(ageAgg); // 1.3) 计算平均工资 AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance"); searchSourceBuilder.aggregation(balanceAvg); System.out.println("检索条件:"+searchSourceBuilder.toString()); request.source(searchSourceBuilder); // 2、执行检索 SearchResponse response = client.search(request, ElasticsearchConfig.COMMON_OPTIONS); // 3、分析结果 System.out.println(response.toString()); //Map map = JSON.parseObject(response.toString(), Map.class); // 3.1) 获取所有查询的记录 SearchHits hits = response.getHits(); SearchHit[] searchHits = hits.getHits(); for (SearchHit searchHit : searchHits) { String sourceAsString = searchHit.getSourceAsString(); Accout accout = JSON.parseObject(sourceAsString, Accout.class); System.out.println(accout); } // 3.2)获取聚合信息 Aggregations aggregations = response.getAggregations(); //年龄聚合 Terms ageAgg1 = aggregations.get("ageAgg"); for (Terms.Bucket bucket : ageAgg1.getBuckets()) { String keyAsString = bucket.getKeyAsString(); System.out.println("年龄:"+keyAsString); } //平均工资聚合 Avg balanceAvg1 = aggregations.get("balanceAvg"); System.out.println("平均工资:"+balanceAvg1.getValue());}