es是面向文档型数据库,一条数据就是一个文档。
http://localhost:9200/_cat/indices?v
http://localhost:9200/new_index/_doc
http://localhost:9200/new_index/_doc/1001
http://localhost:9200/new_index/_doc/_search
http://localhost:9200/new_index/_doc/1001
http://localhost:9200/new_index/_update/1001
http://localhost:9200/new_index/_doc/1001
http://localhost:9200/new_index/_search?q=category:分类
引入依赖:
<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.17.2version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.13.3version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.13.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
dependency>
dependencies>
构建es操作客户端
/**
* @Description es客户端
* @date 2022/7/14 9:33
*/
public class ES_Client {
private static final String HOST_NAME = "localhost";
private static final Integer PORT = 9200;
private static final String SCHEME = "http";
public static void main(String[] args) throws IOException {
// 创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(HOST_NAME,PORT,SCHEME)));
// 关闭es客户端
esClient.close();
}
/**
* @Description
* @date 2022/7/14 14:42
*/
public class ConnESUtil {
private static final String HOST_NAME = "localhost";
private static final Integer PORT = 9200;
private static final String SCHEME = "http";
public static RestHighLevelClient getEsClient(){
return new RestHighLevelClient(RestClient.builder(new HttpHost(HOST_NAME, PORT, SCHEME)));
}
}
创建索引:
/**
* 创建索引
*/
private static void createIndices() throws IOException {
CreateIndexRequest user = new CreateIndexRequest("user_2");
CreateIndexResponse response = esClient.indices().create(user, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
查询索引:
/**
* 查询索引信息
* @throws IOException
*/
private static void getIndices() throws IOException {
GetIndexRequest request = new GetIndexRequest("user_2");
GetIndexResponse response = ES_Client.esClient.indices().get(request, RequestOptions.DEFAULT);
// 获取当前索引的信息
System.out.println(response.getAliases());
System.out.println(response.getMappings());
System.out.println(response.getSettings() );
}
删除索引:
/**
* 删除索引
* @throws IOException
*/
private static void delIndices() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("user_2");
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
封装数据模型:
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User3 {
private String name;
private String sex;
private Integer age;
}
新增文档:
/**
* 添加文档
* @throws IOException
*/
private static void addDoc() throws IOException {
IndexRequest request = new IndexRequest();
// 指定索引名称 和 当前文档的id
request.index("user3").id("1001");
User3 user = new User3("a", "男" ,30);
// 对象转json
ObjectMapper mapper = new ObjectMapper();
String jsonUser = mapper.writeValueAsString(user);
// 指定请求传入数据 和 数据类型
request.source(jsonUser, XContentType.JSON);
// 创建
IndexResponse res = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(res.getResult());
}
修改文档:
/**
* 修改文档
* @throws IOException
*/
private static void updateDoc() throws IOException {
UpdateRequest request = new UpdateRequest();
// 指定要修改的索引 和 id
request.index("user3").id("1001");
// 传入要修改的数据
request.doc(XContentType.JSON, "sex", "女");
UpdateResponse res = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(res.getResult());
}
查询文档:
/**
* 查询文档
* @throws IOException
*/
private static void getDoc() throws IOException {
GetRequest request = new GetRequest();
// 指定索引 和 id
request.index("user3").id("1001");
GetResponse res = esClient.get(request, RequestOptions.DEFAULT);
System.out.println(res.getSourceAsString());
}
删除文档:
/**
* 删除文档
* @throws IOException
*/
private static void delDoc() throws IOException {
DeleteRequest request = new DeleteRequest();
// 指定索引 和 id
request.index("user3").id("1001");
DeleteResponse res = esClient.delete(request, RequestOptions.DEFAULT);
}
批量新增:
/**
* 批量文档新增
* @throws IOException
*/
private static void addDocs() throws IOException {
BulkRequest request = new BulkRequest();
// 添加多个请求,同时发送
// 这里的 source 方法,必须要使用 v : k 的形式传入一个或者多个,不能使用对象转json字符串方法。
request.add(new IndexRequest().index("user3").id("1002").source(XContentType.JSON, "name", "b"));
request.add(new IndexRequest().index("user3").id("1003").source(XContentType.JSON, "name", "c"));
request.add(new IndexRequest().index("user3").id("1004").source(XContentType.JSON, "name", "d"));
BulkResponse res = esClient.bulk(request, RequestOptions.DEFAULT);
// 查看耗时
System.out.println(res.getTook());
}
批量删除:
/**
* 批量删除文档
* @throws IOException
*/
private static void delDocs() throws IOException {
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("user3").id("1002"));
request.add(new DeleteRequest().index("user3").id("1003"));
request.add(new DeleteRequest().index("user3").id("1004"));
BulkResponse res = esClient.bulk(request, RequestOptions.DEFAULT);
// 查看耗时
System.out.println(res.getTook());
}
数据准备,以上方批量插入的形式插入多条可供测试的数据到es:
BulkRequest request = new BulkRequest();
// 添加多个请求,同时发送
// 这里的 source 方法,必须要使用 v : k 的形式传入一个或者多个,不能使用对象转json字符串方法。
request.add(new IndexRequest().index("user3").id("1001").source(XContentType.JSON, "name", "zhangsan", "age", "30", "sex", "男"));
request.add(new IndexRequest().index("user3").id("1002").source(XContentType.JSON, "name", "lisi", "age", "30", "sex", "女"));
request.add(new IndexRequest().index("user3").id("1003").source(XContentType.JSON, "name", "wangwu", "age", "40", "sex", "男"));
request.add(new IndexRequest().index("user3").id("1004").source(XContentType.JSON, "name", "wangwu1", "age", "40", "sex", "女"));
request.add(new IndexRequest().index("user3").id("1005").source(XContentType.JSON, "name", "wangwu2", "age", "50", "sex", "男"));
request.add(new IndexRequest().index("user3").id("1006").source(XContentType.JSON, "name", "wangwu3", "age", "50", "sex", "男"));
BulkResponse res = esClient.bulk(request, RequestOptions.DEFAULT);
// 查看耗时
System.out.println(res.getTook());
查询索引下全部文档:
/**
* 查询指定索引下全部文档信息
* @throws IOException
*/
private static void list() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user3");
// 构建查询参数
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
根据传入参数查询:
/**
* 根据查询文档信息
* @param age
* @throws IOException
*/
private static void getByAge(String age) throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user3");
// 构建查询参数
request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", age)));
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
分页查询:
/**
* 分页查询
* @param page
* @param size
* @throws IOException
*/
private static void list(Integer page, Integer size) throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user3");
// 构建查询参数
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.from(page);
query.size(size);
request.source(query);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
排序查出的数据:
/**
* 排序查询
* @param sortedBy
* @param order
* @throws IOException
*/
private static void list(String sortedBy, SortOrder order) throws IOException{
SearchRequest request = new SearchRequest();
request.indices("user3");
// 构建查询参数
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.sort(sortedBy, order);
request.source(query);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
过滤字段:
/**
* 过滤字段
* @param excludes 要排除的字段
* @param includes 包含的字段
* @throws IOException
*/
private static void filterCol(String[] excludes, String[] includes) throws IOException{
SearchRequest request = new SearchRequest();
request.indices("user3");
// 构建查询参数
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.fetchSource(excludes, includes);
request.source(query);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
多条件查询:
/**
* 多字段组合查询
* @throws IOException
*/
private static void getByInfo() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user3");
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.matchQuery("age", "30"));
boolQuery.must(QueryBuilders.matchQuery("sex", "男"));
// boolQuery.should(QueryBuilders.matchQuery("age", "30"));
// boolQuery.should(QueryBuilders.matchQuery("sex", "男"));
// 构建查询参数
builder.query(boolQuery);
request.source(builder);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
范围查询:
/**
* 范围查询
* @throws IOException
*/
private static void getByRange() throws IOException{
SearchRequest request = new SearchRequest();
request.indices("user3");
SearchSourceBuilder builder = new SearchSourceBuilder();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
builder.query(rangeQuery);
rangeQuery.gt("30");
request.source(builder);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
模糊查询:
/**
* 模糊查询
* @throws IOException
*/
private static void getLike() throws IOException{
SearchRequest request = new SearchRequest();
request.indices("user3");
SearchSourceBuilder builder = new SearchSourceBuilder();
FuzzyQueryBuilder fuzzyQuery =
QueryBuilders.fuzzyQuery("name","wangwu")
.fuzziness(Fuzziness.ONE); //指定模糊查询精度:相差小于等于几个
builder.query(fuzzyQuery);
request.source(builder);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
// 遍历返回信息
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}
高亮查询:
/**
* 高亮查询
* @throws IOException
*/
private static void getHighLight() throws IOException{
SearchRequest request = new SearchRequest();
request.indices("user3");
SearchSourceBuilder builder = new SearchSourceBuilder();
TermQueryBuilder queryBuilder =
QueryBuilders
.termQuery("name","zhangsan");// 指定高亮字段和对应值
// 高亮builder
HighlightBuilder highlightBuilder = new HighlightBuilder();
// 指定前缀和后缀标签
highlightBuilder.preTags("");
highlightBuilder.postTags("");
// 指定高亮字段
highlightBuilder.field("name");
builder.highlighter(highlightBuilder);
builder.query(queryBuilder);
request.source(builder);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
System.out.println(res);
}
聚合查询(获取最大值):数据都在res对象中
/**
* 获取最大年龄数据
* @throws IOException
*/
private static void getMax() throws IOException{
SearchRequest request = new SearchRequest();
request.indices("user3");
SearchSourceBuilder builder = new SearchSourceBuilder();
// 指定聚合要求的内容 和 字段
AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
builder.aggregation(aggregationBuilder);
request.source(builder);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
System.out.println(res);
}
分组查询:
/**
* 分组查询
* @throws IOException
*/
private static void getGroupByAge() throws IOException{
SearchRequest request = new SearchRequest();
request.indices("user3");
SearchSourceBuilder builder = new SearchSourceBuilder();
// 指定聚合要求的内容 和 字段
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("count").field("age");
builder.aggregation(aggregationBuilder);
request.source(builder);
// 执行查询
SearchResponse res = esClient.search(request, RequestOptions.DEFAULT);
// 返回数据
SearchHits hits = res.getHits();
// 查看总条数
System.out.println(hits.getTotalHits());
System.out.println(res);
}