elasticsearch
文章目录
- 说明
- 下载安装 elasticsearch
- 依赖
- 初始化 es 客户端
- 创建索引
- 查看索引
- 删除索引
- 创建文档
- 通过id查看文档
- 修改文档
- 通过id删除文档
- 条件查询
- 分页查询
- 查询过滤字段
- 条件查询
- 聚合查询
说明
请先看理论知识, 再看代码, 事半功倍! 有些代码的测试数据可能不全, 旨在理解API的调用, 所以…
下载安装 elasticsearch
这里下载安装的版本是 elasticsearch-7.8.0
依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch 的客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch 依赖 2.x 的 log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- junit 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 一个小而全的Java工具类库(推荐使用) -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
</dependencies>
初始化 es 客户端
public void initESClient() throws IOException {
//创建 es 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//业务代码区 -- begin
//TODO
//业务代码区 -- end
//关闭 es 客户端
esClient.close();
}
创建索引
public void createIndex() throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//使用 CreateIndexRequest 创建 user 索引
CreateIndexRequest request = new CreateIndexRequest("user");
CreateIndexResponse response = esClient.indices().create(request, RequestOptions.DEFAULT);
boolean acknowledged = response.isAcknowledged();
System.out.println("索引创建" + (acknowledged?"成功!":"失败!"));
esClient.close();
}
查看索引
public void getIndex() throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//查看 user 索引
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse response = esClient.indices().get(request, RequestOptions.DEFAULT);
System.out.println("aliases: " + response.getAliases());
System.out.println("mappings: " + response.getMappings());
System.out.println("settings: " + response.getSettings());
esClient.close();
}
删除索引
public void delete() throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
DeleteIndexRequest request = new DeleteIndexRequest("car");
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
boolean acknowledged = response.isAcknowledged();
System.out.println("删除索引" + (acknowledged?"成功!":"失败!"));
esClient.close();
}
创建文档
//单条新增&循环新增
public void createDocument() throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//单条新增
/*IndexRequest request = new IndexRequest("student").id("1001");
request.source(JSONUtil.toJsonStr(new Student("1001", "1001", 1001, 1001)), XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getId());
System.out.println(response.getResult());
System.out.println(response.getIndex());
System.out.println(response.getType());
System.out.println(response.getVersion());*/
//循环新增
List<String> list = Arrays.asList("1001_蔡卓妍_38_女", "1002_杨幂_36_女", "1003_赵丽颖_32_女", "1004_高圆圆_40_女", "1005_江莱_37_女");
for (String item : list) {
String[] split = item.split("_");
//id 可以设置自定义id, 不设置的话 es 将自动生成id
IndexRequest request = new IndexRequest().index("user").id(split[0]);
request.source(JSONUtil.toJsonStr(new User(split[0], split[1], Integer.parseInt(split[2]), split[3])), XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println("_index:" + response.getIndex());
System.out.println("_id:" + response.getId());
System.out.println("_result:" + response.getResult());
System.out.println("------------------------------------");
}
esClient.close();
}
//批量新增
public void createDoc() throws IOException{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//批量新增
BulkRequest request = new BulkRequest();
List<Integer> list = Arrays.asList(1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009);
for (Integer integer : list) {
request.add(new IndexRequest().index("student").id(integer.toString()).source(JSONUtil.toJsonStr(new Student(integer.toString(),integer.toString(), integer, integer)), XContentType.JSON));
}
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
System.out.println("took:" + responses.getTook());
System.out.println("items:" + responses.getItems());
client.close();
}
通过id查看文档
public void getDocument() throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
String indexName = "user";
List<String> list = Arrays.asList("1001_蔡卓妍_38_女", "1002_杨幂_36_女", "1003_赵丽颖_32_女", "1004_高圆圆_40_女", "1005_江莱_37_女");
for (String item : list) {
String[] split = item.split("_");
GetRequest request = new GetRequest().index(indexName).id(split[0]);
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println("id: " + response.getId());
System.out.println("index: " + response.getIndex());
System.out.println("type: " + response.getType());
System.out.println("source: " + response.getSource());
System.out.println("version: "+ response.getVersion());
System.out.println("----------------------------------");
}
esClient.close();
}
//查看索引下所有数据
public void queryAllData() throws Exception{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest searchRequest = new SearchRequest("student");
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(response.getTook());
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
修改文档
//tips: 修改数据的某个属性; 数据所有属性都要修改, 设置即可, 原数据将被覆盖;
public void updateDocument() throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
UpdateRequest request = new UpdateRequest().index("user").id("1001").doc(XContentType.JSON, "sex", "女");
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println("id: " + response.getId());
System.out.println("index: " + response.getIndex());
System.out.println("result: " + response.getResult());
System.out.println("version: " + response.getVersion());
esClient.close();
}
//批量修改
public void updateDoc() throws IOException{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
BulkRequest request = new BulkRequest();
request.add(new UpdateRequest().index("student").id("1001").doc(XContentType.JSON, "name", "1001"));
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(responses.getTook());
System.out.println(responses.getItems());
System.out.println(JSONUtil.toJsonStr(responses));
client.close();
}
通过id删除文档
public void deleteDocument() throws IOException{
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
DeleteRequest request = new DeleteRequest().index("user").id("1005");
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println("id: "+ response.getId());
System.out.println("index: "+ response.getIndex());
System.out.println("result: "+ response.getResult());
System.out.println("version: "+ response.getVersion());
esClient.close();
}
//批量删除文档
public void deleteDoc() throws IOException{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
BulkRequest request = new BulkRequest();
List<Integer> list = Arrays.asList(1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009);
for (Integer integer : list) {
request.add(new DeleteRequest().index("student").id(integer.toString()));
}
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
System.out.println("took:" + responses.getTook());
System.out.println("items:" + responses.getItems());
client.close();
}
条件查询
//tips: 查询指定索引下所有(文档)数据;(无条件,即: 所有数据)
public void query() throws IOException{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest request = new SearchRequest();//可以注释掉下面这行,student放到这的括号里
request.indices("student");
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getHits().getTotalHits());
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
分页查询
public void queryByPage() throws IOException{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest request = new SearchRequest();
request.indices("student");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
sourceBuilder.from(0);//起始页码
sourceBuilder.size(2);//每页显示数据数
sourceBuilder.sort("age", SortOrder.DESC);//排序;对age(年龄)字段进行倒序排序
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(response.getHits().getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
查询过滤字段
public void queryPageByCondition() throws IOException{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest request = new SearchRequest();
request.indices("student");
SearchSourceBuilder source = new SearchSourceBuilder();
source.query(QueryBuilders.termQuery("name", "1007"));
String[] includes = {"no", "name", "age", "sorted"};
String[] excludes = {};
source.fetchSource(includes, excludes);
request.source(source);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(response.getHits().getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
条件查询
//tips: 有条件的查询; 查询所有男生 的信息
public void query() throws Exception {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest searchRequest = new SearchRequest("student");
SearchSourceBuilder source = new SearchSourceBuilder();
source.query(QueryBuilders.termQuery("gender", "男"));
searchRequest.source(source);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getHits().getTotalHits());
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
聚合查询
student索引的所有数据:
{"no":"1001","name":"张三","gender":"男","age":21,"skill":78}
{"no":"1002","name":"李四","gender":"男","age":21,"skill":80}
{"no":"1003","name":"王五","gender":"男","age":24,"skill":90}
{"no":"1004","name":"赵六","gender":"男","age":20,"skill":90}
{"no":"1005","name":"小花","gender":"女","age":26,"skill":96}
{"no":"1006","name":"小丽","gender":"女","age":24,"skill":89}
{"no":"1007","name":"小翠","gender":"女","age":27,"skill":86}
{"no":"1008","name":"小妹","gender":"女","age":25,"skill":88}
{"no":"1009","name":"小娟","gender":"女","age":28,"skill":84}
public void queryAggregation() throws Exception {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest searchRequest = new SearchRequest("student");
SearchSourceBuilder source = new SearchSourceBuilder();
source.aggregation(AggregationBuilders.terms("genderTerm").field("gender.keyword")//聚合名称(自定义): genderTerm; 字段: 按照性别字段聚合; 类似于mysql的group by
.subAggregation(AggregationBuilders.count("genderCount").field("gender.keyword"))//分组数量
.subAggregation(AggregationBuilders.avg("ageAvg").field("age"))//分组数据的平均年龄
.subAggregation(AggregationBuilders.min("minAge").field("age"))//分组数据的最小年龄
.subAggregation(AggregationBuilders.topHits("details").sort("age", SortOrder.ASC).size(10)));//每组详情数据,并设置按年龄升序排序
searchRequest.source(source);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = response.getAggregations();
ParsedStringTerms terms = aggregations.get("genderTerm");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
String key = bucket.getKeyAsString();
System.out.println("key: " + key);
Aggregations aggregations1 = bucket.getAggregations();
ParsedValueCount count = aggregations1.get("genderCount");
System.out.println("统计本组总个数: " + count.getValueAsString());
ParsedAvg avg = aggregations1.get("ageAvg");
System.out.println("统计本组年龄平均数: " + avg.getValue());
ParsedMin minAge = aggregations1.get("minAge");
System.out.println("统计本组年龄最小数: " + minAge.getValueAsString());
ParsedTopHits topHits = aggregations1.get("details");
SearchHits hits = topHits.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
client.close();
}
运行结果:
key: 女
统计本组总个数: 5.0
统计本组年龄平均数: 26.0
统计本组年龄最小数: 24.0
{"no":"1006","name":"小丽","gender":"女","age":24,"skill":89}
{"no":"1008","name":"小妹","gender":"女","age":25,"skill":88}
{"no":"1005","name":"小花","gender":"女","age":26,"skill":96}
{"no":"1007","name":"小翠","gender":"女","age":27,"skill":86}
{"no":"1009","name":"小娟","gender":"女","age":28,"skill":84}
key: 男
统计本组总个数: 4.0
统计本组年龄平均数: 21.5
统计本组年龄最小数: 20.0
{"no":"1004","name":"赵六","gender":"男","age":20,"skill":90}
{"no":"1001","name":"张三","gender":"男","age":21,"skill":78}
{"no":"1002","name":"李四","gender":"男","age":21,"skill":80}
{"no":"1003","name":"王五","gender":"男","age":24,"skill":90}