elasticsearch-rest-high-level-client在 7.15.0 中已弃用。
不推荐使用高级 REST 客户端,取而代之的是 Java API 客户端 。
spring-boot-starter-data-elasticsearch 也不推荐,虽然基础操作简化了很多,但是一旦使用了es高级特性,那么就如同进入了地狱,同时elasticsearch更新太快了spring-boot-starter-data-elasticsearch的版本根本就赶不上,导致升级会出现很多问题
现在在es官网推荐我们现在使用 Elasticsearch Java API 客户端
这个是相当于直接使用elasticsearch自身的接口Api所以不存在升级不兼容的问题
Java API 客户端官网地址
elasticsearch Api 操作大全 rest-api
官网推荐依赖
经过各种的依赖冲突的解决,终于能跑起来了…
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.5version>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>co.elastic.clientsgroupId>
<artifactId>elasticsearch-javaartifactId>
<version>8.1.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.12.3version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.12.3version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.12.3version>
dependency>
<dependency>
<groupId>jakarta.jsongroupId>
<artifactId>jakarta.json-apiartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.75version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
//es 客户端
ElasticsearchClient client = new ElasticsearchClient(transport);
官方文档没有关于怎么设置映射和索引的配置的,自能自己研究,经过千辛万苦看底层源代码(一点注释都没有),了解代码编写的逻辑,经过我不懈的努力终于实现了以下的基础配置,其他的高级配置可以进行参考下配置进行扩展
//构建索引,并且起一个别名
CreateIndexResponse createResponse = client.indices()
.create(c -> c
.index("my-index") //创索引
.aliases("main-index", a -> a //创别名
.isWriteIndex(true)
)
// 映射字段属性
.mappings((m)-> m
.properties("name",Property.of(p->p.text(TextProperty.of(p1->p1.analyzer("ik_max_word").searchAnalyzer("ik_smart")))))
.properties("sku" ,Property.of(p->p.text(TextProperty.of(p1->p1.analyzer("ik_max_word").searchAnalyzer("ik_smart")))))
.properties("price",Property.of(p->p.integer(IntegerNumberProperty.of(p1->p1))) )
)
//设置分片 ,numberOfShards分片 ,Replicas副本
.settings((s)->s.numberOfShards("1").numberOfReplicas("2"))
);
System.out.println(createResponse.index());
把官方文档看完后,我只想说一句,真难用, 经过3天的代码封装,基本能解决es的百分之90%的使用场景,代码压缩和简化程度至少优化了50%,同时保留了原有的代码特性, 特殊场景提供了对外客户端自己也可以进行二次开发,特殊的操作需要自己通过es的api接口手动进行修操作,在后端操作es一般就是增删改查数据,以及聚合,高亮等
es:
port: 9200
hostname: localhost
alals: true # 是否使用别名的方式访问
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface DocId {
}
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface EsClass {
/**
* 索引: 前缀index + 默认类名( 自动转小写)
* 默认类名可以修改为我们自定义
* @return
*/
String index() default "";
/**
* 别名: 前缀alias + 默认类名( 自动转小写)
*默认类名可以修改为我们自定义
* @return
*/
String alias() default "" ;
/*
* 分片
*/
int shards()default 1;
/**
* 分片副本
* @return
*/
int replicas()default 1;
}
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface Field {
//默认属性名
String name()default "" ;
//数据类型
EsDataType type();
String analyzer()default ""; //分词
String searchAnalyzer()default ""; //搜索分词
}
不一定都能用,有些可能随着es版本的变化,去掉了,需要啥自行修改
public enum EsDataType {
TEXT("text"),
KEYWORD("keyword"),
FLOAT("float"),
LONG("long"),
INTEGER("integer"),
SHORT("short"),
DOUBLE("double"),
HALF_FLOAT("half_float"),
SCALED_FLOAT("scaled_float"),
BYTE("byte"),
DATE("date"),
BOOLEAN("boolean"),
RANGE("rang"),
BINARY("binary"),
ARRAY("array"),
OBJECT("object"),
NESTED("nested"),
GEO_POINT("geo_point"),
GEO_SHAPE("geo_shape"),
IP("ip"),
COMPLETION("completion"),
TOKEN_COUNT("token_count"),
ATTACHMENT("attachment"),
PERCOLATOR("percolator");
private String type;
EsDataType(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
package com.es8.utli;
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch.cat.aliases.AliasesRecord;
import co.elastic.clients.elasticsearch.cat.indices.IndicesRecord;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
import co.elastic.clients.elasticsearch.core.search.Highlight;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.indices.Alias;
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import co.elastic.clients.util.ObjectBuilder;
import com.alibaba.fastjson.JSONObject;
import com.es8.an.DocId;
import com.es8.an.EsClass;
import com.es8.esenum.EsDataType;
import com.obj.objcopy.ObjectCopyUtil;
import com.reflect.ReflectUtil;
import com.string.PatternCommon;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@Component
public class Es8Client implements InitializingBean {
private static final Logger log = LoggerFactory.getLogger(Es8Client.class);
@Value("${es.port}")
private int prot;
@Value("${es.hostname}")
private String hostname;
@Value("${es.alals}")
private boolean alals;
private ElasticsearchClient client;
private ElasticsearchAsyncClient asyncClient;
private final String indexPrefix = "index-";
private final String aliasPrefix = "alias-";
// 同步客户端
public ElasticsearchClient getClient() {
return client;
}
//异步客户端
public ElasticsearchAsyncClient getAsyncClient() {
return asyncClient;
}
@Override
public void afterPropertiesSet() throws Exception {
RestClient restClient = RestClient.builder(
new HttpHost(hostname, prot)).build();
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
//es 客户端
this.client = new ElasticsearchClient(transport);
this.asyncClient = new ElasticsearchAsyncClient(transport);
}
/**
* @return 是否成功
* @throws Exception
*/
public <T> boolean createIndexSettingsMappings(Class<T> tClass) throws Exception {
EsClass esClassAnnotation = ReflectUtil.getClassAnnotation(tClass, EsClass.class);
int shards = esClassAnnotation.shards();
int replicas = esClassAnnotation.replicas();
StringBuilder stringBuilder = new StringBuilder("{");
stringBuilder.append("\"settings\": {\n" +
" \"number_of_shards\": " + shards + ",\n" +
" \"number_of_replicas\": " + replicas + "\n" +
" },");
stringBuilder.append("\"mappings\": {\n" +
" \"properties\": ");
JSONObject jsonObject = new JSONObject();
Map<String, Annotation[]> fieldsAnnotation = ReflectUtil.getFieldsAnnotation(tClass);
for (Map.Entry<String, Annotation[]> stringEntry : fieldsAnnotation.entrySet()) {
String key = stringEntry.getKey();
Annotation[] value = stringEntry.getValue();
for (Annotation annotation1 : value) {
JSONObject jsonObject1 = new JSONObject();
if (annotation1 instanceof DocId) {
jsonObject1.put("type", EsDataType.LONG.getType());
jsonObject.put(key, jsonObject1);
continue;
}
if (annotation1 instanceof com.es8.an.Field) {
com.es8.an.Field annotation11 = (com.es8.an.Field) annotation1;
String name = annotation11.name();
name = "".equals(name) ? key : name;
EsDataType type = annotation11.type();
String analyzer = annotation11.analyzer();
String searchAnalyzer = annotation11.searchAnalyzer();
jsonObject1.put("type", type.getType());
if (!"".equals(analyzer)) {
jsonObject1.put("analyzer", analyzer);
}
if (!"".equals(searchAnalyzer)) {
jsonObject1.put("search_analyzer", searchAnalyzer);
}
jsonObject.put(name, jsonObject1);
}
}
}
Assert.isTrue(jsonObject.size() > 0, "请添加es相关注解");
stringBuilder.append(jsonObject);
stringBuilder.append("}}");
Reader queryJson = new StringReader(stringBuilder.toString());
//索引和别名必须小写
String index = getClassIndex(tClass);
String alals = getClassAlals(tClass);
CreateIndexRequest req = CreateIndexRequest.of(b -> b
.index(index)
.aliases(alals, Alias.of(a -> a.isWriteIndex(true)))
.withJson(queryJson)
);
return Boolean.TRUE.equals(client.indices().create(req).acknowledged());
}
// 查询全部索引
public List<String> indexs() throws IOException {
List<IndicesRecord> indices = client.cat().indices().valueBody();
return indices.stream().map(IndicesRecord::index).collect(Collectors.toList());
}
// 查询全部别名
public List<String> aliases() throws IOException {
List<AliasesRecord> aliasesRecords = client.cat().aliases().valueBody();
return aliasesRecords.stream().map(AliasesRecord::alias).collect(Collectors.toList());
}
//查询全部数据
public <T> List<T> queryAll(Class<T> tClass) throws IOException {
List<T> list = new ArrayList<>();
String index = getClassAlalsOrIndex(tClass);
SearchResponse<T> search = client.search(s -> s.index(index).query(q -> q.matchAll(m -> m)), tClass);
for (Hit<T> hit : search.hits().hits()) {
list.add(hit.source());
}
return list;
}
//添加数据
/**
* @param o 数据源
* @param async 是否异步 true异步 ,false同步 ,如果是异步那么永远返回null
* @return
*/
public <T> String addData(T o, boolean async) throws IOException {
Long id = getObjId(o);
if (id == null) {
id = UUID.randomUUID().getMostSignificantBits();
}
String index = getClassAlalsOrIndex(o.getClass());
IndexRequest.Builder<T> indexReqBuilder = new IndexRequest.Builder<>();
indexReqBuilder.index(index);
indexReqBuilder.id(String.valueOf(id));
indexReqBuilder.document(o);
if (async) {
asyncClient.index(indexReqBuilder.build());
return null;
}
IndexResponse response = client.index(indexReqBuilder.build());
return response.id();
}
// 批量添加
public <T> void addDatas(List<T> list, boolean async) throws IOException {
BulkRequest.Builder br = new BulkRequest.Builder();
for (T o : list) {
Long id = getObjId(o);
if (id == null) {
id = UUID.randomUUID().getMostSignificantBits();
}
String index = getClassAlalsOrIndex(o.getClass());
Long finalId = id;
br.operations(op -> op
.index(idx -> idx
.index(index)
.id(String.valueOf(finalId))
.document(o)
)
);
}
if (async) {
asyncClient.bulk(br.build());
return;
}
BulkResponse result = client.bulk(br.build());
// Log errors, if any
assert result != null;
if (result.errors()) {
log.error("Bulk had errors");
for (BulkResponseItem item : result.items()) {
if (item.error() != null) {
log.error(item.error().reason());
}
}
}
}
public <T> T getDocId(String docId, Class<T> clazz) throws IOException {
String indexAlals = getClassAlalsOrIndex(clazz);
GetResponse<T> response = client.get(g -> g
.index(indexAlals)
.id(docId),
clazz
);
return response.source();
}
public <T> List<T> complexQuery(Query query, Class<T> clazz) throws IOException {
List<T> list = new ArrayList<>();
String index = getClassAlalsOrIndex(clazz);
SearchResponse<T> response = client.search(s -> s
.index(index)
.query(query),
clazz
);
List<Hit<T>> hits = response.hits().hits();
for (Hit<T> hit : hits) {
list.add(hit.source());
}
return list;
}
public <T> List<Map<String ,Object>> complexQueryHighlight(Query query, Class<T> clazz, String... fields) throws IOException {
List<Map<String ,Object>> list = new ArrayList<Map<String ,Object>>();
String index = getClassAlalsOrIndex(clazz);
Highlight of = Highlight.of(h -> {
for (String field : fields) {
h.fields(
field
,
h1 -> h1.preTags("").postTags(""));
}
return h;
}
);
SearchResponse<T> response = client.search(s -> s
.index(index)
.query(query)
.highlight(of)
, clazz
);
for (Hit<T> hit : response.hits().hits()) {
Map<String ,Object> map=new HashMap<>();
map.put("source",hit.source());
map.put("highlight",hit.highlight());
list.add(map);
}
return list;
}
public <T> Aggregate complexQueryAggregations(Query query, Function<Aggregation.Builder, ObjectBuilder<Aggregation>> fn, Class<T> clazz) throws IOException {
String index = getClassAlalsOrIndex(clazz);
SearchResponse<T> response = client.search(s -> s
.index(index)
.size(0) //不需要显示数据 ,只想要聚合结果
.query(query)
.aggregations("aggregations", fn),
clazz
);
return response.aggregations().get("aggregations");
}
//根据id进行删除
public <T> void delDocId( String docId,Class<T> clazz) throws IOException {
String index = getClassAlalsOrIndex(clazz);
DeleteRequest de = DeleteRequest.of(d -> d.index(index).id(docId));
client.delete(de);
}
//根据查询进行删除
public <T> void delQuery( Query query,Class<T> clazz) throws IOException {
String index = getClassAlalsOrIndex(clazz);
DeleteByQueryRequest de = DeleteByQueryRequest.of(d -> d.index(index).query(query));
client.deleteByQuery(de);
}
public <T> void upDocId(String docId,T o,boolean async) throws Exception {
//先查询出来
Object docId1 = getDocId(docId, o.getClass());
if (docId1==null) {
throw new Exception("没有docId:"+docId+"这条数据");
}
//进行对象拷贝
ObjectCopyUtil.copy(o,docId1);
String index = getClassAlalsOrIndex(o.getClass());
if (async) {
asyncClient.update(UpdateRequest.of(d -> d.index(index).id(docId).doc(o)), o.getClass());
return;
}
client.update(UpdateRequest.of(d -> d.index(index).id(docId).doc(o)), o.getClass());
}
public <T> void upQuery(Query query,T o,boolean async) throws Exception {
Class<?> aClass = o.getClass();
String index = getClassAlalsOrIndex(aClass);
//获取全部字段和字段的名称以及需要修改的值
StringBuilder stringBuilder=new StringBuilder();
for (Field declaredField : aClass.getDeclaredFields()) {
declaredField.setAccessible(true);
Object o1 = declaredField.get(o);
if (o1==null) {
continue;
}
declaredField.setAccessible(true);
com.es8.an.Field field = declaredField.getAnnotation(com.es8.an.Field.class);
String name = field.name();
name = "".equals(name) ? declaredField.getName() : name;
String finalName = name;
String str = PatternCommon.renderString("ctx._source['${name}'] = '${value}';", new HashMap() {{
put("name", finalName);
put("value",o1) ;
}});
stringBuilder.append(str);
}
UpdateByQueryRequest of = UpdateByQueryRequest.of(u -> u.index(index).query(query).script(s -> s.inline(i -> i.source(stringBuilder.toString()))));
if (async) {
asyncClient.updateByQuery(of);
return;
}
client.updateByQuery(of);
}
private <T> String getClassIndex(Class<T> clazz){
EsClass annotation = clazz.getAnnotation(EsClass.class);
String index = annotation.index();
index = "".equals(index) ? Objects.requireNonNull(ReflectUtil.getClassName(clazz)).toLowerCase() : index.toLowerCase();
return indexPrefix + index;
}
private <T> String getClassAlals(Class<T> clazz){
EsClass annotation = clazz.getAnnotation(EsClass.class);
String alias = annotation.alias();
alias = "".equals(alias) ? Objects.requireNonNull(ReflectUtil.getClassName(clazz)).toLowerCase() : alias.toLowerCase();
return aliasPrefix + alias;
}
/**
* 使用索引还是使用别名
* @param clazz
* @param
* @return
*/
private <T> String getClassAlalsOrIndex(Class<T> clazz){
if(alals){
return getClassAlals(clazz);
}
return getClassIndex(clazz);
}
private <T> Long getObjId(T o){
Long id = null;
for (Field declaredField : o.getClass().getDeclaredFields()) {
declaredField.setAccessible(true);
DocId annotation = declaredField.getAnnotation(DocId.class);
if (annotation != null) {
try {
id = (Long) declaredField.get(o);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return id;
}
}
// 只能使用包装类型 ,不能使用基础类型 ,否则会导致一些高级特性有问题
@EsClass
@Data
public class UserEsEneity {
@DocId
private Long id;
@Field(type = EsDataType.KEYWORD )
private String name;
@Field(type =EsDataType.INTEGER)
private Integer age;
@Field(type = EsDataType.TEXT,analyzer = "ik_max_word",searchAnalyzer = "ik_smart")
private String dec;
@Field(type =EsDataType.KEYWORD)
private String sku;
@Field(type =EsDataType.DOUBLE)
private Double price;
}
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.LongTermsBucket;
import co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import com.es8.eneity.UserEsEneity;
import com.es8.utli.Es8Client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Es8Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Es8ApplictionTest {
@Autowired
private Es8Client es8Client;
@Test
public void createIndexSettingsMappings() throws Exception {
es8Client.createIndexSettingsMappings(UserEsEneity.class);
}
@Test
public void aliases() throws Exception {
System.out.println(es8Client.aliases());
}
@Test
public void indexs() throws Exception {
System.out.println(es8Client.indexs());
}
@Test
public void selectDocIdExists() throws Exception {
if (es8Client.getClient().exists(b -> b.index("main-index").id("1")).value()) {
System.out.println("product exists");
}
}
@Test
public void queryAll() throws Exception {
List<UserEsEneity> querys = es8Client.queryAll(UserEsEneity.class);
System.out.println(querys);
}
@Test
public void addData() throws IOException {
UserEsEneity userEsEneity = new UserEsEneity();
userEsEneity.setId(22L);
userEsEneity.setName("xxxxa");
userEsEneity.setAge(22);
userEsEneity.setDec("xxxxxxxx");
userEsEneity.setPrice(22.1);
userEsEneity.setSku("aaa1");
String s = es8Client.addData(userEsEneity,true);
System.out.println(s);
}
@Test
public void addDatas() throws IOException {
UserEsEneity userEsEneity = new UserEsEneity();
userEsEneity.setId(23L);
userEsEneity.setName("hu");
userEsEneity.setAge(22);
userEsEneity.setDec("游泳");
userEsEneity.setPrice(22.1);
userEsEneity.setSku("aaa1");
UserEsEneity userEsEneity1 = new UserEsEneity();
userEsEneity1.setId(24L);
userEsEneity1.setName("an");
userEsEneity1.setAge(22);
userEsEneity1.setDec("");
userEsEneity1.setPrice(22.2);
userEsEneity1.setSku("vvvvvv");
List<UserEsEneity> list = new ArrayList<>();
list.add(userEsEneity);
list.add(userEsEneity1);
es8Client.addDatas(list,true);
}
@Test
public void getDocId() throws IOException {
UserEsEneity docId = es8Client.getDocId("24", UserEsEneity.class);
System.out.println(docId);
}
@Test
public void complexQuery_MatchAll() throws IOException {
Query query = Query.of(q -> q.matchAll(m -> m));
List<UserEsEneity> userEsEneities = es8Client.complexQuery(query, UserEsEneity.class);
System.out.println(userEsEneities);
}
@Test
public void complexQuery_MatchAll_Alals() throws IOException {
Query query = Query.of(q -> q.matchAll(m -> m));
List<UserEsEneity> userEsEneities = es8Client.complexQuery(query, UserEsEneity.class);
System.out.println(userEsEneities);
}
@Test
public void complexQuery_MatchQuery() throws IOException {
Query query = Query.of(q -> q.match(m -> m.field("name").query("xxxxa")));
List<UserEsEneity> userEsEneities = es8Client.complexQuery(query, UserEsEneity.class);
System.out.println(userEsEneities);
}
@Test
public void complexQuery_query_bool_must() throws IOException {
Query age = Query.of(q -> q.match(m -> m.field("age").query(22)));
Query price = Query.of(q -> q.match(m -> m.field("price").query(22.1)));
Query bool = Query.of(q -> q.bool(b -> b.must(age).must(price)));
List<UserEsEneity> userEsEneities = es8Client.complexQuery(bool, UserEsEneity.class);
System.out.println(userEsEneities);
}
@Test
public void complexQueryHighlight() throws IOException {
Query dec = Query.of(q -> q.matchPhrase(m -> m.field("dec").query("匹配")));
List<Map<String, Object>> maps = es8Client.complexQueryHighlight(dec, UserEsEneity.class, "dec");
System.out.println(maps);
}
@Test
public void complexQuery_query_complexQueryAggregations() throws IOException {
Query query = Query.of(q -> q.matchAll(m -> m));
Aggregate age = es8Client.complexQueryAggregations(query, a ->
a.terms(t -> t.field("age"))
, UserEsEneity.class);
for (LongTermsBucket longTermsBucket : age.lterms().buckets().array()) {
System.out.println("key:"+longTermsBucket.key()+":共多少:"+longTermsBucket.docCount());
}
Aggregate name = es8Client.complexQueryAggregations(query, a ->
a.terms(t -> t.field("name"))
, UserEsEneity.class);
for (StringTermsBucket stringTermsBucket : name.sterms().buckets().array()) {
System.out.println("key:"+stringTermsBucket.key()+":共多少:"+stringTermsBucket.docCount());
}
Aggregate price = es8Client.complexQueryAggregations(query, a ->
a.avg(t -> t.field("price"))
, UserEsEneity.class);
System.out.println(price.avg().value());
}
@Test
public void delDocId() throws IOException {
es8Client.delDocId( "23",UserEsEneity.class);
}
@Test
public void delQuery() throws IOException {
Query price = Query.of(q -> q.match(m -> m.field("price").query("0.0")));
es8Client.delQuery( price,UserEsEneity.class);
}
@Test
public void upDocId() throws Exception {
UserEsEneity userEsEneity1 = new UserEsEneity();
// userEsEneity1.setId(24L);
// userEsEneity1.setName("an");
userEsEneity1.setAge(21);
userEsEneity1.setDec("嘻嘻嘻嘻嘻嘻擦擦擦");
userEsEneity1.setPrice(28.2);
userEsEneity1.setSku("mmmmm");
es8Client.upDocId("241",userEsEneity1,true);
}
@Test
public void upQuery() throws Exception {
UserEsEneity userEsEneity1 = new UserEsEneity();
// userEsEneity1.setId(24L);
// userEsEneity1.setName("an");
// userEsEneity1.setAge(21);
userEsEneity1.setDec("嘻嘻嘻嘻嘻嘻擦擦擦");
// userEsEneity1.setPrice(28.2);
// userEsEneity1.setSku("mmmmm");
Query name = Query.of(q -> q.matchPhrase(m -> m.field("age").query("33")));
es8Client.upQuery(name,userEsEneity1,true);
}
}