Elasticarch8.0版本中ElasticarchJavaAPIClient。。。
关于Elasticarch Java API Client客户端如何连接以及如何对索引和⽂档进⾏基本的增删改查操作请查看我的上⼀篇博⽂:
本篇主要描述在Elasticarch Java API Client客户端中如何进⾏批量操作,以及如何进⾏各种条件及类型的查询(Search)⽅法。1. 批量添加⽂档
// 创建客户端连接部分
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
ElasticarchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
ElasticarchClient client = new ElasticarchClient(transport);
// 构建⼀个批量操作BulkOperation的集合
List<BulkOperation> bulkOperations = new ArrayList<>();
// 向集合添加数据
bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("zhangsan", "男", 30)).id("3001").index("newapi")).build()); bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("lisi", "⼥", 30)).id("3002").index("newapi")).build()); bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("wangwu", "男", 40)).id("3003").index("newapi")).build()); bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("wangwu1", "⼥", 40)).id("3004").index("newapi")).build()); bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("wangwu2", "男", 50)).id("3005").index("newapi")).build()); bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("wangwu3", "男", 50)).id("3006").index("newapi")).build()); bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("wangwu33", "男", 50)).id("3007").index("newapi")).build()); bulkOperations.add(new BulkOperation.Builder().create(d-> d.document(new Test("wangwu333", "男", 50)).id("3008").index("newapi")).build());
// 使⽤bulk⽅法执⾏批量操作并获得响应
BulkRespon respon = client.bulk(e->e.index("newapi").operations(bulkOperations));
// 打印结果
System.out.k());
System.out.println(respon.items());
// 关闭客户端连接部分
transport.clo();
restClient.clo();
⾸先需要创建ES客户端连接,然后构建⼀个批量操作BulkOperation的ArrayList集合,并向其添加你需要插⼊的⽂档数据,这⾥新版客户端可以直接传⼊Java对象,ES会在内部⾃⾏处理。
使⽤ES客户端的bulk⽅法进⾏批量操作并获得批量操作的响应结果,最后打印出结果即可。辛亥革命背景
2. 批量删除⽂档
// 创建ES客户端部分
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
ElasticarchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
ElasticarchClient client = new ElasticarchClient(transport);
// 构建批量操作对象BulkOperation的集合
List<BulkOperation> bulkOperations = new ArrayList<>();
// 向集合中添加需要删除的⽂档id信息
bulkOperations.add(new BulkOperation.Builder().delete(d-> d.index("newapi").id("3001")).build());
bulkOperations.add(new BulkOperation.Builder().delete(d-> d.index("newapi").id("3002")).build());
bulkOperations.add(new BulkOperation.Builder().delete(d-> d.index("newapi").id("3003")).build());
/
/ 调⽤客户端的bulk⽅法,并获取批量操作响应结果
BulkRespon respon = client.bulk(e->e.index("newapi").operations(bulkOperations));
System.out.k());
System.out.println(respon.items());
// 关闭ES客户端部分
transport.clo();
restClient.clo();
与批量添加⽂档类似,⾸先需要创建ES客户端,同样使⽤BulkOperation集合来存储批量操作的内容,不同的是这次使⽤BulkOperationBuilder的delete⽅法构建批量删除操作,最后调⽤ES客户端的bulk⽅法执⾏,获取的响应结果同样为BulkRespon类型。
3. 分页查询
// 分页查询
SearchRespon<Test> respon3 = client.arch(s -> s
.index("newapi")
.query(q -> q
.matchAll(m -> m)
)
.from(4)
.size(2)
, Test.class);
中秋月宋苏轼
System.out.k());
System.out.println(respon3.hits().total().value());
respon3.hits().hits().forEach(e -> System.out.println(e.source().toString()));
Elasticarch Java API Client客户端中的分页查询主要使⽤SearchRespon的from和size⽅法传⼊参数,其中from代表数据开始的下表位置,size代表每次查询需要获取到的⽂档数量。
4. 查询排序
.matchAll(m -> m)
)
.sort(sort -> sort
.field(f -> f
.field("age")
.order(SortOrder.Desc)
)
)
, Test.class);
System.out.k());
System.out.println(respon4.hits().total().value());
respon4.hits().hits().forEach(e -> System.out.println(e.source().toString()));
Elasticarch Java API Client客户端中的查询排序主要使⽤sort⽅法传⼊排序参数,我这⾥使⽤了lambda形式传⼊参数。与RestAPI⼀致,需要传⼊field名称以及排序⽅式,如ASC、DESC。
5. 过滤字段
// 过滤字段
SearchRespon<Test> respon5 = client.arch(s -> s
.index("newapi")
.query(q -> q
.matchAll(m -> m)
)
.sort(sort -> sort
.field(f -> f
.field("age")
.order(SortOrder.Desc)
竹林壁纸)
)
.source(source -> source
.filter(f -> f
.includes("name")
.excludes("")
)
)
, Test.class);
System.out.k());
System.out.println(respon5.hits().total().value());
一年级生字表respon5.hits().hits().forEach(e -> System.out.println(e.source().toString()));
Elasticarch Java API Client客户端中的过滤字段同样使⽤source传⼊参数,与RestAPI相同,使⽤includes和excludes来标记⽩名单或⿊名单模式,其中includes代表⽩名单,只返回指定的字段。excludes代表⿊名单,不返回指定的字段。
7. 组合查询
不成功便成仁是什么意思
.bool(b -> b
.must(must -> must
.match(m -> m
.field("age")
.query(30)
)
)
.must(must -> must
.match(m -> m
.field("x")
.query("男")
)
)
.should(should -> should
.match(m -> m
.
field("age")
.query(30)
)
)
.should(should -> should
.match(m -> m
.field("age")
.query(40)
)
)
)
)
, Test.class);
System.out.k());
System.out.println(respon6.hits().total().value());
respon6.hits().hits().forEach(e -> System.out.println(e.source().toString()));
Elasticarch Java API Client客户端中的组合查询,与RestAPI保持⼀直,使⽤bool下的must或should来代表必须满⾜某条件或只需满⾜某条件。
8. 范围查询
// 范围查询
SearchRespon<Test> respon7 = client.arch(s -> s
天馈系统.index("newapi")
.query(q -> q
.
range(r -> r
.field("age")
.gte(JsonData.of(30))
.lt(JsonData.of(40))
)
)
, Test.class);
System.out.k());
System.out.println(respon7.hits().total().value());
看书的英文怎么写
respon7.hits().hits().forEach(e -> System.out.println(e.source().toString()));
Elasticarch Java API Client客户端中的范围查询,与组合查询不同的是,使⽤了range⽽不是bool。field参数代表需要判断的字
段,ge、gte、lt、lte分别代表⼤于、⼤于等于、⼩于、⼩于等于。
9. 模糊查询
.fuzzy(f -> f
.field("name")
.value("wangwu")
.fuzziness("1"))
)
, Test.class);
System.out.k());
System.out.println(respon8.hits().total().value());
respon8.hits().hits().forEach(e -> System.out.println(e.source().toString()));
Elasticarch Java API Client客户端中的模糊查询,使⽤fuzzy⽽不是like,其中field代表需要判断的字段名称,value代表需要模糊查询的关键词,fuzziness代表可以与关键词有误差的字数,可选值为0、1、2这三项。
10. ⾼亮查询
// ⾼亮查询
SearchRespon<Test> respon9 = client.arch(s -> s
.index("newapi")
.query(q -> q
.term(t -> t
.field("name")
.value("wangwu")
)
)
税法论文.highlight(h -> h
.fields("name", f -> f
.preTags("<font color='red'>")
.postTags("</font>")
)
)
, Test.class);
System.out.k());
System.out.println(respon9.hits().total().value());
respon9.hits().hits().forEach(e -> {
System.out.println(e.source().toString());
for (Map.Entry<String, List<String>> entry : e.highlight().entrySet()) {
System.out.println("Key = " + Key());
}
});
Elasticarch Java API Client客户端中的⾼亮查询,主要⽤于给查询出的关键词添加⼀个标识符,便于前端展⽰。使⽤highlight字段,其中fields的key代表需要标记的字段名称,preTags代表需要添加标记的前缀,postTags代表需要添加标记的后缀。同时响应的获取⽅式也有所改变,具体可以参照上述代码。
11. 聚合查询