http 操作
http - 创建索引
PUT http://127.0.0.1:9200/shopping
http - 查看所有索引
GET http://127.0.0.1:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open shopping ol-YolUMRRapiJ51ESN83Q 1 1 4 0 6.2kb 6.2kb
表头 | 含义 |
---|---|
health | 当前服务器健康状态:green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) |
status | 索引打开、关闭状态 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主分片数量 |
rep | 副本数量 |
docs.count | 可用文档数量 |
docs.deleted | 文档删除状态(逻辑删除) |
store.size | 主分片和副分片整体占空间大小 |
pri.store.size | 主分片占空间大小 |
http - 查看单个索引
GET http://127.0.0.1:9200/shopping
http - 删除索引
DELETE http://127.0.0.1:9200/shopping
http - 创建索引
指定ID为1002
PUT http://127.0.0.1:9200/shopping/_doc/1002
{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
注:PUT具有幂等性,POST不具有幂等性
不指定ID
POST http://127.0.0.1:9200/shopping/_doc
{
"title":"苹果手机",
"category":"苹果",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price": 6999.00
}
http - 查询所有文档
GET http://127.0.0.1:9200/shopping/_search
http - 主键查询
GET http://127.0.0.1:9200/shopping/_doc/1001
http - 全量更新
PUT http://127.0.0.1:9200/shopping/_doc/1001
{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":1999.00
}
http - 局部更新
POST http://127.0.0.1:9200/shopping/_update/1002
{
"doc" : {
"price" : 1999
}
}
http - 文档删除
DELETE http://127.0.0.1:9200/shopping/_doc/1001
http - 条件查询
GET http://127.0.0.1:9200/shopping/_search?q=category:小米
http - 请求体查询
GET http://127.0.0.1:9200/shopping/_search
{
"query" : {
"match" : {
"category" : "小米"
}
}
}
http - 全查询
GET http://127.0.0.1:9200/shopping/_search
{
"query" : {
"match_all" : {
}
}
}
http - 分页查询
GET http://127.0.0.1:9200/shopping/_search
{
"query" : {
"match_all" : {
}
},
"from" : 1,
"size" : 2,
"_source" : ["title"],
"sort" : {
"price" :{
"order" : "asc"
}
}
}
http - 多条件查询
GET http://127.0.0.1:9200/shopping/_search
{
"query" : {
"bool" : {
"should" : [
{
"match" : {
"category" : "小米"
}
},
{
"match" : {
"price" : 3999.00
}
}
],
"filter" : {
"range" : {
"price" : {
"gt" : 3000
}
}
}
}
}
}
http - 完全匹配
GET http://127.0.0.1:9200/shopping/_search
{
"query" : {
"match_phrase" : {
"category" : "小米"
}
},
"highlight" : {
"fields" : {
"category" : {}
}
}
}
http - 聚合查询
GET http://127.0.0.1:9200/shopping/_search
{
"aggs" : { //聚合操作
"price_avg" : { //名称,随意起名
"avg" : { //平均值
"field" : "price"
}
}
},
"size" : 0
}
http - 建立映射
PUT http://127.0.0.1:9200/user/_mapping
{
"properties" : {
"name" : {
"type" : "text",
"index" : true
},
"sex" : {
"type" : "keyword",
"index" : true
},
"tel" : {
"type" : "keyword",
"index" : false
}
}
}
http - 集群状态查询
GET http://localhost:9202/_cluster/health
http - 集群 - 创建索引
PUT http://127.0.0.1:9201/users
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
http - 集群 - 添加副本
PUT http://127.0.0.1:9201/users/_settings
{
"number_of_replicas" : 2
}
JavaAPI 操作
以下代码基于ESsearch7.8.0版本,对于8.x以上版本不适用。
JavaAPI - 建立依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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>
</project>
JavaAPI - 连接服务器
package com.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
public class ESTest_Client {
public static void main(String[] args) throws Exception{
//创建客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//关闭客户端
esClient.close();
}
}
JavaAPI - 索引 - 创建
package com.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
public class ESTest_Index_Create {
public static void main(String[] args) throws Exception{
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);
esClient.close();
}
}
JavaAPI - 索引 - 删除
package com.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
public class ESTest_Index_Delete {
public static void main(String[] args) throws Exception{
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());
esClient.close();
}
}
JavaAPI - 索引 - 查询
package com.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
public class ESTest_Index_Search {
public static void main(String[] args) throws Exception{
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());
esClient.close();
}
}
JavaAPI - 文档 - 插入
package com.es.test;
public class User {
private String Name;
private String Sex;
private Integer age;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
public class ESTest_Doc_Insert {
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("Messi6");
user.setAge(35);
user.setSex("male");
//注: 向ES插入数据必须将数据转换成json格式
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
// source函数表示请求体的数据源
request.source(userJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
}
JavaAPI - 文档 - 批量插入
package com.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
public class ESTest_Doc_Insert_Batch {
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", "Ronaldo", "age", 30, "sex", "male"));
request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "Messi", "age", 31, "sex", "male"));
request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "Modric", "age", 32, "sex", "male"));
request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "Ronaldo", "age", 33, "sex", "female"));
request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "Messi", "age", 34, "sex", "female"));
request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "Modric", "age", 35, "sex", "female"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
esClient.close();
}
}
JavaAPI - 文档 - 查询
package com.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
public class ESTest_Doc_Get {
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();
}
}
JavaAPI - 文档 - 删除
package com.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
public class ESTest_Doc_Delete {
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();
}
}
JavaAPI - 文档 - 批量删除
package com.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
public class ESTest_Doc_Delete_Batch {
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();
}
}
JavaAPI - 文档 - 修改
package com.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
public class ESTest_Doc_Update {
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, "age", 36);
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
}
JavaAPI - 文档 - 高级查询
package com.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.util.Iterator;
import java.util.Map;
public class ESTest_Doc_Query {
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.条件查询 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("sex", "male")));
// 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.查询排序 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
// 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(hits.getTotalHits());
// System.out.println(response.getTook());
// for( SearchHit hit : hits){
// System.out.println(hit.getSourceAsString());
// }
/* *********** 5.过滤字段 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
// 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(hits.getTotalHits());
// System.out.println(response.getTook());
// for( SearchHit hit : hits){
// System.out.println(hit.getSourceAsString());
// }
/* *********** 6.组合查询 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
// SearchSourceBuilder builder = new SearchSourceBuilder();
// BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// builder.query(boolQueryBuilder);
// boolQueryBuilder.must(QueryBuilders.matchQuery("name","messi"));
// boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex","male"));
// boolQueryBuilder.should(QueryBuilders.matchQuery("age", 35));
// boolQueryBuilder.should(QueryBuilders.matchQuery("age", 34));
// 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());
// }
/* *********** 7.范围查询 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
// SearchSourceBuilder builder = new SearchSourceBuilder();
// RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
// rangeQuery.gte(30);
// rangeQuery.lte(33);
// builder.query(rangeQuery);
// 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());
// }
/* *********** 8.模糊查询 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
// SearchSourceBuilder builder = new SearchSourceBuilder();
// builder.query(QueryBuilders.fuzzyQuery("name", "messi").fuzziness(Fuzziness.ONE));
// 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());
// }
/* *********** 9.高亮查询 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
// SearchSourceBuilder builder = new SearchSourceBuilder();
// TermQueryBuilder termsQueryBuilder = QueryBuilders.termQuery("name","messi");
// HighlightBuilder highlightBuilder = new HighlightBuilder();
// highlightBuilder.preTags("<font color='red'>");
// highlightBuilder.postTags("</font>");
// highlightBuilder.field("name");
// builder.highlighter(highlightBuilder);
// builder.query(termsQueryBuilder);
// 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());
// System.out.println(hit.getHighlightFields());
// }
/* *********** 10.聚合查询 *************** */
// SearchRequest request = new SearchRequest();
// request.indices("user");
// SearchSourceBuilder builder = new SearchSourceBuilder();
// AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
// builder.aggregation(aggregationBuilder);
// request.source(builder);
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
// System.out.println(new ObjectMapper().writeValueAsString(response.getAggregations()));
//
// SearchHits hits = response.getHits();
// System.out.println(hits.getTotalHits());
// System.out.println(response.getTook());
// for( SearchHit hit : hits){
// System.out.println(hit.getSourceAsString());
// }
/* *********** 11.分组查询 *************** */
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
builder.aggregation(aggregationBuilder);
request.source(builder);
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
System.out.println(new ObjectMapper().writeValueAsString(response.getAggregations()));
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
System.out.println(response.getTook());
for( SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
}
esClient.close();
}
}