引入jar包依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<!-- es客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
调用API创建Index
查询和删除Index
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
System.out.println("Aliases = " + response.getAliases());
System.out.println("Mappings = " + response.getMappings());
// 删除
DeleteIndexRequest deleteRequest = new DeleteIndexRequest("user");
AcknowledgedResponse deleteResp = client.indices().delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println("deleteResp = " + deleteResp.toString());
创建和修改文档
User user = new User();
user.setName("刘德华");
user.setAge(60L);
user.setSex("男");
IndexRequest request = new IndexRequest();
request.index("user").id("1");
// 插入es需要转成json格式
request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
// 修改
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("user").id("1");
updateRequest.doc(XContentType.JSON, "age", 65L);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(updateResponse.getResult());
查询文档和删除
// 查询
GetRequest request = new GetRequest();
request.index("user").id("1");
GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
System.out.println(documentFields.getSource());
// 删除
DeleteRequest deleteRequest = new DeleteRequest();
deleteRequest.index("user").id("1");
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.getResult());
批量新增和删除
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest().index("user").id("2").source(JSON.toJSONString(new User("梁朝伟", "男", 62L)), XContentType.JSON));
bulkRequest.add(new IndexRequest().index("user").id("3").source(JSON.toJSONString(new User("黎明", "男", 58L)), XContentType.JSON));
bulkRequest.add(new IndexRequest().index("user").id("4").source(JSON.toJSONString(new User("郭富城", "男", 55L)),XContentType.JSON));
bulkRequest.add(new IndexRequest().index("user").id("5").source(JSON.toJSONString(new User("刘亦菲", "女", 28L)), XContentType.JSON));
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
// 批量删除
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new DeleteRequest().index("user").id("2"));
bulkRequest.add(new DeleteRequest().index("user").id("3"));
bulkRequest.add(new DeleteRequest().index("user").id("4"));
bulkRequest.add(new DeleteRequest().index("user").id("5"));
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
全量查询
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user")
.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsString());
}
对于其他的查询,需要修改“QueryBuilders.matchAllQuery()”文章来源:https://www.toymoban.com/news/detail-597015.html
条件查询、排序、分页、指定查询字段
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
// 排除的字段
String[] excludes = {};
// 查询的字段
String[] includes = {"name"};
searchRequest.source(new SearchSourceBuilder()
// 条件查询
.query(QueryBuilders.termQuery("sex", "男"))
// 指定查询的字段
.fetchSource(includes, excludes)
// 排序
.sort("age", SortOrder.DESC)
// 分页
.from(0).size(2));
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsString());
}
组合查询、高亮显示
注意:中文自动分词,可模糊搜索。如:文章来源地址https://www.toymoban.com/news/detail-597015.html
must(QueryBuilders.matchQuery("name", "亦菲"))
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
searchRequest.source(
new SearchSourceBuilder().query(
QueryBuilders.boolQuery()
// and条件
//.must(QueryBuilders.matchQuery("age", 28))
//.must(QueryBuilders.matchQuery("sex", "女"))
//.must(QueryBuilders.matchQuery("name", "亦菲"))
// or条件
.should(QueryBuilders.matchQuery("age", 28))
.should(QueryBuilders.matchQuery("age", 58))
).highlighter(
new HighlightBuilder()
.preTags("<font color='red'>")
.postTags("</font>")
.field("name")
)
);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsString());
}
范围查询
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
searchRequest.source(
new SearchSourceBuilder().query(
QueryBuilders.rangeQuery("age").gte(18).lt(30)
)
);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getSourceAsString());
}
聚合查询
// 聚合查询
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
searchRequest.source(
new SearchSourceBuilder().aggregation(
// 最小值
//AggregationBuilders.min("minAge").field("age")
// 分组统计
AggregationBuilders.terms("ageGroup").field("age")
)
);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
到了这里,关于ES基本操作(JavaAPI篇)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!