Elasticsearch-03-JavaApi以及springboot中操作-RestHighLevelClient
时间:2022-11-14 14:00:01
文章目录
- 前言
- 一:导包
- 二、客户端对象
- 三、客户端操作
-
- 1:索引操作
-
- 1:创建索引
- 2:查看索引
- 3:删除索引
- 2:文档操作
-
- 1:创建文档
- 2:查询文档
- 3:删除文档
- 4:批量新增
- 5:批量删除
- 3:高级查询
-
- 1:全量查询
- 2:条件查询-term 查询条件是关键字
- 3:分词查询-match
- 4:分页查询-from&size
- 5:数据排序-sort
- 6:过滤字段-fetch
- 7:Bool查询-bool
- 8:范围查询-range
- 9:模糊查询-fuzzi
- 10:聚合查询
- 11:分组查询
- 四:springboot中使用HighLevelClient 操作es
-
- 1:导包
- 2:配置config,链接es
- 三、依赖注入使用
前言
Elasticsearch 软件是由 Java 语言开发也可以通过 JavaAPI 的方式对 Elasticsearch
访问服务
之前是对ES创建/查看/删除索引、创建定义映射、创建/查看/修改/删除文档的操作有一定的了解,但通过Postman JSON实现串法
之后还是对的ES操作索引、映射和文档,但方法被替换Java API。
一:导包
<dependencies> <dependency> <groupId>org.elasticsearchgroupId> <artifactId>elasticsearchartifactId> <version>7.8.0version> dependency> <dependency> <groupId>org.elasticsearch.clientgroupId> <artifactId>elasticsearch-rest-high-level-clientartifactId> <version>7.8.0version> dependency> <dependency> <groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.9version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.47version>
dependency>
dependencies>
二:客户端对象
创建 com.atguigu.es.test.Elasticsearch01_Client 类,代码中创建 Elasticsearch 客户端对象
因为早期版本的客户端对象已经不再推荐使用,且在未来版本中会被删除,所以这里我们采
用高级 REST 客户端对象-RestHighLevelClient
package es;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
/** 创建es客户端 * @author wkl * @create 2022-06-28 14:56 */
public class EsClient {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//关闭ES客户端
esClient.close();
}
}
三:客户端操作
1:索引操作
1:创建索引
/** 索引类操作 * @author wkl * @create 2022-06-28 15:04 */
public class CreateIndex {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建索引对象
CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
//发送请求
CreateIndexResponse createIndexResponse = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("索引操作:"+acknowledged);
//关闭ES客户端
esClient.close();
}
}
2:查看索引
/** * @author wkl * @create 2022-06-28 15:15 */
public class GerIndex {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getDataStreams());
System.out.println(getIndexResponse.getSettings());
//关闭ES客户端
esClient.close();
}
}
3:删除索引
/** * @author wkl * @create 2022-06-28 15:20 */
public class DeleteIndex {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("student");
AcknowledgedResponse delete = esClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
//关闭ES客户端
esClient.close();
}
}
2:文档操作
1:创建文档
1:创建实体-我这里使用了lombok代替get,set,这个就不介绍了哈
/**
- @author wkl
- @create 2022-06-28 15:24
*/
@Data
public class User {
private String name;
private String sex;
private int age;
}
2:创建文档
/** * @author wkl * @create 2022-06-28 15:25 */
public class CreateDocument {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建文档对象
IndexRequest indexRequest = new IndexRequest();
//设置索引及索引中文档的唯一性标识id(如果不指定,则ES会默认随机生成一个id,建议指定id值,也是为了后续查找方便)
indexRequest.index("user").id("1");
//创建数据对象
User user = new User();
user.setName("张三");
user.setSex("男");
user.setAge(35);
user.setBir(new Date());
//向es中插入数据,此数据必须是json格式,并且es在后台动态创建映射
indexRequest.source(JSONObject.toJSONString(user), XContentType.JSON);
//发送请求
IndexResponse index = esClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getId());
System.out.println(index.getIndex());
System.out.println(index.getResult());
//关闭ES客户端
esClient.close();
}
}
2:查询文档
/** * @author wkl * @create 2022-06-28 16:02 */
public class UpdateDocument {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("user").id("1");
//将修改后的内容,以JSON格式写入请求体中
updateRequest.doc(XContentType.JSON,"age",26);
//发送请求 --- 获取响应
UpdateResponse update = esClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.getResult());
//关闭ES客户端
esClient.close();
}
}
3:删除文档
/** * @author wkl * @create 2022-06-28 16:07 */
public class DeleteDoucument {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建删除文档请求
DeleteRequest deleteRequest = new DeleteRequest();
//设置删除id
deleteRequest.index("user").id("1");
//发送请求
DeleteResponse delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.getResult());
//关闭ES客户端
esClient.close();
}
}
4:批量新增
public class BatchCreateDocument {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建请求对象
BulkRequest bulkRequest = new BulkRequest();
//设置请求参数
bulkRequest.add(new IndexRequest().index("user").id("2").source(JSONObject.toJSONString(new User("李四","男",20,new Date())), XContentType.JSON));
bulkRequest.add(new IndexRequest().index("user").id("3").source(JSONObject.toJSONString(new User("小花","女",18,new Date())), XContentType.JSON));
bulkRequest.add(new IndexRequest().index("user").id("4").source(JSONObject.toJSONString(new User("小雪","女",24,new Date())), XContentType.JSON));
//发送请求
BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.getTook());
//关闭ES客户端
esClient.close();
}
}
5:批量删除
/** * @author wkl * @create 2022-06-28 16:12 */
public class BatchDleteDocument {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建请求对象
BulkRequest bulkRequest = new BulkRequest();
//设置请求参数
bulkRequest.add(new DeleteRequest().index("user").id("1"));
bulkRequest.add(new DeleteRequest().index("user").id("2"));
//发送请求
BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.getItems());
//关闭ES客户端
esClient.close();
}
}
3:高级查询
1:全量查询
/** * @author wkl * @create 2022-06-28 16:29 */
public class Match_allQuery {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建搜索对象
SearchRequest searchRequest = new SearchRequest(<