写操作
单条操作
import com.atguigu.entity.Person;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;
import java.io.IOException;
/**
* @ClassName: Demo1
* @Description:
* @Author: kele
* @Date: 2021/3/24 21:30
*
* 1、创建客户端
* 2、设置请求对象(string、或者类)
* 3、执行
* 4、关闭连接
*
**/
public class Demo1 {
public static void main(String[] args) throws IOException {
//新建一个客户端,通过Factory创建一个连接对象
JestClientFactory jestClientFactory = new JestClientFactory();
/**
* 设置ES的连接参数
*
* 默认连接参数
* httpClientConfig = new HttpClientConfig.Builder("http://localhost:9200").build();
*/
HttpClientConfig config = new HttpClientConfig.Builder("http://hadoop102:9200").build();
jestClientFactory.setHttpClientConfig(config);
//创建一个jestClient的客户端
JestClient jestClient = jestClientFactory.getObject();
//方式一:将请求体作为string传入
String source = "{ \"empid\":1020,\n" +
" \"age\":20,\n" +
" \"balance\":2000,\n" +
" \"name\":\"七阿哥\",\n" +
" \"gender\":\"男\",\n" +
" \"hobby\":\"弟BUG\"\n" +
"}";
Index index1 = new Index.Builder(source)
.index("test")
.type("emps")
.id("25")
.build();
//方式二:传入对象
Person person = new Person("26",40,88888.88,"雍正","男","治BUG");
Index index2 = new Index.Builder(person)
.index("test")
.type("emps")
.id("26")
.build();
//执行,传入action类,
DocumentResult result1 = jestClient.execute(index1);
DocumentResult result2 = jestClient.execute(index2);
//关闭客户端连接,
//6.x版本用shutdown,之后的版本用close
jestClient.shutdownClient();
}
}
结果:
批量操作
package com.atguigu.es;
import com.atguigu.entity.Person;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import java.io.IOException;
/**
* @ClassName: Demo2
* @Description:
* @Author: kele
* @Date: 2021/3/25 9:21
**/
public class Demo2 {
public static void main(String[] args) throws IOException {
//创建连接
JestClientFactory jestClientFactory = new JestClientFactory();
HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
jestClientFactory.setHttpClientConfig(httpClientConfig);
//获取连接对象
JestClient jestClient = jestClientFactory.getObject();
Person person1 = new Person("26",40,88888.88,"雍正","男","治BUG");
//插入一条数据
Index index = new Index.Builder(person1)
.id("28")
.build();
//删除一条数据
Delete delete = new Delete.Builder("1").build();
Bulk batch = new Bulk.Builder()
.defaultIndex("test")
.defaultType("emps")
//index可以用来增,改操作
.addAction(index)
.addAction(delete)
.build();
//执行
jestClient.execute(batch);
//关闭连接
jestClient.shutdownClient();
}
}
读操作
package com.atguigu.es;
import com.atguigu.entity.Person;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.*;
import io.searchbox.core.search.aggregation.AvgAggregation;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;
import java.io.IOException;
import java.util.List;
/**
* @ClassName: Demo2
* @Description:
* @Author: kele
* @Date: 2021/3/25 9:21
*
* 读操作
**/
public class Demo3 {
public static void main(String[] args) throws IOException {
//配置连接参数
JestClientFactory jestClientFactory = new JestClientFactory();
HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
jestClientFactory.setHttpClientConfig(httpClientConfig);
//获取连接客户端对象
JestClient jestClient = jestClientFactory.getObject();
//查询条件
String query = "{\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"hobby\": \"购物\"\n" +
" }\n" +
" }, \n" +
" \"aggs\": {\n" +
" \"gender_count\": {\n" +
" \"terms\": {\n" +
" \"field\": \"gender.keyword\",\n" +
" \"size\": 10\n" +
" }\n" +
" },\n" +
" \"avg_age\": {\n" +
" \"avg\": {\n" +
" \"field\": \"age\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex("test")
.addType("emps")
.build();
/**
* 获取到查询结果
*
* 结果包括:
* total :总查询条数
* MaxScore :查询数据中最大的score,最匹配的
* hits:结果集
* score :评分
* routing:路由
* source:查询的结果信息
*/
SearchResult result = jestClient.execute(search);
System.out.println("总条数:"+result.getTotal());
System.out.println("最大的分数:"+result.getMaxScore());
List<SearchResult.Hit<Person, Void>> hits = result.getHits(Person.class);
//获取hit部分
for (SearchResult.Hit<Person, Void> hit : hits) {
System.out.println("评分:"+hit.score);
System.out.println(hit.source);
System.out.println();
}
//获取aggreagte聚合的部分
MetricAggregation aggregations = result.getAggregations();
//根据term聚合的部分,包含term,term中可能还包含其他的aggs
TermsAggregation gender_count = aggregations.getTermsAggregation("gender_count");
List<TermsAggregation.Entry> buckets = gender_count.getBuckets();
for (TermsAggregation.Entry bucket : buckets) {
System.out.println(bucket.getKey() +" : "+ bucket.getCount());
}
//获取根据avg聚合的部分
AvgAggregation avg = aggregations.getAvgAggregation("avg_age");
System.out.println("平均年龄:" + avg.getAvg());
jestClient.shutdownClient();
}
}