1. 创建Maven项目,添加依赖
<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>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- junit 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2. 创建客户端对象
public class ESTest_Client {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//关闭ES客户端
esClient.close();
}
}
3. 索引操作
3.1 创建索引
public class ESTest_Index_Create {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//创建索引
CreateIndexRequest request = new CreateIndexRequest("users");
CreateIndexResponse response = esClient.indices().create(request, RequestOptions.DEFAULT);
//响应状态
boolean acknowledged = response.isAcknowledged();
System.out.println("索引操作:" + acknowledged);
//关闭ES客户端
esClient.close();
}
}
前往postman查看有没有创建成功
3.2 查看索引
public class ESTest_Index_Search {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//查询索引
GetIndexRequest request = new GetIndexRequest("users");
GetIndexResponse response = esClient.indices().get(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(response.getAliases());
System.out.println(response.getMappings());
System.out.println(response.getSettings());
//关闭ES客户端
esClient.close();
}
}
3.3 删除索引
public class ESTest_Index_Delete {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//查询索引
DeleteIndexRequest request = new DeleteIndexRequest("users");
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(response.isAcknowledged());
//关闭ES客户端
esClient.close();
}
}
前往postman确认 已经被删除
4. 文档操作
4.1 新增文档
创建数据模型
public class Users {
private String name;
private String sex;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
public class ESTest_Doc_Insert {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//插入数据
IndexRequest request = new IndexRequest();
request.index("users").id("1001");
Users users = new Users();
users.setName("zhangsan");
users.setAge(22);
users.setSex("男");
//向ES插入数据,必须将数据转换成JSON格式
ObjectMapper mapper = new ObjectMapper();
String usersJson = mapper.writeValueAsString(users);
request.source(usersJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
//关闭ES客户端
esClient.close();
}
}
4.2 修改文档
public class ESTest_Doc_Update {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//修改数据
UpdateRequest request = new UpdateRequest();
request.index("users").id("1001");
request.doc(XContentType.JSON,"name","李四");
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
//关闭ES客户端
esClient.close();
}
}
4.3 查询文档
public class ESTest_Doc_Get {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//查询数据
GetRequest request = new GetRequest();
request.index("users").id("1001");
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
//关闭ES客户端
esClient.close();
}
}
4.4 删除文档
public class ESTest_Doc_Delete {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
DeleteRequest request = new DeleteRequest();
request.index("users").id("1001");
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
//关闭ES客户端
esClient.close();
}
}
4.5 批量新增
public class ESTest_Doc_Insert_Batch {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//批量插入数据
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("users").id("1001").source(XContentType.JSON,"name","张三"));
request.add(new IndexRequest().index("users").id("1002").source(XContentType.JSON,"name","李四"));
request.add(new IndexRequest().index("users").id("1003").source(XContentType.JSON,"name","王五"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
//关闭ES客户端
esClient.close();
}
}
4.6 批量删除
public class ESTest_Doc_Delete_Batch {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//批量删除数据
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());
//关闭ES客户端
esClient.close();
}
}
4.7 全量查询
public class ESTest_Doc_Query {
public static void main(String[] args) throws IOException {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("hadoop102",9200))
);
//全量查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
//关闭ES客户端
esClient.close();
}
}
4.8 条件查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件termQuery
request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.9 分页查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//(当前页码-1)* 每页显示的条数
builder.from(0);
builder.size(2); //每页两条数据
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.10 查询排序
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
builder.sort("age", SortOrder.DESC);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.11 过滤字段
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
String[] excludes = {};
String[] includes = {"name"};
builder.fetchSource(includes,excludes);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.11 组合查询
//6. 组合查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// boolQueryBuilder.must(QueryBuilders.matchQuery("age",30));
// boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));
// boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex","男"));
boolQueryBuilder.should(QueryBuilders.matchQuery("age",30));
boolQueryBuilder.should(QueryBuilders.matchQuery("age",40));
builder.query(boolQueryBuilder);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.12 范围查询
//7. 范围查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
rangeQuery.gte(30);
rangeQuery.lte(40);
builder.query(rangeQuery);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.13 模糊查询
//7. 模糊查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "王五").fuzziness(Fuzziness.ONE);//差一个字符
builder.query(fuzziness);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.14 高亮查询
//8. 高亮查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "张三");
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font color ='red'>");
highlightBuilder.postTags("</font>");
highlightBuilder.field("name");
builder.highlighter(highlightBuilder);
builder.query(termQueryBuilder);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.15 聚合查询
//9. 聚合查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
builder.aggregation(aggregationBuilder);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
4.16 分组查询
//10. 分组查询
SearchRequest request = new SearchRequest();
request.indices("users");
//构造查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
builder.aggregation(aggregationBuilder);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(response.getTook());
System.out.println(hits.getTotalHits());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}