需要添加下面的maven依赖
org.elasticsearch
elasticsearch
7.13.4
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.13.4
org.apache.logging.log4j
log4j-api
2.8.2
org.apache.logging.log4j
log4j-core
2.8.2
com.fasterxml.jackson.core
jackson-databind
2.9.9
junit
junit
4.12
======================================================================
public static void Index_Create() throws Exception{
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
// 创建索引
CreateIndexRequest request = new CreateIndexRequest(“user”);
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
//响应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(“索引操作:”+ acknowledged);
//关闭ES客户端
esClient.close();
}
//查询索引
public static void Index_Search() throws Exception{
//创建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.getSettings());
//关闭ES客户端
esClient.close();
}
//删除索引
public static void Index_Delete() throws Exception{
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”,9200,“http”))
);
// 查询索引
DeleteIndexRequest request = new DeleteIndexRequest(“user”);
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(response.isAcknowledged());
//关闭ES客户端
esClient.close();
}
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 插入数据
IndexRequest request = new IndexRequest();
request.index(“user”).id(“1001”);
User user = new User();
user.setName(“zhangsan”);
user.setAge(30);
user.setSex(“男”);
// 向ES插入数据,必须将数据转换位JSON格式
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
request.source(userJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
批量插入数据:
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 批量插入数据
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index(“user”).id(“1001”).source(XContentType.JSON, “name”, “zhangsan”, “age”,30,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1002”).source(XContentType.JSON, “name”, “lisi”, “age”,30,“sex”,“女”));
request.add(new IndexRequest().index(“user”).id(“1003”).source(XContentType.JSON, “name”, “wangwu”, “age”,40,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1004”).source(XContentType.JSON, “name”, “wangwu1”, “age”,40,“sex”,“女”));
request.add(new IndexRequest().index(“user”).id(“1005”).source(XContentType.JSON, “name”, “wangwu2”, “age”,50,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1006”).source(XContentType.JSON, “name”, “wangwu3”, “age”,50,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1007”).source(XContentType.JSON, “name”, “wangwu44”, “age”,60,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1008”).source(XContentType.JSON, “name”, “wangwu555”, “age”,60,“sex”,“男”));
request.add(new IndexRequest().index(“user”).id(“1009”).source(XContentType.JSON, “name”, “wangwu66666”, “age”,60,“sex”,“男”));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
esClient.close();
}
将上面的数据中的性别改成女
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 修改数据
UpdateRequest request = new UpdateRequest();
request.index(“user”).id(“1001”);
request.doc(XContentType.JSON, “sex”, “女”);
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 查询数据
GetRequest request = new GetRequest();
request.index(“user”).id(“1001”);
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
esClient.close();
}
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
DeleteRequest request = new DeleteRequest();
request.index(“user”).id(“1001”);
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
esClient.close();
}
批量删除:
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 批量删除数据
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index(“user”).id(“1001”));
request.add(new DeleteRequest().index(“user”).id(“1002”));
request.add(new DeleteRequest().index(“user”).id(“1003”));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
esClient.close();
}
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(“localhost”, 9200, “http”))
);
// 1. 查询索引中全部的数据
// SearchRequest request = new SearchRequest();
// request.indices(“user”);
// request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 2. 条件查询 : termQuery
// SearchRequest request = new SearchRequest();
// request.indices(“user”);
// request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery(“age”, 30)));
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// 3. 分页查询
// SearchRequest request = new SearchRequest();
// request.indices(“user”);
// SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
// // (当前页码-1)*每页显示数据条数
// builder.from(2);
// builder.size(2);
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// // 4. 查询排序
总结
总体来说,如果你想转行从事程序员的工作,Java开发一定可以作为你的第一选择。但是不管你选择什么编程语言,提升自己的硬件实力才是拿高薪的唯一手段。
如果你以这份学习路线来学习,你会有一个比较系统化的知识网络,也不至于把知识学习得很零散。我个人是完全不建议刚开始就看《Java编程思想》、《Java核心技术》这些书籍,看完你肯定会放弃学习。建议可以看一些视频来学习,当自己能上手再买这些书看又是非常有收获的事了。
了解详情https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB
t, RequestOptions.DEFAULT);
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for ( SearchHit hit : hits ) {
// System.out.println(hit.getSourceAsString());
// }
// // 4. 查询排序
总结
总体来说,如果你想转行从事程序员的工作,Java开发一定可以作为你的第一选择。但是不管你选择什么编程语言,提升自己的硬件实力才是拿高薪的唯一手段。
如果你以这份学习路线来学习,你会有一个比较系统化的知识网络,也不至于把知识学习得很零散。我个人是完全不建议刚开始就看《Java编程思想》、《Java核心技术》这些书籍,看完你肯定会放弃学习。建议可以看一些视频来学习,当自己能上手再买这些书看又是非常有收获的事了。
[外链图片转存中…(img-AuJwgyH6-1724127360626)]
了解详情https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB